在Python中修改或删除Exif标签'Orientation'

时间:2014-02-26 15:28:27

标签: python python-imaging-library exif

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

示例
假设图像的Orientation exif标记设置为3.我想要做的是根据此标记旋转此图像并以这种方式保存。因此,不解释exif的软件仍然会以良好的方向显示它。虽然如果exif标签Orientation仍然设置为3,那么解释Exif的软件将旋转我已旋转的图像。这就是为什么我想将此标记设置为1(这意味着:没有方向)或删除它。

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

有很多问题,Exif和Python,等等等等等等。这是我听说过的lib列表:

  • Pyexiv2:不合适,我目前正在使用带有Pillow的Python 3.3
  • Gexiv2:看起来有点特定于平台
  • EXIF.py
  • Pexif:看起来像是最近的那个?

最佳做法是什么?有纯粹的python解决方案吗? (我可以用pip安装并将它放在我的requirements.txt中)是否有某种新的lib我可以使用哪种特定于Python3?

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

3 个答案:

答案 0 :(得分:12)

注意:

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

概述

您需要为这两个步骤使用两个单独的工具:

  • pexif修改元数据(EXIF标记)
  • PIL旋转图片

pexif部分:

四个步骤:

  • 打开图片
  • 检查方向(稍后需要轮换)
  • 将方向更改为1
  • 保存图片

如果找不到方向标记,则会产生AttributeError,因此会导致try:。另请注意,pexif需要将orientation标记作为列表(包含一个元素)。

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部分:

现在旋转图像。可能方向含义(用于生成旋转查找表)的来源是here

  • 打开图片
  • 应用必要的旋转/反射
  • 保存图片
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

答案 1 :(得分:1)

在这个Git存储库中查看EXIF和Pyxif: https://github.com/hMatoba

两者都提供简单而纯粹的Python方法来读取和修改EXIF标记。与PIL或Pillow结合使用时,这些库非常棒,而且它们不会带来其他大型软件包的开销,例如pyexif2。

答案 2 :(得分:-2)

您可以使用Pillow save Exif数据

img.save(output, img_format, quality, exif=img_info.get('exif'))
相关问题