469,923 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

Dynamic generation of images (was Re: Custom Classes?)

On Thu, 2008-05-08 at 10:33 -0500, Victor Subervi wrote:
Okay, trying this again with everything working and no ValueError or
any other errors, here we go:

Load this code. Unless you use a similar login() script, you will want
to edit your own values into the user, passwd, db and host:

#!/usr/bin/python

import MySQLdb

import sys,os
sys.path.append(os.getcwd())
from login import login
user, passwd, db, host = login()

pic = "pic1"
w = 20
x = 0
d = 6
y = 1

getpic = "getpic" + str(w) + ".py"
try:
os.remove(getpic)
except:
pass
code = """
#!/usr/local/bin/python
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append(os.getcwd())
from login import login
user, passwd, db, host = login()
form = cgi.FieldStorage()
picid = int(form["id"].value)
x = int(form["x"].value)
pics =
{1:'pic1',2:'pic1_thumb',3:'pic2',4:'pic2_thumb',5 :'pic3',6:'pic3_thumb',7:'pic4',8:'pic4_thumb',\
9:'pic5',10:'pic5_thumb',11:'pic6',12:'pic6_thumb' }
pic = pics[x]
db = MySQLdb.connect(host, user, passwd, db)
cursor= db.cursor()
sql = "select %s from products where id='%s';" % (pic, str(picid))

cursor.execute(sql)
content = cursor.fetchall()[0][0].tostring()
cursor.close()
print 'Content-Type: image/jpeg'
print
print content
"""
script = open(getpic, "w")
script.write(code)
script.close()

and then surf to:
http://whatever.url/getpic20.py?id=6&x=1
Also, please re-send the link on how to post good questions to the
list. I cannot find it.
TIA,
Victor
Why are you dynamically creating getpic20.py?

Obviously, in the real script, you probably have several of them:
getpic1.py, getpic2.py, etc., but is there any reason you couldn't just
have one getpic.py, and pass the number in as a parameter in your GET
request, like this:

http://localhost/getpic.py?id=6&x=1&w=20

Then you can just have a static getpic.py. I've stripped out useless
bits:

* You don't use sys or os, so why import them?
* MySQL makes it difficult for me to replicate your behavior, because I
don't have your DB setup. The DB call has been replaced with a
dictionary of keys into JPG data, pulled from the filesystem. Change
filenames to jpegs on your own hard drive
* login is useless without MySQL.

So your new getpic.py (statically created) looks like this:

~~~ getpic.py ~~~
#!/usr/local/bin/python

import cgitb; cgitb.enable()
import cgi

pics = {
1: open('pic.jpg').read(),
2: open('pic2.jpg').read()
}
# you could just pass the filename, and then you wouldn't have to load
the
# image data in advance, but this more closely mimics the concept of
your DB
# setup.

form = cgi.FieldStorage()
x = int(form["x"].value)
pic = pics[x]

print 'Content-Type: image/jpeg'
print
print pic
~~~

Then if you want to include your pictures in a web page, you do

~~~ show_pics.html ~~~
<html>
<head><title>Pictures</title></head>
<body>
<h1>pictures</h1>
<img src='http://example.com/getpic.py?x=1'/>
<img src='http://example.com/getpic.py?x=2'/>
</body>
</html>
~~~

This should work just as well when you refactor the script to pull from
the database.

Hope this helps. Also, the article on asking good questions is
available here: http://catb.org/~esr/faqs/smart-questions.html

Jun 27 '08 #1
0 745

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

51 posts views Thread by Mudge | last post: by
1 post views Thread by duncan.lovett | last post: by
7 posts views Thread by Andrew Robinson | last post: by
3 posts views Thread by NateDawg | last post: by
reply views Thread by Eniac | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.