python修改图片大小
时间: 2023-10-17 13:16:47 浏览: 41
要使用Python调整图像大小,可以使用Pillow库。以下是一个简单的示例,它将图像缩小为给定的宽度和高度:
```python
from PIL import Image
def resize_image(input_image_path, output_image_path, size):
original_image = Image.open(input_image_path)
width, height = original_image.size
print(f"The original image size is {width} wide x {height} high")
resized_image = original_image.resize(size)
width, height = resized_image.size
print(f"The resized image size is {width} wide x {height} high")
resized_image.save(output_image_path)
if __name__ == '__main__':
resize_image('input.jpg', 'output.jpg', (1200, 800))
```
在此示例中,我们首先打开原始图像,然后使用`resize()`方法调整大小。最后,我们将新图像保存到输出路径中。在示例中,我们将图像缩小为1200像素宽和800像素高。
相关问题
python 修改图片大小的代码
以下是Python修改图片大小的代码示例:
```python
from PIL import Image
# 打开要修改大小的图片
img = Image.open('example.jpg')
# 修改大小为100x100
new_img = img.resize((100, 100))
# 保存修改后的图片
new_img.save('example_resized.jpg')
```
这段代码使用了Pillow库中的Image模块进行图片大小的修改,并将修改后的图片保存到指定路径中。
python如何改变图片大小
可以使用Pillow库中的Image模块来改变图片大小。具体操作如下:
1. 首先需要安装Pillow库,可以使用pip install Pillow命令进行安装。
2. 导入Image模块,使用open()方法打开图片文件。
3. 使用resize()方法改变图片大小,传入一个元组作为参数,元组中的两个值分别表示新的宽度和高度。
4. 使用save()方法保存修改后的图片。
示例代码如下:
```
from PIL import Image
# 打开图片文件
img = Image.open('example.jpg')
# 改变图片大小
new_size = (800, 600)
img = img.resize(new_size)
# 保存修改后的图片
img.save('example_resized.jpg')
```
以上就是使用Python改变图片大小的方法。