[python] ctypes a file object

Zdenko Podobný zdposter na gmail.com
Neděle Listopad 28 20:16:49 CET 2010


Aj toto skonci s chybovou hlaskou:

...
>>> fp2 = C.pythonapi.PyFile_AsFile(open("phototest.tif"))
>>> npages = lib.tiffGetCount(fp2)
Error in fopenTiff: invalid file descriptor
Error in tiffGetCount: tif not open for read

Zd.

2010/11/24 Petr Messner <petr.messner at gmail.com>

> Jenom takovy napad - nebylo by lepsi fp =
> C.pythonapi.PyFile_AsFile(open("phototest.tif")) ?
>
> PM
>
>
> 2010/11/24 Zdenko Podobný <zdposter at 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 at py.cz
>> http://www.py.cz/mailman/listinfo/python
>>
>
>
> _______________________________________________
> Python mailing list
> Python at py.cz
> http://www.py.cz/mailman/listinfo/python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.py.cz/pipermail/python/attachments/20101128/7caacdba/attachment.html>


Další informace o konferenci Python