473,383 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

PIL: reading bytes from Image

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...

Mar 10 '07 #1
4 13647
"cyberco" <cy*****@gmail.comwrote:
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

Mar 11 '07 #2
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

Mar 11 '07 #3
"cyberco" <cy*****@gmail.comwrote:
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:
>>import Image
import StringIO
im=Image.new('RGB', (100,100))
fp=StringIO.StringIO()
im.save(fp, 'jpeg')
len(fp.getvalue())
823

Works for me on python 2.5 on windows.
max

Mar 11 '07 #4
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
Mar 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: BRAINIAC | last post by:
I am trying to process JPEG images using PIL 1.1.4 with Python 2.2.3. GIF and PNG are working fine. I am using jpeg-6b and all my other software using libjpeg work fine. I am thinking you need...
6
by: rzed | last post by:
I'm using PIL to generate some images which may be rotated at the user's option. When they are rotated, the original image is cropped in the new image (which is fine), and the corners are black...
4
by: Will McGugan | last post by:
Hi, I'm writing an app that downloads images. It rejects images that are under a certain size - whithout downloading them completely. I've implemented this using PIL, by downloading the first K...
5
by: mardif | last post by:
Hi guys, I've a problem, but very big! So, i have a python/PIL application that manipulate images ( rotate, crop, save, etc etc ). If this application work on a PC mono-processor, I don't have...
2
by: Odalrick | last post by:
I'm making a simple program to crop and scale images, essentially make thumbnails from a user defined subset of the image. I'm planning to use Python Image Library to crop and resize the images,...
2
by: Cameron Walsh | last post by:
Hi all, I'm trying to extract the data from a bitmap or .pnm file using the following code: import Image img = Image.open("test.bmp","r") data=img.getdata() Unfortunately I get the...
2
by: Craig | last post by:
Hi there, I'm trying to open colour BMPs using PIL and I'm getting the following errors. Opening a 16 colour BMP I get: Traceback (most recent call last): File "<pyshell#3>", line 1, in...
13
by: vaneric | last post by:
hi i have a set of RGB images of diff faces (of people )as a 2 dim numpyarray ...something like threefaces=array(, , ]) where xa1,xa2,xa3 are tuples each representing rgb values of a pixel...
2
by: Silfheed | last post by:
Heyas So I've been messing around with the PIL and PNG's and came across a little problem with PNG's. So just to clarify, I'm running with the standard ubuntu 8.04 python- imaging package...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.