我有以下场景:
- 我正在将 iPhone 中的图像连同 EXIF 信息发送到我的 Pyhon 套接字服务器。
- 我需要根据拍摄图像时的实际方向正确调整图像的方向。我知道 IOS 总是将图像保存为 Landscape Left,并将实际方向添加为 EXIF 字段 (EXIF.Image.Orientation)。
- 我正在读取 EXIF 字段以查看实际方向。然后我使用 wxpython 将图像旋转到正确的方向。
我正在使用 pyexiv2 进行 EXIF 操作。
问题:使用 wxpython 旋转图像时,包括缩略图在内的 EXIF 信息丢失。
我做了什么:我在旋转图像之前读取 EXIF。我重置了 EXIF 中的方向字段。然后我在旋转后把它放回去。
问题:
EXIF 中的缩略图没有旋转。因此,图像和缩略图具有不同的方向。
有问题吗?
是否有除 PIL 之外的任何模块来旋转图像并保留其 EXIF 信息?
缩略图方向是否有单独的 EXIF 字段?
有没有办法可以单独旋转缩略图?
感谢您的帮助...
最佳答案
这个解决方案对我有用: PIL thumbnail is rotating my image?
不需要检查它是 iPhone 还是 iPad: 如果照片有方向标签 – 旋转它。
from PIL import Image, ExifTags
try:
image=Image.open(filepath)
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation]=='Orientation':
break
exif = image._getexif()
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.save(filepath)
image.close()
except (AttributeError, KeyError, IndexError):
# cases: image don't have getexif
pass
之前:
之后:
关于python - 使用不带 PIL 的 Python 以 EXIF 中指定的方向旋转图像,包括缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13872331/