【问题标题】:Changing EXIF data of a TIF file affects the rest metadata更改 TIF 文件的 EXIF 数据会影响其余元数据
【发布时间】:2021-04-01 06:20:45
【问题描述】:

我正在使用piexif 库来修改 tif 文件的 EXIF 数据的 gps 高度。这是我的实现:

import piexif
from PIL import Image
Image.MAX_IMAGE_PIXELS = 1000000000

fname_1='initial.tif'
fname_2='new_file.tif'
img = Image.open(fname_1)
exif_dict = piexif.load(fname_1)
new_exif_dict = exif_dict
new_exif_dict['GPS'][piexif.GPSIFD.GPSAltitude] = (140, 1)
del new_exif_dict['0th'][50714]  # I delete this because it causes an error for class type for some reason. It happens even if I dump the original metadata as they are, to a new tif file

exif_bytes = piexif.dump(new_exif_dict)
im = Image.open(fname_1)
im.save(fname_2, exif=exif_bytes)

代码有效,但是现在新 tif 照片上的元数据比原始照片少了很多。甚至 GPS 坐标都丢失了。

我的问题是如何在不影响其余部分的情况下更改 tif 文件中关于 GPS 的元数据?

【问题讨论】:

  • 如果您以 new_exif_dict 作为 exif_dict 的完整副本开始,您只更改要修改的字段,而不是通过显式列出您的字段来创建 new_exif_dict,它会起作用吗?当前初始化它?
  • 刚刚尝试过,但许多元数据仍然丢失。但是,我只是根据您的建议更新了代码,因为它更简单。我被文档愚弄了!

标签: python metadata tiff exif piexif


【解决方案1】:

使用 PIL 并保存图像将重新压缩数据并重写或删除许多标签。您可以使用更改较少的其他库。例如,如果您只使用 tiff 文件,则可以通过命令行使用 tifftools python 包来执行此操作:

tifftools set --set GPSAltitude,0,GPSIFD:0 140,1 IMG_0036_1.tif new_file.tif

或通过python:

import tifftools

# Load all tag information from the file
info = tifftools.read_tiff('IMG_0036_1.tif')
# Get a reference to the IFD with GPS tags.  If the GPS data is in a different
# location, you might need to change this.
gpsifd = info['ifds'][0]['tags'][tifftools.Tag.GPSIFD.value]['ifds'][0][0]
# Set the altitude tag; this assumes it already exists and is stored as a rational
gpsifd['tags'][tifftools.constants.GPSTag.GPSAltitude.value]['data'] = [140, 1]
# Write the output; this copies all image data from the original file.
tifftools.write_tiff(info, 'new_file.tif')

免责声明:我是 tifftools 的作者。

【讨论】:

  • 像魅力一样工作!而且我也没有注意到任何元数据丢失。谢谢!
  • 请问,如何更新同一个 tif 文件的数据?上面的代码似乎不适用于同一个 tif 文件。
  • 我刚刚注意到 write_tif 上的 allowExisting=True 但由于某种原因它会导致永无止境的执行
  • tifftools always writes the entire file, so it has to write to a different file that the source. The allowExisting` 标志让它覆盖现有的输出文件(但与输入不同)。更高级别的命令写入一个新的临时文件,然后用它替换原始文件。例如,tifftools.commands.tiff_set 函数等效于 tiff set ... 命令行,并且会处理这个问题。
  • 明确一点,您是说无法更新文件的现有 exif 数据,对吧?
【解决方案2】:

尝试tifffile就地修改文件:

import tifffile

# open the TIFF file for reading and writing
with tifffile.TiffFile(filename, mode='r+b') as tif:
    # get the GPS tag of the first IFD/page
    gpstag = tif.pages[0].tags['GPSTag']
    # seek to the beginning of the GPSIFD
    tif.filehandle.seek(gpstag.valueoffset)
    # load the IFD
    gpsifd = tifffile.TiffPage(tif, None)
    # patch the GPSAltitude (6) field
    gpsifd.tags[6].overwrite(tif, (140, 1))

【讨论】:

  • 似乎不起作用。错误是 TypeError: unexpected keyword argument: mode 好像没有“mode”参数。你能修改你的答案吗?
  • 您可能使用的是过时版本的 tifffile。有一个mode parameter
  • 你是对的。 pip install 给了我一个旧版本,因为我使用的是 Python 3.6。从 v2020.10.1 开始,Tifffile 似乎需要 python >= 3.7
  • 你知道如何使用 python2.7 更新现有文件吗?
猜你喜欢
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 2010-11-07
  • 2012-06-01
相关资源
最近更新 更多