473,544 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic image creation for the web...

Hi,

I would like to create images on the fly as a response to an http request.
I can do this with PIL like this (file create_gif.py):
from PIL import Image, ImageDraw

print 'Status: 200 OK'
print 'Content-type: text/html'
print
print '<HTML><HEAD><T ITLE>Python Dynamic Image Creation Test</TITLE></HEAD>'
print '<BODY>'
im = Image.new("P", (600, 400))
draw = ImageDraw.Draw( im)
draw.rectangle( (0, 0) + im.size, fill="blue")
im.save("images/tmp.gif");
print '<img src="/scripts/images/tmp.gif">'
print '</BODY>'
However, I would like to 1) avoid saving the image in a file on disk and
2) separate the HTLM code from the python image creation code.

Something like this is what I have in mind:
(file index.html):
<html>
<head><meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
<title>Python Dynamic Image Creation</title>
</head>
<IMG SRC="/scripts/create_image.py " ALT="Image created on the fly...">
</html>

and in file create_image.py :
from PIL import Image, ImageDraw, ImageFont
im = Image.new("P", (600, 400))
draw = ImageDraw.Draw( im)
draw.rectangle( (0, 0) + im.size, fill="blue")
Unfortunately this does not work :-(
What is missing?

Thanks in advance!
/Tompa
Aug 28 '05 #1
5 4374
Tompa wrote:
Hi,

I would like to create images on the fly as a response to an http request.
I can do this with PIL like this (file create_gif.py):
from PIL import Image, ImageDraw

print 'Status: 200 OK'
print 'Content-type: text/html'
print
print '<HTML><HEAD><T ITLE>Python Dynamic Image Creation
Test</TITLE></HEAD>' print '<BODY>'
im = Image.new("P", (600, 400))
draw = ImageDraw.Draw( im)
draw.rectangle( (0, 0) + im.size, fill="blue")
im.save("images/tmp.gif");
print '<img src="/scripts/images/tmp.gif">'
print '</BODY>'
However, I would like to 1) avoid saving the image in a file on disk and
2) separate the HTLM code from the python image creation code.

Something like this is what I have in mind:
(file index.html):
<html>
<head><meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
<title>Python Dynamic Image Creation</title>
</head>
<IMG SRC="/scripts/create_image.py " ALT="Image created on the fly...">
</html>

and in file create_image.py :
from PIL import Image, ImageDraw, ImageFont
im = Image.new("P", (600, 400))
draw = ImageDraw.Draw( im)
draw.rectangle( (0, 0) + im.size, fill="blue")
Unfortunately this does not work :-(
What is missing?


You are almost there. Your create_image.py does not return anything to the
browser yet.

First return proper HTTP headers, e.g.

sys.stdout.writ e('Status: 200 OK\r\n')
sys.stdout.writ e('Content-type: image/gif\r\n')
sys.stdout.writ e('\r\n')

(Your prints above are mostly equivalent, but do not output the correct \r\n
as line terminator - at least on UNIX style systems. Most webservers
tolarate this, if it's coming from a CGI - but doing it right and not
relying on a certain server behaviour is not bad anyway ;)

Then check the PIL docs to find out, how to output the image to sys.stdout
(instead of writing to a file).

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Aug 28 '05 #2
Tompa <to*******@yaho o.com> wrote in
news:ma******** *************** *************@p ython.org:
Hi,

I would like to create images on the fly as a response to an http
request. I can do this with PIL like this (file create_gif.py):
from PIL import Image, ImageDraw


check out sparklines:

http://bitworking.org/projects/sparklines/

It is a script very similar to what you want to do.

The difference between your script and sparklines is mostly that it
sends:

print "Content-type: image/png"

instead of:

print 'Content-type: text/html'
max
Aug 28 '05 #3
Benjamin Niemann <pink <at> odahoda.de> writes:
You are almost there. I don't feel so...
Your create_image.py does not return anything to the
browser yet. Yes, I am aware of that but I do not what to return.
First return proper HTTP headers, e.g.

sys.stdout.writ e('Status: 200 OK\r\n')
sys.stdout.writ e('Content-type: image/gif\r\n')
sys.stdout.writ e('\r\n')
Ok, but if possible I'd rather not return anything HTTP/HTML-related from my
create_image.py file.
Then check the PIL docs to find out, how to output the image to sys.stdout
(instead of writing to a file).

Ok, then I get this:

from PIL import Image, ImageDraw
import sys

im = Image.new("P", (600, 400))
draw = ImageDraw.Draw( im)
draw.rectangle( (0, 0) + im.size, fill="blue")

sys.stdout.writ e('Status: 200 OK\r\n')
sys.stdout.writ e('Content-type: image/gif\r\n')
sys.stdout.writ e('\r\n')

im.save(sys.std out, "GIF")

But this does not work.
I also tested to skip the HTTP-header stuff and just write the gif to
sys.stdout, believing that that would work. But not so...

Hmm, I'm a newbie to Python (as you already probably have noticed ;-) so I
don't know what else I should try. Any hints are welcome!

/Tompa
Aug 28 '05 #4
Max Erickson <maxerickson <at> gmail.com> writes:

check out sparklines:

http://bitworking.org/projects/sparklines/

It is a script very similar to what you want to do.


This sure looks interesting! Strange that I couldn't find this when I googled
for this kind of stuff...
I will check it out - thanks!

Regards,
/Tompa
Aug 28 '05 #5
Tompa wrote:
Benjamin Niemann <pink <at> odahoda.de> writes:
You are almost there. I don't feel so...
Your create_image.py does not return anything to the
browser yet.

Yes, I am aware of that but I do not what to return.
First return proper HTTP headers, e.g.

sys.stdout.writ e('Status: 200 OK\r\n')
sys.stdout.writ e('Content-type: image/gif\r\n')
sys.stdout.writ e('\r\n')


Ok, but if possible I'd rather not return anything HTTP/HTML-related from
my create_image.py file.


When the browser fetches the images for displaying, it performs just another
HTTP request, and you must reply with a valid HTTP response. The
Content-type header is the absolute minimum that must always be returned.
(IIRC the 'Status' can be omitted, if it's 200).
Then check the PIL docs to find out, how to output the image to
sys.stdout (instead of writing to a file).

Ok, then I get this:

from PIL import Image, ImageDraw
import sys

im = Image.new("P", (600, 400))
draw = ImageDraw.Draw( im)
draw.rectangle( (0, 0) + im.size, fill="blue")

sys.stdout.writ e('Status: 200 OK\r\n')
sys.stdout.writ e('Content-type: image/gif\r\n')
sys.stdout.writ e('\r\n')

im.save(sys.std out, "GIF")

But this does not work.
I also tested to skip the HTTP-header stuff and just write the gif to
sys.stdout, believing that that would work. But not so...


Works perfectly here...
What does the error.log of the webserver say?
Hmm, I'm a newbie to Python (as you already probably have noticed ;-) so I
don't know what else I should try. Any hints are welcome!

/Tompa


--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Aug 28 '05 #6

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

Similar topics

5
2516
by: dickseacup | last post by:
Is there a way to generate a dynamic image, passing an array (of arrays) to the image creation script, such that it displays along with text on a php generated HTML page? I either get header errors (can't modify headers already sent), or I get the php script passing 'Array' to the image script instead of the actual array variable/object. ...
2
9154
by: macbul | last post by:
(C++ Builder 6.0 enterprise) I tried to create a TImage component by using this source code: // ---
2
1346
by: ng5000 | last post by:
Hi, I'm new to ASP.Net (first looked at it 2 days ago). All I want to do is create a user's web page that displays data from a database in the form of a bespoke display (e.g picture of a computer with a temp guage on the monitor). I've got lots of ADO.Net experience so I was able to create the database side very quickly. I now have a...
2
5003
by: jmgopi | last post by:
Hi: Can somebody provide me samples on how to create a dynamic CollapsiblePanel using ASP.NET AJAX Toolkit. Any points are highly appreciated. Thanks, GJM
4
2034
by: vamshiv | last post by:
hi...im working on asp.net with VB i have created a database which hav fields id,name,image path in web form i hav written code for creation of image buttons dynamically i hav connected the form to DB and got the table into DataSet and also i hav placed one panel which contains image and label controls for dynamic image buttons i hav...
7
2362
by: dino d. | last post by:
Hi- I want to create a dynamic image with areas so that when the user clicks different areas, the user jumps to those pages. The problem is, I can't seem to figure out how to do this efficiently. Suppose I have a table,items in a database: itemid description count So, basically, I want to create an image that has 3 ovals,...
0
755
by: rbkreddy | last post by:
hello, i have created dynamic report using c#. in the header section i am trying to place a dynamic image. here where i am exprience a problem for the first page i am getting the image but for the next page onwards i am getting the error icon so can anyone help me in solving this problem. if i staticaly give the image i am getting it for all...
4
1516
by: Jonathan Wood | last post by:
I've figured out how to create an image on the fly on my Website. Once created, I write the image to the page using code like this: Bitmap bm = Response.ContentType = "image/jpeg"; bm.Save(Response.OutputStream, ImageFormat.Jpeg); The code I have does indeed display the graphic, but the page shows nothing else. I need to write the...
1
3381
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. The problem is when it creates transparent PNG format image then and it pixel ate the image. e.g. If i am using a gradient in background then it...
0
7452
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...
0
7387
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7798
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...
0
7738
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...
0
5956
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4938
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...
0
3436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1862
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
1
1004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.