一个图片变大怎么编程的

时间:2025-01-28 03:47:58 网络游戏

使用OpenCV库(Python)

```python

import cv2

打开图片

img = cv2.imread('path_to_image.jpg')

while True:

cv2.imshow('Image', img)

key = cv2.waitKey(1)

放大图片

if key == ord('+'):

img = cv2.resize(img, None, fx=1.2, fy=1.2)

缩小图片

elif key == ord('-'):

img = cv2.resize(img, None, fx=0.8, fy=0.8)

退出循环

elif key == ord('q'):

break

cv2.destroyAllWindows()

```

使用C语言

```c

include

include

include

include

define MAX_WIDTH 400

define MAX_HEIGHT 400

struct picture {

int width;

int height;

int value;

};

struct picture a = {400, 400, 2};

void loadimage(struct picture *img, char *filename) {

FILE *fp = fopen(filename, "rb");

if (fp == NULL) {

printf("Cannot open file %s\n", filename);

exit(1);

}

fread(img, sizeof(struct picture), 1, fp);

fclose(fp);

}

void putimage(int x, int y, struct picture *img) {

setfillcolor(RED);

solidrectangle(x, y, x + img->width, y + img->height);

setfillcolor(BLACK);

solidrectangle(x, y, x + img->width, y + img->height);

putimage(x, y, img);

}

void show() {

setbkcolor(WHITE);

cleardevice();

setlinecolor(BLACK);

setlinestyle(PS_SOLID, 5);

}

int main() {

show();

loadimage(&maxImg, "1.jpg");

loadimage(&minImg, "1.jpg", 60, 60);

show_p();

while (1) {

if (_kbhit()) {

int key = _getch();

if (key == 27) { // Esc key

break;

} else if (key == '+') {

a.width = maxImg.width;

a.height = maxImg.height;

putimage(100, 0, &a);

} else if (key == '-') {

a.width = minImg.width;

a.height = minImg.height;

putimage(100, 0, &a);

}

}

}

return 0;

}

```

使用Python和PIL库

```python

from PIL import Image

打开一张图片

image = Image.open('my_photo.jpg')

定义缩放比例

new_size = (800, 600)

缩放图像

resized_image = image.resize(new_size)

保存缩放后的图像

resized_image.save('resized_photo.jpg')

```

使用HTML和JavaScript