我正在使用pylibtiff
图书馆(托管在这里),我正在尝试扩展class TIFF(ctypes.c_void_p)
. 我在使用以下方法时遇到问题:
# lib has been loaded with the path to libtiff.so, or equivalent
libtiff = ctypes.cdll.LoadLibrary(lib)
class TIFF(ctypes.c_void_p):
@classmethod
def open(cls, filename, mode='r'):
""" Open tiff file as TIFF.
"""
tiff = libtiff.TIFFOpen(filename, mode)
if tiff.value is None:
raise TypeError ('Failed to open file '+`filename`)
return tiff
(这里全班)
现在,这里的预期使用模式是我们打开一个文件并接收一个class TIFF
实例,如下所示:
import libtiff
t = libtiff.TIFF.open('myfile.tiff')
# e.g. grab a numpy array of the data:
arr = t.read_image()
这是什么魔法?如何将 libtiff 中的 C 变量键入为 TIFF*
返回 pythonland 并自动成为class TIFF
实例?大概这与子类化有关ctypes.c_void_p
我问是因为我试图覆盖一个方法class TIFF
:
import libtiff
class TIFFdifferent(libtiff.TIFF):
def read_image(self, verbose=False, different=False):
if not different:
return libtiff.TIFF.read_image(self, verbose)
# my different code ...
但是,当我尝试创建一个class TIFFdifferent
实例时,我得到了一个class TIFF
:
In [3]: t = libtiffdifferent.TIFFdifferent.open('file.tiff')
In [4]: arr = t.read_image(different=True)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-8bdc24ec874c> in <module>()
----> 1 arr = t.read_image(different=True)
TypeError: read_image() got an unexpected keyword argument 'different'
In [5]: t.read_image?
Type: instancemethod
String Form:<bound method TIFF.read_image of <TIFF object at 0x10b67df80>>
File: /Library/Python/2.7/site-packages/libtiff/libtiff_ctypes.py
Definition: t.read_image(self, verbose=False)
Docstring:
Read image from TIFF and return it as an array.
In [6]: t
Out[6]: <TIFF object at 0x10b67df80>
所以,我需要做的也是重写——如果不了解从 C到 python实例的魔法转换,没有构造函数、强制转换或任何其他显式的东西open
,我不知道该怎么做。TIFF*
class TIFF