是的,我也遇到过这个问题。对我来说,这是一个学习练习,因为我从未使用PIL裁剪或创建缩略图...
thumbnail(size, filter=None)
在原地用给定大小的新图像(第2页)替换原始图像。可选的filter参数与.resize()方法中的方式相同。
此操作通过保持纵横比(高度:宽度)来实现。结果图像尽可能大,同时仍适合于给定的大小。例如,如果图像im的大小为(400,150),那么im.thumbnail((40,40))后的大小将为(40,15)。
所以发生了什么
- 您使用了保持纵横比的thumbnail
- 您期望图像为40 x 40
- 您正在裁剪超出缩略图的实际大小
- 由于裁剪超出大小,最可能在底部出现黑色条带
我编写的代码重复了这个问题:
def croptest(file, width, height):
import Image as pil
import os
max_width = width
max_height = height
file, ext = os.path.splitext(file)
img = pil.open(file)
img.thumbnail((max_width, max_height), pil.ANTIALIAS)
img.save(file + ".thumb.jpeg", 'JPEG')
croppedImage = img.crop((10, 10, 40, 40))
croppedImage.save(file + ".croppedthumb.jpeg", 'JPEG')
if __name__ == "__main__":
croptest("Desktop.bmp", 50, 50)
Desktop.thumb.jpeg 的尺寸为 50 x 37,而 Desktop.croppedthumb.jpeg 的尺寸为 30 x 30,因此在底部有一条高度为 3 像素的黑线。
解决方案可以是在缩略图的实际尺寸内进行裁剪,或者找出如何忽略宽高比创建缩略图。
resize
会解决这个问题吗?我一直对thumbnail
的质量有问题。 - bobince