[英]Resize images in python using PIL
我正在尝试调整一组图像的大小,大约是366,所以我创建了一个脚本,我首先在3上测试并且它成功了。
问题是当我处理整个文件夹时,它会返回以下错误:
resizeimage.imageexceptions.ImageSizeError: 'Image is too small, Image size : (275, 183), Required size : (399, 399)'
我的脚本应该迭代整个文件夹,调整图像大小,然后将输出文件存储在另一个文件夹中:
import os
from PIL import Image
from resizeimage import resizeimage
path = "/Users/sigc2sige/PycharmProjects/helloworld/photos"
size = (399, 399)
for file in os.listdir(path):
with open('/Users/sigc2sige/PycharmProjects/helloworld/photos/'+file, 'r+b') as f:
with Image.open(f) as image:
cover = resizeimage.resize_cover(image, size, Image.ANTIALIAS)
cover.save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)
我确实使用了这个指令:
thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
但我相信它会thumb = ImageOps.fit(image, size, Image.ANTIALIAS)
图像而不是调整它们的大小。
如果您对如何解决这个问题有任何想法,那就太好了。
对图像进行下采样(使其更小)是一回事,并且上采样(使其更大)是另一回事。 如果你想下采样,ANTIALIAS是一个不错的选择,如果你想上传,你可以使用其他过滤器。
import os
from PIL import Image
from resizeimage import resizeimage
path = "/Users/sigc2sige/PycharmProjects/helloworld/photos"
size = (399, 399)
for file in os.listdir(path):
with open('/Users/sigc2sige/PycharmProjects/helloworld/photos/'+file, 'r+b') as f:
with Image.open(f) as image:
if (image.size) >= size:
cover = resizeimage.resize_cover(image, size, Image.ANTIALIAS)
cover.save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)
else:
cover = image.resize(size, Image.BICUBIC).save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.