PIL: reading bytes from Image

Larry Bates lbates at websafe.com
Mon Mar 12 10:22:25 EDT 2007


cyberco wrote:
> Thanks,
> 
> I've tried the StringIO option as follows:
> 
> =================================
> img = Image.open('/some/path/img.jpg')
> img.thumbnail((640,480))
> file = StringIO, StringIO()
> img.save(file, 'JPEG')
> 
> =================================
> 
> But it gives me:
> 
> =================================
> 
> exceptions.TypeError                                 Traceback (most
> recent call last)
> 
> C:\Python24\lib\site-packages\PIL\Image.py in save(self, fp, format,
> **params)
>    1303
>    1304         try:
> -> 1305             save_handler(self, fp, filename)
>    1306         finally:
>    1307             # do what we can to clean up
> 
> C:\Python24\lib\site-packages\PIL\JpegImagePlugin.py in _save(im, fp,
> filename)
>     407         )
>     408
> --> 409     ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0,
> rawmode)])
>     410
>     411 def _save_cjpeg(im, fp, filename):
> 
> C:\Python24\lib\site-packages\PIL\ImageFile.py in _save(im, fp, tile)
>     464     bufsize = max(MAXBLOCK, im.size[0] * 4) # see RawEncode.c
>     465     try:
> --> 466         fh = fp.fileno()
>     467         fp.flush()
>     468     except AttributeError:
> 
> TypeError: descriptor 'fileno' of 'file' object needs an argument
> =================================
> 
> It looks similar to the code in Sparklines except for the fact that
> the latter creates an Image from scratch.
> 
> 2B
> 
>> Saving the image, in a
>> format your client understands, to a file like object like
>> StringIO.StringIO is an easy path to take.
>>
>> Sparklines shows this in action:
>>
>> http://bitworking.org/projects/sparklines/
>>
>> max
> 
> 
If it hasn't already bitten you it will: It is a BAD idea to use
'file' as a variable name.  If you do, you mask the built-in file
function.  That is why most people use fp or something else.

-Larry



More information about the Python-list mailing list