如何使用 PIL 调整文件大小并将旋转 EXIF 信息应用到文件?
我正在尝试使用Python来调整图片大小。 使用我的相机,文件都是横向写入的。
exif 信息处理一个标签,要求图像查看器以某种方式旋转。 由于大多数浏览器不理解此信息,因此我想使用此 EXIF 信息旋转图像并保留所有其他 EXIF 信息。
你知道我如何使用 Python 做到这一点吗?
阅读 EXIF.py 源代码,我发现了类似的内容:
0x0112: ('Orientation',
{1: 'Horizontal (normal)',
2: 'Mirrored horizontal',
3: 'Rotated 180',
4: 'Mirrored vertical',
5: 'Mirrored horizontal then rotated 90 CCW',
6: 'Rotated 90 CW',
7: 'Mirrored horizontal then rotated 90 CW',
8: 'Rotated 90 CCW'})
如何使用此信息和 PIL 来应用它?
I am trying to use Python to resize picture.
With my camera, files are all written is landscape way.
The exif information handle a tag to ask the image viewer to rotate in a way or another.
Since most of the browser doesn't understand this information, I want to rotate the image using this EXIF information and keeping every other EXIF information.
Do you know how I can do that using Python ?
Reading the EXIF.py source code, I found something like that :
0x0112: ('Orientation',
{1: 'Horizontal (normal)',
2: 'Mirrored horizontal',
3: 'Rotated 180',
4: 'Mirrored vertical',
5: 'Mirrored horizontal then rotated 90 CCW',
6: 'Rotated 90 CW',
7: 'Mirrored horizontal then rotated 90 CW',
8: 'Rotated 90 CCW'})
How can I use this information and PIL to apply it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终使用了pyexiv2,但是在GNU以外的其他平台上安装有点棘手。
如果您发现有一些可以改进的地方(除了它仍然适用于 Python 2.5),请告诉我。
I finally used pyexiv2, but it is a bit tricky to install on other platforms than GNU.
If you see something that could be improved (except the fact that it is still for Python 2.5) then please let me know.
尽管 PIL 可以读取 EXIF 元数据,但它无法更改它并将其写回图像文件。
更好的选择是 pyexiv2 库。有了这个库,翻转照片的方向就变得非常简单。下面是一个示例:
这将方向设置为 6,对应于“顺时针旋转 90 度”。
Although PIL can read EXIF metadata, it doesn't have the ability to change it and write it back to an image file.
A better choice is the pyexiv2 library. With this library it's quite simple flip the photo's orientation. Here's an example:
This sets the orientation to 6, corresponding to "Rotated 90 CW".
首先,您必须确保您的相机确实具有旋转传感器。大多数不带传感器的相机型号只需将所有照片的方向标签设置为 1(水平)。
然后我建议在您的情况下使用 pyexiv2 和 pyjpegtran 。 PIL 不支持无损旋转,这是 pyjpegtran 的领域。 pyexiv2 是一个库,允许您将元数据从一个图像复制到另一个图像(我认为方法名称是 copyMetadata)。
您确定不想在浏览器中显示照片之前调整照片大小吗? 8 兆像素 JPEG 对于浏览器窗口来说太大了,并且会导致无休止的加载时间。
First you have to make sure that your camera actually has a rotation sensor. Most camera models without sensor simply set the Orientation Tag to 1 (Horizontal) for ALL pictures.
Then I recommend to use pyexiv2 and pyjpegtran in your case. PIL doesn't support lossless rotation, which is the domain of pyjpegtran. pyexiv2 is a library that allows you to copy metadata from one image to another (i think the method name is copyMetadata).
Are you sure that you don't want to resize your photos before displaying them in the browser? A 8 Megapixel JPEG is much too big for the browser window and will cause endless loading times.