使用 PIL 加载图像时,有没有办法忽略 EXIF 方向数据? [英] Is there a way to ignore EXIF orientation data when loading an image with PIL?

查看:65
本文介绍了使用 PIL 加载图像时,有没有办法忽略 EXIF 方向数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 PIL 加载图像时出现了一些不需要的旋转.我正在加载图像样本及其二进制掩码,所以这会导致问题.我正在尝试将代码转换为使用 openCV,但事实证明这很棘手.我在 Image.load() 下的文档中没有看到任何参数,但我希望有一个我还没有找到的解决方法......

I'm getting some unwanted rotation when loading images using PIL. I'm loading image samples and their binary mask, so this is causing issues. I'm attempting to convert the code to use openCV instead, but this is proving sticky. I haven't seen any arguments in the documentation under Image.load(), but I'm hoping there's a workaround I just haven't found...

推荐答案

有,但我还没写完.基本上,如果您加载带有 EXIF "Orientation" 字段集的图像,您可以获得该参数.

There is, but I haven't written it all up. Basically, if you load an image with EXIF "Orientation" field set, you can get that parameter.

首先,使用来自 PIL GitHub 源的此图像进行快速测试 Pillow-7.1.2/Tests/images/hopper_orientation_6.jpg 并在其上运行 jhead 您可以看到EXIF方向是6:

First, a quick test using this image from the PIL GitHub source Pillow-7.1.2/Tests/images/hopper_orientation_6.jpg and run jhead on it you can see the EXIF orientation is 6:

jhead /Users/mark/StackOverflow/PillowBuild/Pillow-7.1.2/Tests/images/hopper_orientation_6.jpg
File name    : /Users/mark/StackOverflow/PillowBuild/Pillow-7.1.2/Tests/images/hopper_orientation_6.jpg
File size    : 4951 bytes
File date    : 2020:04:24 14:00:09
Resolution   : 128 x 128
Orientation  : rotate 90     <--- see here
JPEG Quality : 75

现在在 PIL 中执行此操作:

Now do that in PIL:

from PIL import Image

# Load that image
im = Image.open('/Users/mark/StackOverflow/PillowBuild/Pillow-7.1.2/Tests/images/hopper_orientation_6.jpg')                         

# Get all EXIF data
e = im.getexif()

# Specifically get orientation
e.get(0x0112)                                                                                                                       
# prints 6

现在点击可以计算出您的图像如何旋转并撤消它.

Now click on the source and you can work out how your image has been rotated and undo it.

或者,您可能完全不专业 ;-) 并创建一个名为 SneakilyRemoveOrientationWhileNooneIsLooking(filename) 的函数,然后将(子进程)转为 exiftool 并删除方向:

Or, you could be completely unprofessional ;-) and create a function called SneakilyRemoveOrientationWhileNooneIsLooking(filename) and shell out (subprocess) to exiftool and remove the orientation with:

exiftool -Orientation=  image.jpg

这篇关于使用 PIL 加载图像时,有没有办法忽略 EXIF 方向数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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