How to create an image from a string in python -
i'm having problem creating image binary string of info in python program. receive binary info via socket when seek methods read on here this:
buff = stringio.stringio() #buffer image stored #then concatenate info doing buff.write(data) #the info socket im = image.open(buff) i exception effect of "image type not recognized". know receiving info correctly because if write image file , open file works:
buff = stringio.stringio() #buffer image stored buff.write(data) #data socket output = open("tmp.jpg", 'wb') output.write(buff) output.close() im = image.open("tmp.jpg") im.show() i figure doing wrong in using stringio class i'm not sure
i suspect you're not seek-ing origin of buffer before pass stringio object pil. here's code demonstrates problem , solution:
>>> buff = stringio.stringio() >>> buff.write(open('map.png', 'rb').read()) >>> >>> #seek origin whole thing read pil >>> buff.seek(0) >>> >>> image.open(buff) <pngimageplugin.pngimagefile instance @ 0x00bd7dc8> >>> >>> #that worked.. if seek again: >>> image.open(buff) traceback (most recent phone call last): file "<stdin>", line 1, in <module> file "c:\python25\lib\site-packages\pil-1.1.6-py2.5-win32.egg\image.py", line 1916, in open raise ioerror("cannot identify image file") ioerror: cannot identify image file make sure phone call buff.seek(0) before reading stringio objects. otherwise you'll reading end of buffer, empty file , causing error you're seeing.
python string image sockets
No comments:
Post a Comment