473,800 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3268
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.std out, 'GIF')

Cheers, Matt

--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: ma**@pollenatio n.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.std out, 'GIF')

Cheers, Matt

--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: ma**@pollenatio n.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_ima ge, 'r')

im = Image.open(file )

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

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

file_out = cStringIO.Strin gIO()

im.save(file_ou t, 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
1789
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: <a href="link.html"><div class="image"><span>text</span></div></a>
0
1867
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 control image with the advanced binding panel, my dataset shows the table with the column but it will not set. I read thru the MS Knowledge Base and the closest solution I could find was article 317701. The example code reads an image "from...
1
1032
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) for use with Button/PictureBox/ImageList etc. The ability to get info on such images (e.g., width, height, etc.) and how to use color with them (such as the use of palettes)... Thanx! : )
0
1840
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 ASP:MENU uses images to create the buttons. Unfortunately, I can't get the buttons to link to pages. Also I can't remove the "sitemap" from the left of my menu.
4
1555
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 test, the content moves behind the center image. Also, my text is blacked out. This started happening recently. Here it is in a nut shell. Box-1 contains images, text and box-2. This is repeated three levels vertically, with different images of...
13
4908
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 of first image ..
1
1933
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 tableHide() { var tableID = document.getElementById("TableID"); if (tableID.style.display=="none") { tableID.style.display="block"; } else {
1
2084
by: simigill | last post by:
how can i created multiple masking in flash using images
5
4120
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 (images) shows with roll overs as well. That's what I have... <script type="text/JavaScript"> <!-- function MM_swapImgRestore() { //v3.0 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a)&&x.oSrc;i++) x.src=x.oSrc; }
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10276
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10035
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.