Pro*_*eus 5 python python-imaging-library pillow
我想使用 Python 根据以下 2 个条件调整任何图像的大小。
1) 如果图像是横向的,则获取宽度,如果大于将1280图像宽度调整为 1280保持纵横比。
2)如果图像是纵向的,则获取高度,如果大于将高度1280调整为 1280保持纵横比。
在 Python 中,实现这一目标的最佳包/方法是什么?在不知道使用什么的情况下,我认为它是如何工作的。
伪代码:
If image.height > image.width:
size = image.height
If image.height < image.width:
size = image.width
If size > 1280:
resize maintaining aspect ratio
Run Code Online (Sandbox Code Playgroud)
我在看Pillow(PIL)。
您可以通过 PIL 来完成,如下所示:
import Image
MAX_SIZE = 1280
image = Image.open(image_path)
original_size = max(image.size[0], image.size[1])
if original_size >= MAX_SIZE:
resized_file = open(image_path.split('.')[0] + '_resized.jpg', "w")
if (image.size[0] > image.size[1]):
resized_width = MAX_SIZE
resized_height = int(round((MAX_SIZE/float(image.size[0]))*image.size[1]))
else:
resized_height = MAX_SIZE
resized_width = int(round((MAX_SIZE/float(image.size[1]))*image.size[0]))
image = image.resize((resized_width, resized_height), Image.ANTIALIAS)
image.save(resized_file, 'JPEG')
Run Code Online (Sandbox Code Playgroud)
此外,您可以删除原始图像并重命名调整大小。
| 归档时间: |
|
| 查看次数: |
1657 次 |
| 最近记录: |