python修改图片分辨率
时间: 2023-07-26 13:09:30 浏览: 130
使用Python可以使用Pillow库来修改图片分辨率。以下是一个简单的示例:
```python
from PIL import Image
# 打开图片
img = Image.open('example.jpg')
# 修改分辨率为800x600
img = img.resize((800, 600))
# 保存修改后的图片
img.save('example_resized.jpg')
```
在这个示例中,我们首先打开了一张名为`example.jpg`的图片。然后,使用`resize()`方法将其分辨率修改为800x600,并将结果保存到`img`变量中。最后,使用`save()`方法将修改后的图片保存到名为`example_resized.jpg`的文件中。
请注意,这个示例只修改了图片的分辨率,并没有进行其他的编辑。如果需要进行其他的编辑,可以参考Pillow库的文档,使用其它的方法来完成。
相关问题
python读取图片分辨率
可以使用Python的PIL库(Python Imaging Library)来读取图片的分辨率。下面是一个示例代码:
```python
from PIL import Image
# 打开图片文件
image = Image.open('image.jpg')
# 获取图片分辨率
width, height = image.size
# 输出分辨率
print(f"图片分辨率为:{width}x{height}")
# 关闭图片文件
image.close()
```
请注意,上述代码中的`image.jpg`是要读取的图片文件路径,你需要根据实际情况进行修改。
python改变图片分辨率的代码
在Python中,可以使用PIL(Python Imaging Library)或其更现代的替代品Pillow库来修改图片的分辨率。以下是一个简单的示例,展示如何通过Pillow库来调整图片的宽度和高度:
```python
from PIL import Image
def resize_image(input_path, output_path, width, height):
# 打开图片
with Image.open(input_path) as img:
# 调整分辨率
resized_img = img.resize((width, height), resample=Image.LANCZOS)
# 保存处理后的图片
resized_img.save(output_path)
# 使用方法
resize_image('input.jpg', 'output_resized.jpg', 800, 600)
```
在这个例子中,`input_path`是原图的路径,`output_path`是新分辨率图片的保存路径,`width`和`height`分别是新的宽度和高度。`resample`参数指定了插值方法,LANCZOS是一种高质量的插值选项。
阅读全文
相关推荐
















