python改变显示图片的大小
时间: 2023-10-13 21:05:48 浏览: 75
使用PIL库可以轻松改变图片大小。以下是一个示例代码:
```python
from PIL import Image
# 打开图片
image = Image.open('example.jpg')
# 改变大小为宽度为200,高度按比例缩放
new_size = (200, int(image.size[1]/(image.size[0]/200)))
image = image.resize(new_size)
# 保存修改后的图片
image.save('example_resized.jpg')
```
在上述代码中,我们首先打开需要修改大小的图片,然后使用`resize()`方法将图片大小改变为指定的宽度和高度。最后,我们保存修改后的图片。
相关问题
opencv改变图片大小python
可以使用OpenCV库中的resize()函数来改变图片大小,具体步骤如下:
1. 导入OpenCV库
```python
import cv2
```
2. 读取图片
```python
img = cv2.imread('image.jpg')
```
3. 改变图片大小
```python
resized_img = cv2.resize(img, (new_width, new_height))
```
其中,new_width和new_height分别为新的宽度和高度。
4. 显示图片
```python
cv2.imshow('Resized Image', resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
完整代码如下:
```python
import cv2
# 读取图片
img = cv2.imread('image.jpg')
# 改变图片大小
new_width = 500
new_height = 500
resized_img = cv2.resize(img, (new_width, new_height))
# 显示图片
cv2.imshow('Resized Image', resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
python通过手指触摸改变图片大小
要通过手指触摸改变图片大小,可以使用Python中的OpenCV库和Pygame库。
首先,您需要安装这些库。您可以在终端中使用以下命令安装:
```
pip install opencv-python pygame
```
然后,您需要编写代码来捕获鼠标事件和处理图像大小更改。以下是一个示例代码:
```python
import cv2
import pygame
# 读取图像
img = cv2.imread('example.jpg')
# 设置窗口大小
window_size = (img.shape[1], img.shape[0])
screen = pygame.display.set_mode(window_size)
# 显示图像
screen.blit(pygame.surfarray.make_surface(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), (0, 0))
pygame.display.flip()
# 定义拖动过程的函数
def drag(surface, rect):
mouse_down = False
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mouse_down = True
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
mouse_down = False
elif event.type == pygame.MOUSEMOTION:
if mouse_down:
rect.move_ip(event.rel)
surface.fill((0, 0, 0))
surface.blit(pygame.surfarray.make_surface(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), rect)
pygame.display.flip()
pygame.time.wait(10)
# 定义缩放过程的函数
def zoom(surface, rect):
zoom_factor = 1.0
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
zoom_factor *= 1.1
new_width = int(rect.width * zoom_factor)
new_height = int(rect.height * zoom_factor)
rect.width = new_width
rect.height = new_height
surface.fill((0, 0, 0))
surface.blit(pygame.transform.scale(pygame.surfarray.make_surface(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), (new_width, new_height)), rect)
pygame.display.flip()
elif event.button == 5:
zoom_factor /= 1.1
new_width = int(rect.width * zoom_factor)
new_height = int(rect.height * zoom_factor)
rect.width = new_width
rect.height = new_height
surface.fill((0, 0, 0))
surface.blit(pygame.transform.scale(pygame.surfarray.make_surface(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), (new_width, new_height)), rect)
pygame.display.flip()
pygame.time.wait(10)
# 创建可移动和可缩放的矩形
rect = pygame.Rect(0, 0, img.shape[1], img.shape[0])
drag(screen, rect)
zoom(screen, rect)
```
这个示例代码创建了一个Pygame窗口并显示了一张图片。您可以通过拖动和缩放图像来更改其大小。要拖动图像,请单击并拖动鼠标。要缩放图像,请使用鼠标滚轮。
阅读全文
相关推荐
















