letmaik / lensfunpy

📷 Lens distortion correction for Python, a wrapper for lensfun
https://pypi.python.org/pypi/lensfunpy
MIT License
145 stars 18 forks source link

check type in lensfunpy.Modifier().initialize() #27

Open dschneiderch opened 4 years ago

dschneiderch commented 4 years ago

a small usability improvement suggestion. I was consistently getting a segfault error at initialize with the code below because I forgot to include the [0] indexing for lens. It would make sense to check that lens is of type lensfunpy._lensfun.Lens and cam is of type lensfunpy._lensfun.Camera

import lensfunpy
import cv2
import piexif

image_path = 'swirf16.tif'
img_png = image_path+'.png'
undistorted_image_path = image_path+'-corrected.png'

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

meta = piexif.load(image_path)
focal_length=meta['Exif'][37386]
focal_length = focal_length[0]/focal_length[1]
aperture = meta['Exif'][33437]
aperture = aperture[0]/aperture[1]
distance = .3

cam_make = meta['0th'][271]
cam_model = meta['0th'][272]
lens_make = meta['Exif'][42035]
lens_model=meta['Exif'][42036]
db = lensfunpy.Database()
cam = db.find_cameras(cam_make, cam_model)[0]
lens = db.find_lenses(cam, lens_make, lens_model)
mod = lensfunpy.Modifier(lens, cam.crop_factor, width, height)
mod.initialize(focal_length, aperture, distance)
letmaik commented 4 years ago

Thanks for reporting, I can reproduce that.

The signature has the right type: https://github.com/letmaik/lensfunpy/blob/a16273a5488c9124cf5f035092cae031df213933/lensfunpy/_lensfun.pyx#L814 I would have thought Cython adds relevant checks to reject any objects that are not Lens...

EDIT: According to https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#extension-types-and-none it should check the type.