Using PIL(/other?) to display resized images over the web?

Steve Castellotti stevec at innocent.com
Wed Oct 8 20:04:41 EDT 2003


Hey all-

	Someone on the PIL mailing list suggested that I look into using StringIO
	when writing to the file. I had never used StringIO before, but after a
	little research I was able to figure out what I needed to know. For the
	sake of the newsgroup archive, here's sample code for the answer to my
	question:

#!/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)

file_out.reset()

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

file_out.close()
file.close()

Cheers

-Steve Castellotti
 SteveC (at) Innocent.com
 http://www.deltaflux.org


On Thu, 09 Oct 2003 12:03:10 +1300, Steve Castellotti wrote:

> Hey all--
> 
>         I have a simple photo website written in python. I would
> like to be able to use Python Imaging Library (or similar) to read an
> image file from the disk, resize/thumbnail it in memory, and then print
> the modified image to stdout (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?
> 
> Cheers
> 
> -Steve Castellotti
>  SteveC (at) Innocent.com
>  http://www.deltaflux.org




More information about the Python-list mailing list