473,394 Members | 1,481 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,394 software developers and data experts.

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 864

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

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
1
by: duncan.lovett | last post by:
I am working on a graphical heading generator for a clients website as their server does not have the GD library or similar plugins for dynamic image generation. I have achieved the result...
7
by: Andrew Robinson | last post by:
Given HTML text (likely from SQL), is there any method that could be employed to render server and/or custom controls that are contained inside of that text? I would be loading content from a...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
0
by: Eniac | last post by:
Hi, I've been working on a custom user control that needs to be modified and the validation is causing me headaches. The control used to generate a table of 4 rows x 7 columns to display all...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.