在Python中修改或删除Exif标签“方向" [英] Modify or Delete Exif tag 'Orientation' in Python

查看:410
本文介绍了在Python中修改或删除Exif标签“方向"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论软件是否读取exif数据,我都需要以相同的方向显示我的一些图片.一种解决方案(唯一可能实际适用的解决方案)是根据exif标签旋转图像(如果存在),然后将该标签删除或修改为'1'.

I need some of my pictures to be displayed with the same orientation whether the software reads the exif data or not. One solution (the only one that could fit actually) would be to rotate the image according to the exif tag if it exists and then delete or modify this tag to '1'.

示例
比方说,图像的Orientation exif标签设置为3.我要做的就是根据此标签旋转此图像并以这种方式保存.这样,不会解释exif的软件仍可以正确显示它.尽管如果exif标签的Orientation仍设置为3,则解释Exif的软件将旋转我已经旋转的图像.所以这就是为什么我要将此标签设置为1(这意味着:无方向)或将其删除.

我的最终目标是,无论我使用哪种软件打开图像,图像都将始终显示相同的图像.

关于这个问题,Exif和Python有很多问题,等等等等.这是我听说过的库列表:

There are a lot of questions about that, Exif and Python, blah blah blah. Here is a list of libs that I heard about :

  • Pyexiv2:不适合,我目前正在将Python 3.3与Pillow一起使用
  • Gexiv2:看起来有点平台特定
  • EXIF.py
  • Pexif :看起来像最新的吗?
  • Pyexiv2 : Not suitable, I'm currently using Python 3.3 with Pillow
  • Gexiv2 : Looks like a bit platform specific
  • EXIF.py
  • Pexif : Looks like the most recent one ?

最佳做法是什么?是否有一个纯python解决方案? (我可以使用pip进行安装并将其放在我的requirements.txt中)是否可以使用某种新的lib3特定于Python3的库?

What are the best practices ? Is there a pure python solution ? (Which I could install with pip and put it in my requirements.txt) Is there some kind of new lib I could use which is specific to Python3 ?

我现在唯一的问题是修改这些exif数据并将其写入映像文件.我没问题读取exif数据,并根据方向标签旋转图像.有任何提示或建议吗?

My only problem right now is to modify and write those exif data to the image file. I have no problem to read exif data, and rotate the image according to the orientation tag. Any tips or advice on that ?

推荐答案

注意:

此答案适用于Python2.7-您也许可以在Python3中将Pillow换成PIL,但我不能凭经验谈谈.请注意,与pyexiv2和大多数其他允许您修改EXIF元数据的软件包不同,pexif库是独立的纯Python,因此它不需要绑定到计算机上可能不存在的任何C库.

note:

This answer works for Python2.7 - you can probably just swap Pillow for PIL in Python3, but I can't speak to that from experience. Note that unlike pyexiv2 and most of the other packages that allow you to modify EXIF metadata, the pexif library is standalone and pure python, so it does not need to bind to any C libraries that may not be present on your machine.

您需要使用两个单独的工具来完成两个步骤:

You need to use two separate tools for the two steps:

  • pexif to modify the metadata (EXIF tag)
  • PIL to rotate the image

四个步骤:

  • 打开图像
  • 检查方向(以后需要旋转)
  • 将方向更改为1
  • 保存图像

如果未找到方向标签,则会导致AttributeError,从而导致try:.另外请注意,pexif需要将方向标记作为一个列表(包含一个元素).

If the orientation tag isn't found, an AttributeError results, hence the try:. Also note that pexif needs the orientation tag to be a list (with one element).

import pexif
img = pexif.JpegFile.fromFile(temp_dir + filename)

try:
  #Get the orientation if it exists
  orientation = img.exif.primary.Orientation[0]
  img.exif.primary.Orientation = [1]
  img.writeFile(temp_dir + filename)

PIL部分:

现在旋转图像.

  • 打开图像
  • 应用必要的旋转/反射
  • 保存图像
  • from PIL import Image
    
    img = Image.open(temp_dir + filename)
    if orientation is 6: img = img.rotate(-90)
    elif orientation is 8: img = img.rotate(90)
    elif orientation is 3: img = img.rotate(180)
    elif orientation is 2: img = img.transpose(Image.FLIP_LEFT_RIGHT)
    elif orientation is 5: img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
    elif orientation is 7: img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
    elif orientation is 4: img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT)
    
    #save the result
    img.save(temp_dir + filename)
    

    将它们放在一起:

    if ctype == 'image/jpeg':
      from PIL import Image
      import pexif
    
      img = pexif.JpegFile.fromFile(temp_dir + filename)
    
      try:
        #Get the orientation if it exists
        orientation = img.exif.primary.Orientation[0]
        img.exif.primary.Orientation = [1]
        img.writeFile(temp_dir + filename)
    
        #now rotate the image using the Python Image Library (PIL)
        img = Image.open(temp_dir + filename)
        if orientation is 6: img = img.rotate(-90)
        elif orientation is 8: img = img.rotate(90)
        elif orientation is 3: img = img.rotate(180)
        elif orientation is 2: img = img.transpose(Image.FLIP_LEFT_RIGHT)
        elif orientation is 5: img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
        elif orientation is 7: img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
        elif orientation is 4: img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT)
    
        #save the result
        img.save(temp_dir + filename)
      except: pass
    

    这篇关于在Python中修改或删除Exif标签“方向"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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