[python] ctypes a file object

Zdenko Podobný zdposter na gmail.com
Středa Listopad 24 17:47:09 CET 2010


Zdravim,

potrebujem v pythone robit s viacstrankovymi tiff subormi a po roznych
testoch a googlovani som skoncil na tom, ze cisto python riesenie
neexistuje. Najrozumnejsie sa mi javi pouzitie ctypes a kniznice leptonica.
Problem je v tom, ze v niektorych pripadoch jej potrebujem posunut file
object.

Napr. (C kod):
filein = "phototest.tif";
fp = fopen(filein, "r");
tiffGetCount(fp, &npages);
fprintf(stderr, " Tiff: %d pages\n", npages);
fclose(fp);

Ked to skusim spravit v pythone, tak dostanem chybu:

import ctypes
leptonica = ctypes.CDLL("liblept.so")
filein = "phototest.tif"
fp = open(filein, "r")
npages = leptonica.tiffGetCount(fp)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know
how to convert parameter 1


Skusal som to aj cez PyFile_AsFile ale to tiez skonci s chybou:

>>> import ctypes as C
>>> lib = C.CDLL("liblept.so")
>>> class FILE(C.Structure):
...     pass
...
>>> FILE_P = C.POINTER(FILE)
>>> C.pythonapi.PyFile_AsFile.argtypes = [C.py_object]
>>> C.pythonapi.PyFile_AsFile.restype = FILE_P
>>> fp = C.pythonapi.PyFile_AsFile("phototest.tif")
>>> npages = lib.tiffGetCount(fp)
Error in tiffGetCount: stream not defined

Skusal som googlovat, ale sa nic funkcne sa mi nepodarilo zostavit... Vie mi
s tym niekto pomoc?

Zd.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.py.cz/pipermail/python/attachments/20101124/79ac57b5/attachment.html>


Další informace o konferenci Python