图像exif/元数据读/写?

2024-04-29 07:43:36 发布

您现在位置:Python中文网/ 问答频道 /正文

一直在四处寻找,因为仍然似乎没有很好的文档记录和最新的图像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,这很好。我的问题是我如何着手修改它?不幸的是,我不知道该怎么做。也许我遗漏了一些显而易见的东西?在


Tags: 数据pathinimportforostagroot
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)

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

相关问题 更多 >