You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何修改TIF文件EXIF数据?Python piexif使用遇两大问题

Fixing GPS Metadata Editing Issues with piexif and Python PIL

Hey there! Let's work through your two piexif problems step by step:

1. How to Modify GPS Latitude and Longitude

First, let's break down that tuple format you're seeing: ((53, 1), (291191, 10000), (0, 1)). This represents the degrees, minutes, seconds of the coordinate, stored as fractions (numerator, denominator) to maintain precision.

For example:

  • (53,1) = 53 degrees
  • (291191,10000) = 291191/10000 = 29.1191 minutes
  • (0,1) = 0 seconds

To set a new coordinate, convert your target lat/long into this fraction tuple format. Also, don't forget to set the reference direction (North/South for latitude, East/West for longitude) using GPSLatitudeRef and GPSLongitudeRef—this is crucial for the coordinate to be interpreted correctly.

Here's an example of setting a new latitude (30° 15' 45" North) and longitude (120° 30' 10" East):

# Convert 30°15'45" North to fraction tuple
new_latitude = ((30, 1), (15, 1), (45, 1))
# Convert 120°30'10" East to fraction tuple
new_longitude = ((120, 1), (30, 1), (10, 1))

# Update the GPS dict
exif_dict['GPS'][piexif.GPSIFD.GPSLatitude] = new_latitude
exif_dict['GPS'][piexif.GPSIFD.GPSLatitudeRef] = "N"  # North
exif_dict['GPS'][piexif.GPSIFD.GPSLongitude] = new_longitude
exif_dict['GPS'][piexif.GPSIFD.GPSLongitudeRef] = "E"  # East

2. Fixing TIFF Metadata Not Saving

The issue here is that PIL's save() method for TIFF files doesn't handle the exif parameter the same way it does for JPEG. Piexif has a dedicated method to insert modified EXIF data into TIFF files without compressing them: piexif.insert().

Here's how to adjust your code to make this work:

  1. First, copy the original TIFF to the new file (preserves full image quality).
  2. Then use piexif.insert() to write your modified EXIF bytes into the new TIFF file.

Updated Full Code

import piexif
from PIL import Image
import shutil

# Disable image size limit
Image.MAX_IMAGE_PIXELS = 1000000000

# File paths (using raw string to avoid escape character issues)
fname_1 = r'D:\EZG\Codding\photo\iiq/eee.tif'
fname_2 = r'D:\EZG\Codding\photo\iiq/eee_change.tif'

# Step 1: Copy original TIFF to preserve quality
shutil.copy(fname_1, fname_2)

# Step 2: Load and modify EXIF data
exif_dict = piexif.load(fname_1)

# Update altitude
exif_dict['GPS'][piexif.GPSIFD.GPSAltitude] = (140, 1)

# Update latitude and longitude (replace with your target values)
new_latitude = ((30, 1), (15, 1), (45, 1))
new_longitude = ((120, 1), (30, 1), (10, 1))
exif_dict['GPS'][piexif.GPSIFD.GPSLatitude] = new_latitude
exif_dict['GPS'][piexif.GPSIFD.GPSLatitudeRef] = "N"
exif_dict['GPS'][piexif.GPSIFD.GPSLongitude] = new_longitude
exif_dict['GPS'][piexif.GPSIFD.GPSLongitudeRef] = "E"

# Dump modified EXIF to bytes
exif_bytes = piexif.dump(exif_dict)

# Step 3: Insert modified EXIF into the copied TIFF
piexif.insert(exif_bytes, fname_2)

This approach ensures your TIFF file stays at full size (no compression) while updating the GPS metadata correctly.

内容的提问来源于stack exchange,提问作者Maxler

火山引擎 最新活动