PIL: reading bytes from Image

C

cyberco

I'm using web.py to send an image to the client. This works
(shortened):

print open(path, "rb").read()

but this doesn't:

img = Image.open(path)
img.thumbnail((10,10))
print img.getdata()

or

print img.load()


How do I get the bytes of the Image object? 'getdata()' seemed the
way, but unfortunately...
 
M

Max Erickson

cyberco said:
I'm using web.py to send an image to the client. This works
(shortened):

print open(path, "rb").read()

but this doesn't:

img = Image.open(path)
img.thumbnail((10,10))
print img.getdata()

or

print img.load()


How do I get the bytes of the Image object? 'getdata()' seemed the
way, but unfortunately...

getdata() returns the data in a PIL format. 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
 
C

cyberco

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
 
M

Max Erickson

cyberco said:
Thanks,

I've tried the StringIO option as follows:

=================================
img = Image.open('/some/path/img.jpg')
img.thumbnail((640,480))
file = StringIO, StringIO()

Is the above line exactly what you tried? If it is, the comma and
space in the line are likely the problem. Try either:

from StringIO import StringIO
file=StringIO()

or

import StringIO
file=StringIO.StringIO()

(using file as a variable name can cause issues if you later want
access to the built in object 'file')

The following:
823

Works for me on python 2.5 on windows.


max
 
L

Larry Bates

cyberco said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top