Use the lensfunpy.Database class as the main entry point
masterlensfunpy is through the lensfunpy.Database class. This class serves as the main entry point for accessing lens and camera data and performing lens correction tasks.repository·master·Indexed 41 days ago
https://github.com/letmaik/lensfunpyA Python wrapper for the lensfun_ library providing tools to correct lens-induced artifacts, including geometric distortion, vignetting, and transverse chromatic aberration (TCA). It provides a Database class to access lens and camera profiles and a Modifier class to apply corrections to images.
lensfunpy is through the lensfunpy.Database class. This class serves as the main entry point for accessing lens and camera data and performing lens correction tasks.The lensfunpy library is organized around several core classes that represent the components of the Lensfun database and the operations performed on images.
Database: The entry point for accessing the Lensfun database. It is used to search for cameras, lenses, and mounts.Camera: Represents a specific camera model. It contains metadata such as the maker, model, and crop factor.Mount: Represents a lens mount type (e.g., Nikon F AF).Lens: Represents a specific lens model. It contains optical parameters like focal length, aperture, and type.Modifier: Used to apply corrections to an image, such as Vignette or TCA (Transverse Chromatic Aberration) correction.To correct both vignetting and TCA, you must follow a specific order: Vignetting must be corrected first.
Steps:
lensfunpy.Modifier with the flags parameter set to lensfunpy.ModifyFlags.VIGNETTING | lensfunpy.ModifyFlags.TCA.mod.apply_color_modification(img) to handle vignetting.mod.apply_subpixel_distortion() to get the subpixel distortion coordinates.cv2.remap on each color channel (R, G, B) separately using the corresponding channel from the distortion coordinates.Note: The input image must be in a linear state.
import imageio
import cv2
import lensfunpy
db = lensfunpy.Database()
cam = db.find_cameras('Canon', 'Canon EOS 5D Mark IV')[0]
lens = db.find_lenses(cam, 'Sigma', 'Sigma 8mm f/3.5 EX DG circular fisheye')[0]
img = imageio.imread('/path/to/image.tiff')
focal_length = 8.0
aperture = 11
distance = 10
width = img.shape[1]
height = img.shape[0]
mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
mod.initialize(focal_length, aperture, distance, pixel_format=img.dtype, flags=lensfunpy.ModifyFlags.VIGNETTING | lensfunpy.ModifyFlags.TCA)
# 1. Vignette Correction
mod.apply_color_modification(img)
# 2. TCA Correction
undist_coords = mod.apply_subpixel_distortion()
img[..., 0] = cv2.remap(img[..., 0], undist_coords[..., 0, :], None, cv2.INTER_LINEAR)
img[..., 1] = cv2.remap(img[..., 1], undist_coords[..., 1, :], None, cv2.INTER_LINEAR)
img[..., 2] = cv2.remap(img[..., 2], undist_coords[..., 2, :], None, cv2.INTER_LINEAR)
imageio.imwrite('/path/to/image_corrected.tiff', img)To correct geometric distortion, use lensfunpy.Modifier. This process typically involves generating undistorted coordinates and remapping the image using a library like OpenCV.
Steps:
lensfunpy.Modifier instance with the lens, camera.crop_factor, width, and height.focal_length, aperture, distance, and the image's pixel_format.mod.apply_geometry_distortion() to get the undistorted coordinates.cv2.remap (OpenCV) or SciPy to apply the transformation to your image.Note: The lensfunpy.util module provides convenience functions for RGB images that handle both OpenCV and SciPy automatically.
import cv2
import lensfunpy
focal_length = 28.0
aperture = 1.4
distance = 10
image_path = '/path/to/image.tiff'
undistorted_image_path = '/path/to/image_undist.tiff'
img = cv2.imread(image_path)
height, width = img.shape[0], img.shape[1]
# Assuming 'lens' and 'cam' were found via Database()
mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
mod.initialize(focal_length, aperture, distance, pixel_format=img.dtype)
undist_coords = mod.apply_geometry_distortion()
img_undistorted = cv2.remap(img, undist_coords, None, cv2.INTER_LINEAR)
cv2.imwrite(undistorted_image_path, img_undistorted)Building from source on Windows is experimental and requires Visual Studio. Use the provided PowerShell script with environment variables to configure the build.
Environment Variables:
USE_CONDA: Set to '1' to use Conda, or '0' to build within an existing Python environment.PYTHON_VERSION: e.g., '3.10'PYTHON_ARCH: e.g., 'x86_64'NUMPY_VERSION: e.g., '2.0.*'Execution: Run the following in a PowerShell window:
$env:USE_CONDA = '1'
$env:PYTHON_VERSION = '3.10'
$env:PYTHON_ARCH = 'x86_64'
$env:NUMPY_VERSION = '2.0.*'
git clone https://github.com/letmaik/lensfunpy
cd lensfunpy
.github/scripts/build-windows.ps1You can install lensfunpy via pip. 64-bit binary wheels are available for Linux, macOS, and Windows.
pip install lensfunpyIf you need a specific lensfun version or cannot use binary wheels, follow these steps:
liblensfun:sudo apt-get install liblensfun-devgit clone https://github.com/lensfun/lensfun
cd lensfun
cmake .
sudo make installlensfunpy:git clone https://github.com/letmaik/lensfunpy
cd lensfunpy
pip install numpy cython
pip install .Troubleshooting ImportError: liblensfun.so.0 on Linux:
If the library is not found, add /usr/local/lib to your dynamic linker configuration:
echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/99local.conf
sudo ldconfigNote: The installation path might vary (e.g., /usr/local/lib/x86_64-linux-gnu or /usr/local/lib64).
# Install liblensfun from source
git clone https://github.com/lensfun/lensfun
cd lensfun
cmake .
sudo make install
# Install lensfunpy
git clone https://github.com/letmaik/lensfunpy
cd lensfunpy
pip install numpy cython
pip install .To correct vignetting, use lensfunpy.Modifier.apply_color_modification(img).
Important Requirements:
False, calibration data for that lens/camera combination might be missing.Steps:
lensfunpy.Modifier with lens, crop factor, width, and height.mod.initialize(focal_length, aperture, distance, pixel_format=img.dtype).mod.apply_color_modification(img) to modify the image in-place.import lensfunpy
import imageio
db = lensfunpy.Database()
cam = db.find_cameras('NIKON CORPORATION', 'NIKON D3S')[0]
lens = db.find_lenses(cam, 'Nikon', 'Nikkor AF 20mm f/2.8D')[0]
img = imageio.imread('/path/to/image.tiff')
focal_length = 20
aperture = 4
distance = 10
width = img.shape[1]
height = img.shape[0]
mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
mod.initialize(focal_length, aperture, distance, pixel_format=img.dtype)
did_apply = mod.apply_color_modification(img)
if did_apply:
imageio.imwrite('/path/to/image_corrected.tiff', img)
else:
print('vignetting not corrected, calibration data missing?')lensfunpy.TCAModel enumeration specifies the models used for Transverse Chromatic Aberration (TCA) correction, which addresses color fringing caused by different wavelengths of light focusing at different points.lensfunpy.LensType enumeration defines the category of the lens being corrected (e.g., RECTILINEAR, FISH_EYE, etc.). This is used to inform the correction algorithms about the expected geometry of the lens.The lensfunpy.ModifyFlags enumeration is used to specify which lens correction operations should be applied during processing. This allows you to selectively enable or disable effects like vignette correction or TCA (Transverse Chromatic Aberration) correction.
import lensfunpy
# Example usage of ModifyFlags (actual flags depend on implementation)
# flags = lensfunpy.ModifyFlags.VIGNETTE_CORRECTION | lensfunpy.ModifyFlags.TCA_CORRECTIONlensfunpy.Lens class is the primary interface for interacting with lens profiles provided by the Lensfun database. It allows you to access lens-specific parameters such as focal length, aperture, and crop factor, and serves as the basis for applying lens corrections (like vignette or TCA correction) to images.