python - 使用不带 PIL 的 Python 以 EXIF 中指定的方向旋转图像,包括缩略图

标签 python ios image exif

我有以下场景:

  • 我正在将 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

之前:

Before

之后: After

关于python - 使用不带 PIL 的 Python 以 EXIF 中指定的方向旋转图像,包括缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13872331/

相关文章:

iOS 应用被终止问题

ios - 使用 JSON swift4 中的 key 解码结构

javascript - 在定时器代码上显示图像。想添加关闭功能

javascript - 如何使用 laravel 保存裁剪后的头像

python - 如何将日期时间从一个任意时区转换为另一个任意时区

Python bob 包 API - 如何格式化输入数据

ios - 错误 : could not find included file 'Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig' in search paths (in target 'Runner' )

image - Kubernetes kubectl 设置镜像部署无法--记录历史记录?

python - 根据像素的颜色生成RGB图像的 bool 蒙版的最有效的方法是什么?

python - 用Python读取jpeg文件,将其编码为Unicode并放入protobuf中