Calibrate Lens Focal Length with GExiv2 in GIMP
This article provides a practical guide on calibrating and adjusting lens focal length parameters in GIMP using the GExiv2 metadata library. When camera metadata is missing, incomplete, or inaccurate, automated lens correction tools cannot apply proper distortion and vignetting profiles. By utilizing GExiv2 through GIMP's Python interface, you can inspect, calibrate, and write precise optical parameters to image files so that lens calibration algorithms function correctly.
The Role of GExiv2 in Lens Correction
GIMP utilizes GExiv2—a GObject-based wrapper around the Exiv2 library—to manage EXIF, XMP, and IPTC metadata. Automated optical correction modules (such as those powered by the Lensfun database) rely entirely on specific EXIF tags to identify the lens model, actual focal length, crop factor, and aperture. If an adapted vintage lens, manual prime, or third-party optical system is used, these tags are often absent or erroneous, preventing correction filters from calculating accurate geometric transformations.
Step 1: Open the Python-Fu Console
To manipulate metadata directly using GExiv2 within GIMP:
- Open the target image in GIMP.
- Navigate to Filters > Python-Fu > Console from the top menu.
- This interface gives you direct access to the GIMP procedural
database and system-level Python bindings, including
gi.repository.GExiv2.
Step 2: Initialize GExiv2 and Read Image Metadata
In the Python console, access the active image path and load its metadata buffer via GExiv2:
import gi
gi.require_version('GExiv2', '0.10')
from gi.repository import GExiv2
# Get the active image filepath
image = gimp.image_list()[0]
filepath = image.filename
# Initialize the metadata container
metadata = GExiv2.Metadata()
metadata.open_path(filepath)Step 3: Calibrate Focal Length and Lens Tags
Accurate profile-based lens correction requires three critical EXIF tags:
Exif.Photo.FocalLength: The physical focal length of the lens at capture.Exif.Photo.FocalLengthIn35mmFilm: The 35mm equivalent focal length based on sensor crop factor.Exif.Photo.LensModel: The exact name of the lens matching calibration databases.
Set these tags using rational numbers or appropriate strings. For example, to calibrate a 50mm manual lens on an APS-C sensor (1.5x crop factor, resulting in a 75mm equivalent):
# Set the physical focal length as a rational string (numerator/denominator)
metadata.set_tag_string("Exif.Photo.FocalLength", "50/1")
# Set the 35mm equivalent focal length
metadata.set_tag_string("Exif.Photo.FocalLengthIn35mmFilm", "75")
# Set the exact lens model for database matching
metadata.set_tag_string("Exif.Photo.LensModel", "Manual 50mm f/1.8")
# Optional: Ensure aperture is defined if vignetting calibration is needed
metadata.set_tag_string("Exif.Photo.FNumber", "28/10") # f/2.8Step 4: Commit Metadata to the File and Reload in GIMP
Save the calibrated EXIF data back to the image file:
metadata.save_file(filepath)After saving, close and reload the image in GIMP, or update the image's internal metadata buffer via Image > Metadata > View Metadata to verify that the calibrated focal length and lens model appear correctly in the file properties.
Step 5: Execute Profile-Based Lens Correction
Once the metadata accurately reflects the calibrated optical parameters:
- Open Filters > Distorts > Lens Distortion or
invoke a Lensfun-backed correction plug-in (such as
gegl:lens-correctionor G'MIC). - The filter will query the image's updated
Exif.Photo.LensModelandExif.Photo.FocalLengthfields. - The engine automatically maps the calibrated values against known distortion curves (barrel, pincushion, and chromatic aberrations) to perform mathematically precise geometric corrections across the frame.