更改 TIF 文件的 EXIF 数据会影响其余元数据
Changing EXIF data of a TIF file affects the rest metadata
我正在使用 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 的元数据?
使用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 的作者。
尝试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))
我正在使用 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 的元数据?
使用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 的作者。
尝试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))