【问题标题】:Image exif/metadata reading/writing?图像 exif/元数据读/写?
【发布时间】:2017-09-11 17:02:35
【问题描述】:

一直在环顾四周仍然似乎没有很好的文档和最新的图像 exif/元数据操作。

我有以下代码使用piexif

import os
import piexif

for root, dirs, files in os.walk("C:\\Users\\Alan Partridge\\Desktop\\images"):
    if not files:
        continue
    prefix = os.path.basename(root)
    for f in files:
        #print os.path.join(root, f)
        exif_dict = piexif.load(os.path.join(root, f))
        print exif_dict
        for ifd in ("0th", "Exif", "GPS", "1st"):
            for tag in exif_dict[ifd]:
                print(piexif.TAGS[ifd][tag]["name"], exif_dict[ifd][tag])
        #os.rename(os.path.join(root, f), os.path.join(root, "{} ID {}".format(prefix, f)))

它会根据文件夹名称循环重命名文件(已注释掉),但现在,它会输出库可以找到的任何数据。

目前可以找到ImageDescription,没问题。我的问题是我将如何修改它?不幸的是,我无法理解如何做到这一点。也许我遗漏了一些明显的东西?

【问题讨论】:

    标签: python exif


    【解决方案1】:

    查看https://piexif.readthedocs.io/en/latest/sample.html 的示例。您缺少整个 piexif.dump()img.save() 操作。

    【讨论】:

      【解决方案2】:

      图片Exif信息可以在这个文档中找到:

      http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf

      使用 piexif 库构建 exif 信息,并写入目标文件:(python)

      构建 exif 信息以保留源文件的方向,由 windows 读取以显示正确的方向。

      来源:http://piexif.readthedocs.io/en/latest/functions.html?highlight=save

      zeroth_ifd = {piexif.ImageIFD.Orientation: image_orientation,
                    piexif.ImageIFD.ImageWidth: int(new_width),
                    piexif.ImageIFD.ImageLength: int(new_height),
                    piexif.ImageIFD.Software: u"piexif"
                    }
      
      exif_dict = {"0th":zeroth_ifd}
      exif_bytes = piexif.dump(exif_dict)
      
      with open(dest, 'r+b') as f:
          with Image.open(dest,'r') as image:
              image.save(dest, "jpeg", exif=exif_bytes)
      

      【讨论】:

        猜你喜欢
        • 2015-12-14
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        • 2013-07-11
        • 1970-01-01
        • 2013-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多