[python] ctypes a file object

Petr Messner petr.messner na gmail.com
Středa Listopad 24 18:01:55 CET 2010


Jenom takovy napad - nebylo by lepsi fp =
C.pythonapi.PyFile_AsFile(open("phototest.tif")) ?

PM


2010/11/24 Zdenko Podobný <zdposter na gmail.com>

> 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.
>
> _______________________________________________
> Python mailing list
> Python na py.cz
> http://www.py.cz/mailman/listinfo/python
>
------------- dal?í ?ást ---------------
HTML p?íloha byla odstran?na...
URL: <http://www.py.cz/pipermail/python/attachments/20101124/f644497b/attachment.html>


Další informace o konferenci Python