PIL缩略图正在旋转我的图像? [英] PIL thumbnail is rotating my image?

查看:119
本文介绍了PIL缩略图正在旋转我的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数码相机拍摄大(大)图像,然后将其转换为可以在网络上显示的图像.这似乎很简单,也许应该如此.但是,当我尝试使用PIL创建缩略图版本时,如果我的源图像比宽高,则将生成的图像旋转90度,以使源图像的顶部在生成的图像的左侧.如果源图像的宽度大于高度,则结果图像是正确的(原始)方向.可能与我发送的2元组的大小有关吗?我使用的是缩略图,因为它似乎是为了保留宽高比.还是我只是完全盲目地做些愚蠢的事情?元组的大小为1000,1000,因为我希望将最长的一侧缩小到1000像素,同时保留AR.

I'm attempting to take large (huge) images (from a digital camera), and convert them into something that I can display on the web. This seems straightforward, and probably should be. However, when I attempt to use PIL to create thumbnail versions, if my source image is taller than it is wide, the resulting image is rotated 90 degrees, such that the top of the source image is on the left of the resulting image. If the source image is wider than it is tall, the resulting image is the correct (original) orientation. Could it have to do with the 2-tuple I send in as the size? I'm using thumbnail, because it appears it was meant to preserve the aspect ratio. Or am I just being completely blind, and doing something dumb? The size tuple is 1000,1000 because I want the longest side to be shrunk to 1000 pixels, while keeping AR preserved.

代码似乎很简单

img = Image.open(filename)
img.thumbnail((1000,1000), Image.ANTIALIAS)
img.save(output_fname, "JPEG")

在此先感谢您的帮助.

推荐答案

请注意,下面还有更好的答案.

当照片的高度大于宽度时,表示相机已旋转.一些相机可以检测到该情况并将该信息写入图片的EXIF元数据中.一些观看者会注意到此元数据并适当显示图像.

When a picture is taller than it is wide, it means the camera was rotated. Some cameras can detect this and write that info in the picture's EXIF metadata. Some viewers take note of this metadata and display the image appropriately.

PIL可以读取图片的元数据,但是在保存图像时它不能写入/复制元数据.因此,您的智能图像查看器将不会像以前那样旋转图像.

PIL can read the picture's metadata, but it does not write/copy metadata when you save an Image. Consequently, your smart image viewer will not rotate the image as it did before.

在@Ignacio Vazquez-Abrams的评论之后,您可以使用PIL这样读取元数据,并在必要时旋转:

Following up on @Ignacio Vazquez-Abrams's comment, you can read the metadata using PIL this way, and rotate if necessary:

import ExifTags
import Image

img = Image.open(filename)
print(img._getexif().items())
exif=dict((ExifTags.TAGS[k], v) for k, v in img._getexif().items() if k in ExifTags.TAGS)
if not exif['Orientation']:
    img=img.rotate(90, expand=True)
img.thumbnail((1000,1000), Image.ANTIALIAS)
img.save(output_fname, "JPEG")

但是请注意,上面的代码可能不适用于所有相机.

But be aware that the above code may not work for all cameras.

最简单的解决方案可能是使用其他程序制作缩略图.

The easiest solution maybe to use some other program to make thumbnails.

phatch 是用Python编写的批处理照片编辑器,可以处理/保留EXIF元数据.您可以使用此程序制作缩略图,也可以查看其源代码以了解如何在Python中执行此操作.我相信它使用 pyexiv2 处理EXIF元数据. pyexiv2可能比PIL的ExifTags模块能够更好地处理EXIF.

phatch is a batch photo editor written in Python which can handle/preserve EXIF metadata. You could either use this program to make your thumbnails, or look at its source code to see how to do this in Python. I believe it uses the pyexiv2 to handle the EXIF metadata. pyexiv2 may be able to handle EXIF better than the PIL's ExifTags module.

imagemagick 是制作批量缩略图的另一种可能性.

imagemagick is another possibility for making batch thumbnails.

这篇关于PIL缩略图正在旋转我的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆