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

  • Thread starter Steve Castellotti
  • Start date
S

Steve Castellotti

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
 
M

Matt Goodall

Steve said:
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?
The outfile argument to Image.save() can be a string (filename) or a
file object. You can send the image to the browser by saving the
thumbnail to sys.stdout instead of the temporary file name, i.e.

print 'Content-Type: image/gif'
print
im.save(sys.stdout, 'GIF')

Cheers, Matt
 
M

Matt Goodall

Steve said:
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?
The outfile argument to Image.save() can be a string (filename) or a
file object. You can send the image to the browser by saving the
thumbnail to sys.stdout instead of the temporary file name, i.e.

print 'Content-Type: image/gif'
print
im.save(sys.stdout, 'GIF')

Cheers, Matt
 
S

Steve Castellotti

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top