473,406 Members | 2,387 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,406 software developers and data experts.

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

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
Jul 18 '05 #1
3 3244
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?

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

--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: ma**@pollenation.net

Jul 18 '05 #2
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?

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

--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: ma**@pollenation.net


Jul 18 '05 #3
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

Jul 18 '05 #4

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

Similar topics

5
by: David Roden | last post by:
Hi. I'm trying to create a stylesheet for a page that shows images instead of text links. I tried to apply the FIR method but apparently that only works for block-level elements. Used HTML: ...
0
by: Frank S. Romano | last post by:
Hello, Does anyone know how to bind the image property of a picture control to an Access 2000 database OLE object column that contains a MS Photo Editor 3.0 photo? When I try binding the picture...
1
by: Peter Oliphant | last post by:
Is there a good book /on-line tutorial /etc. on how to use images in managed C++ applications? In particular, how to access images in files such as jpg's (either at run-time or development-time)...
0
by: Frenchie | last post by:
Hi, I have created a very neet menu from an example found on the MSDN library at: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitembinding.imageurlfield.aspx My...
4
riccom
by: riccom | last post by:
Hello, I have a very big dilemma here, for me that is. I am using three images as BG's within a wrapper. See link. The top image is fine. But the center shows okay in design view. However, when I...
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...
1
by: jelumalai | last post by:
I am using display:none with using Table. When show the onClick using javascript. Then it will show, again i will hide, content only hidden, but that table doesn't hide. <script> function...
1
by: simigill | last post by:
how can i created multiple masking in flash using images
5
by: remon87 | last post by:
I need some help. I have javasript that creates the submenu but it works if I have a text with css. I need it to do the same with a roll over images. so when I click on the image the submenu...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.