• Stars
    star
    127
  • Rank 282,790 (Top 6 %)
  • Language Cython
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📷 Lens distortion correction for Python, a wrapper for lensfun

lensfunpy

lensfunpy is an easy-to-use Python wrapper for the lensfun library.

API Documentation

Sample code

How to find cameras and lenses:

import lensfunpy

cam_maker = 'NIKON CORPORATION'
cam_model = 'NIKON D3S'
lens_maker = 'Nikon'
lens_model = 'Nikkor 28mm f/2.8D AF'

db = lensfunpy.Database()
cam = db.find_cameras(cam_maker, cam_model)[0]
lens = db.find_lenses(cam, lens_maker, lens_model)[0]

print(cam)
# Camera(Maker: NIKON CORPORATION; Model: NIKON D3S; Variant: ;
#        Mount: Nikon F AF; Crop Factor: 1.0; Score: 0)

print(lens)
# Lens(Maker: Nikon; Model: Nikkor 28mm f/2.8D AF; Type: RECTILINEAR;
#      Focal: 28.0-28.0; Aperture: 2.79999995232-2.79999995232;
#      Crop factor: 1.0; Score: 110)

How to correct lens distortion:

import cv2 # OpenCV library

focal_length = 28.0
aperture = 1.4
distance = 10
image_path = '/path/to/image.tiff'
undistorted_image_path = '/path/to/image_undist.tiff'

im = cv2.imread(image_path)
height, width = im.shape[0], im.shape[1]

mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
mod.initialize(focal_length, aperture, distance)

undist_coords = mod.apply_geometry_distortion()
im_undistorted = cv2.remap(im, undist_coords, None, cv2.INTER_LANCZOS4)
cv2.imwrite(undistorted_image_path, im_undistorted)

It is also possible to apply the correction via SciPy instead of OpenCV. The lensfunpy.util module contains convenience functions for RGB images which handle both OpenCV and SciPy.

How to correct lens vignetting:

import lensfunpy
import imageio

db = lensfun.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)

did_apply = mod.apply_color_modification(img)
if did_apply:
    imageio.imwrite('corrected.tiff', img)
else:
    print('vignetting not corrected, calibration data missing?')

Installation

Install lensfunpy by running:

pip install lensfunpy

64-bit binary wheels are provided for Linux, macOS, and Windows.

Installation from source on Linux/macOS

If you have the need to use a specific lensfun version or you can't use the provided binary wheels then follow the steps in this section to build lensfunpy from source.

First, install the lensfun library on your system.

On Ubuntu, you can get (an outdated) version with:

sudo apt-get install liblensfun-dev

Or install the latest developer version from the Git repository:

git clone https://github.com/lensfun/lensfun
cd lensfun
cmake .
sudo make install

After that, install lensfunpy using:

git clone https://github.com/letmaik/lensfunpy
cd lensfunpy
pip install numpy cython
pip install .

On Linux, if you get the error "ImportError: liblensfun.so.0: cannot open shared object file: No such file or directory" when trying to use lensfunpy, then do the following:

echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/99local.conf
sudo ldconfig

The lensfun library is installed in /usr/local/lib when compiled from source, and apparently this folder is not searched for libraries by default in some Linux distributions. Note that on some systems the installation path may be slightly different, such as /usr/local/lib/x86_64-linux-gnu or /usr/local/lib64.

Installation from source on Windows

These instructions are experimental and support is not provided for them. Typically, there should be no need to build manually since wheels are hosted on PyPI.

You need to have Visual Studio installed to build lensfunpy.

In a PowerShell window:

$env:USE_CONDA = '1'
$env:PYTHON_VERSION = '3.7'
$env:PYTHON_ARCH = 'x86_64'
$env:NUMPY_VERSION = '1.14.*'
git clone https://github.com/letmaik/lensfunpy
cd lensfunpy
.github/scripts/build-windows.ps1

The above will download all build dependencies (including a Python installation) and is fully configured through the four environment variables. Set USE_CONDA = '0' to build within an existing Python environment.

NumPy Dependency

lensfunpy depends on NumPy. The minimum supported NumPy version depends on your Python version:

Python numpy
3.7 >= 1.14
3.8 >= 1.17
3.9 >= 1.19
3.10 >= 1.21
3.11 >= 1.23

More Repositories

1

rawpy

📷 RAW image processing for Python, a wrapper for libraw
Cython
481
star
2

pyvirtualcam

🎥 Send frames to a virtual camera from Python
C++
330
star
3

vscode-git-tree-compare

🎄 VS Code extension for comparing the git working tree against a branch, tag, or commit, displayed as folder tree
TypeScript
48
star
4

node-virtualcam

🎥 Send frames to a virtual camera from Node.js
C
43
star
5

monadiccp

Monadic Constraint Programming framework
Haskell
26
star
6

evolutionary-container-loading

Container loading with evolutionary algorithms in Scala
Scala
20
star
7

rawpy-notebooks

A collection of Jupyter notebooks using rawpy
Jupyter Notebook
19
star
8

xndarray

▦ Multidimensional arrays with semantics in JavaScript
JavaScript
8
star
9

gwt-phantomjs-junit-runstyle

A GWT JUnit run style for local testing with PhantomJS
Java
7
star
10

pixelpitch

Lists current cameras with their physical pixel sizes (pixel pitch)
Python
5
star
11

hungrymonkey

🎮 The Hungry Monkey Game
JavaScript
5
star
12

meeting-backgrounds

🖼️ Download and manage meeting background collections.
Python
5
star
13

PSExercise

▶️ YouTube videos on a schedule
PowerShell
4
star
14

chip8

🕹 CHIP-8 emulator written in AssemblyScript
TypeScript
4
star
15

crowfood

Creates dependency files from C/C++ code for creating dependency graphs with snakefood
Python
3
star
16

pixelart

🎨 My pixel art
3
star
17

valijson-wasm

valijson in the browser
HTML
2
star
18

playingcards-rooms

🎲 Custom rooms for playingcards.io
2
star
19

hungrypuffin

🎮 The Hungry Puffin Game
TypeScript
2
star
20

leafy

🍁 A minigame about leaves
C#
2
star
21

profviewer

🩺 Online viewer for runtime profiles.
JavaScript
2
star
22

gwt-webdriver-junit-runstyle

A GWT JUnit run style for remote testing via WebDriver's JSON over HTTP protocol
Java
1
star
23

exhaust

🌌 Exhaustively enumerate a combinatorial space
Python
1
star
24

ld-trace

ld --trace-symbol, but better.
Python
1
star
25

vscode-metalevel-references

VS Code extension that adds "Find All References" to the file and folder level
TypeScript
1
star
26

lensfunx

Experiments with lensfun
Python
1
star
27

rna-and-adp-wiki

A wiki about RNA folding basics and ADP implementations.
1
star
28

sweetdreams-netvibes-theme

Adaptation of iGoogle's "Sweet Dreams" theme for netvibes
1
star