[Image-SIG] Using PIL to display resized images over the web

Steven M. Castellotti SteveC at innocent.com
Wed Oct 8 19:58:44 EDT 2003


	Many thanks for the tip. I had never used StringIO before, so after a
little research I was able to figure out what I needed to know. For the
sake of the list archive, here's sample code for the answer to my

#!/usr/bin/env python

import Image, cStringIO

sample_image = '/archive/sample.jpg' # The physical image path

file = open(sample_image, 'r')
im = Image.open(file)

format = im.format # remember to maintain image format when saving

im = im.resize((640, 480))

file_out = cStringIO.StringIO()

im.save(file_out, format)


print "Content-Type: image/jpeg\n"
print file_out.read()



-Steve Castellotti
 SteveC (at) Innocent.com

On Thu, 2003-10-09 at 12:06, Chris Cogdon wrote:
> On Wednesday, Oct 8, 2003, at 15:49 US/Pacific, Steven M. Castellotti 
> wrote:
> > Hey all--
> >
> > 	I have a simple photo website written in python. I would like to be
> > able to use Python Imaging Library to read an image file from the disk,
> > resize/thumbnail it in memory, and then print the modified image
> > (sending it to the client web browser after the proper MIME headings).
> >
> > 	Currently, I have only managed to do this via Image.save() to a
> > temporary file and then sending that, but of course that is somewhat
> > inefficient. Surely there's an easier way to do this, perhaps via file
> > descriptors?
> the 'save' method can take a string or a file object. If you pass a 
> string it will 'open' that file and write the image to it. If you pass 
> a file object, it will simply write to that file object.
> So... if you want to save it into a string for later processing, create 
> a StringIO object and pass it to the save method.

More information about the Image-SIG mailing list