我正在尝试(从数码相机拍摄)大(巨大)图像,并将它们转换成可以在网络上显示的东西。这看起来很简单,而且可能应该如此。但是,当我尝试使用 PIL 创建缩略图版本时,如果我的源图像高于它的宽度,则生成的图像会旋转 90 度,这样源图像的顶部就位于生成图像的左侧。如果源图像宽于高,则生成的图像是正确的(原始)方向。这可能与我发送的 2 元组大小有关吗?我正在使用缩略图,因为它似乎是为了保留纵横比。还是我只是完全失明,做着愚蠢的事情?元组的大小为 1000,1000,因为我希望将最长边缩小到 1000 像素,同时保留 AR。
代码看起来很简单
img = Image.open(filename)
img.thumbnail((1000,1000), Image.ANTIALIAS)
img.save(output_fname, "JPEG")
提前感谢您的帮助。
最佳答案
我同意“unutbu”和 Ignacio Vazquez-Abrams 回答的几乎所有内容,但是......
EXIF 方向标志的值可以在 1 到 8 之间,具体取决于相机的握持方式。
人像照片可以在相机顶部左侧或右侧拍摄,风景照片可以倒置拍摄。
这里是考虑到这一点的代码(用 DSLR Nikon D80 测试)
import Image, ExifTags
try :
image=Image.open(os.path.join(path, fileName))
for orientation in ExifTags.TAGS.keys() :
if ExifTags.TAGS[orientation]=='Orientation' : break
exif=dict(image._getexif().items())
if exif[orientation] == 3 :
image=image.rotate(180, expand=True)
elif exif[orientation] == 6 :
image=image.rotate(270, expand=True)
elif exif[orientation] == 8 :
image=image.rotate(90, expand=True)
image.thumbnail((THUMB_WIDTH , THUMB_HIGHT), Image.ANTIALIAS)
image.save(os.path.join(path,fileName))
except:
traceback.print_exc()
关于python - PIL 缩略图正在旋转我的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4228530/