Lens distortion correction for Python, a wrapper for lensfun
Project description
lensfunpy is an easy-to-use Python wrapper for the lensfun library.
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.
Installation
Binary wheels for Linux, macOS, and Windows are provided for Python 3.5 - 3.8. These can be installed with a simple pip install lensfunpy. Currently, Linux and macOS wheels are only available as 64 bit versions.
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.
NumPy Dependency
lensfunpy depends on NumPy. The minimum supported NumPy version depends on your Python version:
Python |
numpy |
3.5 |
>= 1.9 |
3.6 |
>= 1.11 |
3.7 |
>= 1.14 |
3.8 |
>= 1.17 |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for lensfunpy-1.8.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce78e2e1ef4cce89145328d057a7c5e392c5de36fb60b74d428ac47b55fed26b |
|
MD5 | 3716e7cd0427afbbce52379357976e53 |
|
BLAKE2b-256 | fa09ba7a5ccb9fdd4385f6a7469ec30549c981eb9627f6f9f39d7ca4de0f3ced |
Hashes for lensfunpy-1.8.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51b8874ee85e2c872c74af6198501e4ab021ccef49b7fa9a92711d2c45d9506c |
|
MD5 | 10aaaffd2c3564d730cd8fe145f779bd |
|
BLAKE2b-256 | b3ece199d49cafa7b849749404ab0951c83d67f3c31297ca4d2e42544e2c0bec |
Hashes for lensfunpy-1.8.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4b4f7490467a9a74af65133ee538be7caaaa03a767573f5d56adc2b26036dcb |
|
MD5 | cbf759021c457d6891224af812f8e1be |
|
BLAKE2b-256 | 71d935260ea67d04205a0411e102d5b3f1315dbfb2e47066689cb1bbdc7b7e77 |
Hashes for lensfunpy-1.8.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5364a6cd0f752af177f59084648ff867c79001fd4d210113a8caaa0707b6670 |
|
MD5 | 50125887f3c7e9f2cad28ac639a1f931 |
|
BLAKE2b-256 | 230cb9d66f9eb0a29731c9957f13ef7d9ea3f9e9ed22fe94ed5b5f9e7e2916eb |
Hashes for lensfunpy-1.8.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26cf988cff7b385b4f4953b589d8409ce7523fe9b2df4e4ec0d6911e1adbf8e2 |
|
MD5 | 2831a90f53f7fa2a00c822cdc16f4341 |
|
BLAKE2b-256 | d1ce07f1f5b569ea145f49ddc92ec14f0259087235e92b7f9987cd3f58336558 |
Hashes for lensfunpy-1.8.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3da58efab5ad166c9c9069cdcc92478f549a49ca87c203d9231f65807327cd0 |
|
MD5 | 36a842546e7941f0d5a8156a52f3a8b6 |
|
BLAKE2b-256 | 802887a3e6225eb6adacb3e011dbd9a66ae663b6bb4b2c64c26b3a1bfebf1b9a |
Hashes for lensfunpy-1.8.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e75dafe5b45c825bb9621281196958beeb331d0d4d173d43a3ba26f34b53491 |
|
MD5 | 508a3b5828a235b6fcd2be1306bd0205 |
|
BLAKE2b-256 | 83c644cb4305eff2727dc823dd5240b97e82d3e79cb456d8197e5d6ea58e8294 |
Hashes for lensfunpy-1.8.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc9bd764d97ba96023f5ffabd81466085368066bfed9cfedfdfca23f045c3f0d |
|
MD5 | e2e5b118b0c8e9e6fce144abd7f9252d |
|
BLAKE2b-256 | 5154d2d24279d8ea56c0b7d4b991a800280b7bcc94b5013ca8f843b18c385f61 |
Hashes for lensfunpy-1.8.0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10ff04f4ba70dce99921a59d2ff58fe6c99c118bfc0b3be523510ffd695ca200 |
|
MD5 | 1e705a297da915ef22cef9cf11e73515 |
|
BLAKE2b-256 | e4bf9042ac6e11553b7c122b784268c42dc83bf1ba54bf526b20e5bd9b057ea8 |
Hashes for lensfunpy-1.8.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e92de326719637fdbc9240ea01cceb5c75ef5bdee5a29a2abfe881cab1d40a59 |
|
MD5 | 0773026483f6f47923a01aa31f226027 |
|
BLAKE2b-256 | 75ed6f6059a67092cacf8ddad8493a96e945ebc1f9465dff24399884d2c420a3 |
Hashes for lensfunpy-1.8.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12afb6cd35180901be230d18b449b3d812b46a0011b33b76d3d2a15e6c810307 |
|
MD5 | 39b7ec2f4d33c7cd1331f222ece7d0e0 |
|
BLAKE2b-256 | 254d7a9f80c33492b5402eccf491166d7cdcd8d79392f970a686a86f58c169bd |
Hashes for lensfunpy-1.8.0-cp35-cp35m-macosx_10_9_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c2e376413b76745c20a425aa44f82b51ffdea6e5fd4797f307291716448d2b0 |
|
MD5 | 3b15604ba3329e79573cd98e632f3560 |
|
BLAKE2b-256 | 7334c2bb8553f04dd30790bfbd9241b1a7387b4682493b50cc4f8d6e71f402a3 |