[python] ctypes a file object

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


toto je trochu na mna zhurta ;-) Priklad by mi dost pomohol... Inak zabudol
som - potrebujem pokial mozno multiplatformove riesenie. Neviem, ci som
dobre pochopil odporucanie, ale na win som to skusil takto:

import ctypes, msvcrt, os

leptonica = ctypes.cdll.leptonlib
fp = open("phototest.tif", "r")
fh = msvcrt.get_osfhandle(fp.fileno())  # get file handle for the file
descriptor
fd = msvcrt.open_osfhandle(fh, os.O_RDONLY)  # Create a C runtime file
descriptor from the file handle
ofd = os.fdopen(fd, 'r')
npages = leptonica.tiffGetCount(ofd)

Vysledok:

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

Zd.
2010/11/24 Hynek Fabian <hynek.fabian at firma.seznam.cz>

> Numericky handle ziskas metodou fileno() pythoniho file objektu.
> Z nej muzes dostat ceckovou FILE* strukturu libc funkci fdopen().
>
>
> Zdenko Podobný (Wednesday 24 November 2010 17:47:09):
> > 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.py.cz/pipermail/python/attachments/20101128/9f286b6a/attachment.html>


Další informace o konferenci Python