Skip to main content
 首页 » 编程设计

python之如何更改 EXIF 方向标签(最好使用 PythonMagick)

2025年05月04日7wuhuacong

我正在使用 PythonMagick 缩放和旋转一些 JPEG。理想情况下,我也可以更新 EXIF Orientation 标签。但是,虽然我可以检索此标记的值,但我似乎无法修改它。考虑:

from PythonMagick import Image 
 
i = Image("image.jpg") 
print i.attribute("EXIF:Orientation") 
i.attribute("EXIF:Orientation", "5") 
i.write("image-modified.jpg") 

运行它会显示图像的原始方向:

exarkun@top:/tmp$ python broken.py  
6 
exarkun@top:/tmp$ 

只要一开始不是 5,exiftool 就会证明新文件没有调整方向:

exarkun@top:/tmp$ exiftool image.jpg | grep Orient 
Orientation                     : Rotate 90 CW 
exarkun@top:/tmp$ exiftool image-modified.jpg | grep Orient 
Orientation                     : Rotate 90 CW 
exarkun@top:/tmp$  

为什么 ImageMagick 不写出修改后的方向?我怎样才能得到它?

请您参考如下方法:

这个问题类似于:Exif manipulation library for python

另请参阅:Copying and Writing EXIF information from one image to another using pyexiv2

上面涉及到使用pyexiv2读写exif数据。我意识到这不是使用 PythonMagick,而是使用“高级”python 库来编写 EXIF 数据,至少据我所知这是目前最好的(唯一的?)解决方案。

看来 ImageMagick 是有能力的,所以也许有人可以在这里扩展如何从 python 完成它;参见:How do I add exif data to an image?