Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Random and Dynamic Images     using Python CGI                  Steven Kryskalla               September 14 2005          ...
Overview•   Intro•   Simple Python CGI•   Random Image Script•   Python’s Graphics Libraries (PIL)•   Dynamic Image Script...
Intro• CGI Programming with Python• Python Graphics Libraries (PIL)• Goals:  – Create a random image script  – Create a dy...
Simple Python CGI• Like any other language• Generate content from scripts• Python’s built in CGI test server:  python -c "...
Simple Python CGI • Set the Content-type • Pipe image to browser:if __name__ == "__main__":   print "Content-type: image/p...
Simple Python CGI• What if we are serving multiple file types?• Lookup the file’s extension to get the content-  type     ...
Random Image Script• We can serve up an image file• We can find the content type of any image• Pick a random image  – os.l...
Random Image Scriptfrom os import listdirfrom random import choiceext2conttype = {"jpg": "image/jpeg",                "jpe...
Random Image Script• Not that useful…• Improvements, etc.  – Serve multiple directories of images  – Randomly serve other ...
Python’s Graphics Libraries• Many to choose from:  – PIL  – Agg  – Gnuplot  – ImageMagick  –…• PIL• Is there more interest...
PIL• www.pythonware.com/products/pil• Docs available on website• First get acquainted with basic PIL  functionality
PIL• Image• Image objects handle input, output, and  information about PIL images• Opening an image:  – Image.open(filenam...
PIL• ImageDraw• Create a draw object:  – draw = ImageDraw.Draw(img)• Draw object provides simple 2d graphics  – draw.line(...
PIL• Create a dynamic image  script• No CGI yet• Creates a random gradient
PILimport Image,ImageDrawfrom random import randint as rintdef randgradient():   img = Image.new("RGB", (300,300), "#FFFFF...
Dynamic Image Script• Now add CGI• Don’t write to a file• Use cStringIO for a file-like object
import Image,ImageDraw+ import cStringIO  from random import randint as rint    def randgradient():       img = Image.new(...
Slightly More Advanced CGI• More useful  applications of  dynamic images:  – CAPTCHAs  – Resize images for    web photo ga...
Slightly More Advanced CGI• Dynamically graph log files• Accept arguments through the query string  (from forms, user inpu...
Slightly More Advanced CGI• Read values out of the FieldStorage• If a value is not present we can return an  error message...
Slightly More Advanced CGI• The script is now usable with forms• Simple form:   <html>   <head><title>Graph Log File</titl...
Conclusion• Python’s features make it a great CGI  language  – Web frameworks extend its usefulness even    more• Solid im...
Questions?
Thank you :]      Now..A small surprise..
A logo
Upcoming SlideShare
Loading in …5
×

Random And Dynamic Images Using Python Cgi

43,481 views

Published on

  • Be the first to comment

Random And Dynamic Images Using Python Cgi

  1. 1. Random and Dynamic Images using Python CGI Steven Kryskalla September 14 2005 skryskalla@gmail.com http://lost-theory.org
  2. 2. Overview• Intro• Simple Python CGI• Random Image Script• Python’s Graphics Libraries (PIL)• Dynamic Image Script• Slightly more advanced Python CGI• A small surprise…
  3. 3. Intro• CGI Programming with Python• Python Graphics Libraries (PIL)• Goals: – Create a random image script – Create a dynamic image script – Learn about Python’s features as a web scripting language – Learn a little about Python’s graphics capabilities
  4. 4. Simple Python CGI• Like any other language• Generate content from scripts• Python’s built in CGI test server: python -c "import CGIHTTPServer;CGIHTTPServer.test()"• First goal: write a random image script• First step: redirecting a static image to the browser – Duplicate the behavior of a normal server
  5. 5. Simple Python CGI • Set the Content-type • Pipe image to browser:if __name__ == "__main__": print "Content-type: image/pngn" print file(r"c:pythonrandom_img1.png", "rb").read()
  6. 6. Simple Python CGI• What if we are serving multiple file types?• Lookup the file’s extension to get the content- type .png .png .jpg .jpeg .gifext2conttype = {"jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png", "gif": "image/gif"}def content_type(filename): return ext2conttype[filename[filename.rfind(".")+1:].lower()]if __name__ == "__main__": imgpath = r“c:pythonrandom_img3.jpg" print "Content-type: %sn“ % (content_type(imgpath)) print file(imgpath, "rb").read()
  7. 7. Random Image Script• We can serve up an image file• We can find the content type of any image• Pick a random image – os.listdir() – Filter out the non-images – random.choice() on the remaining images
  8. 8. Random Image Scriptfrom os import listdirfrom random import choiceext2conttype = {"jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png", "gif": "image/gif"}def content_type(filename): return ext2conttype[filename[filename.rfind(".")+1:].lower()]def isimage(filename): """true if filenames extension is in content-type lookup""" filename = filename.lower() return filename[filename.rfind(".")+1:] in ext2conttypedef random_file(dir): """returns the filename of a randomly chosen image in dir""" images = [f for f in listdir(dir) if isimage(f)] return choice(images)if __name__ == "__main__": dir = "c:pythonrandom_img“ r = random_file(dir) print "Content-type: %sn" % (content_type(r)) print file(dir+r, "rb").read()
  9. 9. Random Image Script• Not that useful…• Improvements, etc. – Serve multiple directories of images – Randomly serve other kinds of files – Use as a starting point for a dynamic image script
  10. 10. Python’s Graphics Libraries• Many to choose from: – PIL – Agg – Gnuplot – ImageMagick –…• PIL• Is there more interest in this?
  11. 11. PIL• www.pythonware.com/products/pil• Docs available on website• First get acquainted with basic PIL functionality
  12. 12. PIL• Image• Image objects handle input, output, and information about PIL images• Opening an image: – Image.open(filename) – Image.open(“test.png”)• Create a new image: – Image.new(mode, size, color) – Image.new(“RGB”, (300,300), fill=“#000”)• Save an image – Img.save(filename, format) – Img.save(“out.png”, “PNG”)
  13. 13. PIL• ImageDraw• Create a draw object: – draw = ImageDraw.Draw(img)• Draw object provides simple 2d graphics – draw.line(xy, options) – draw.line((0,0,300,300), fill=“#00F”) – draw.text(xy, str, options) – draw.text((50,50), “Hello World”, fill=(50,50,150))
  14. 14. PIL• Create a dynamic image script• No CGI yet• Creates a random gradient
  15. 15. PILimport Image,ImageDrawfrom random import randint as rintdef randgradient(): img = Image.new("RGB", (300,300), "#FFFFFF") draw = ImageDraw.Draw(img) r,g,b = rint(0,255), rint(0,255), rint(0,255) dr = (rint(0,255) - r)/300. dg = (rint(0,255) - g)/300. db = (rint(0,255) - b)/300. for i in range(300): r,g,b = r+dr, g+dg, b+db draw.line((i,0,i,300), fill=(int(r),int(g),int(b))) img.save("img.png", "PNG")if __name__ == "__main__": randgradient()
  16. 16. Dynamic Image Script• Now add CGI• Don’t write to a file• Use cStringIO for a file-like object
  17. 17. import Image,ImageDraw+ import cStringIO from random import randint as rint def randgradient(): img = Image.new("RGB", (300,300), "#FFFFFF") draw = ImageDraw.Draw(img) r,g,b = rint(0,255), rint(0,255), rint(0,255) dr = (rint(0,255) - r)/300. dg = (rint(0,255) - g)/300. db = (rint(0,255) - b)/300. for i in range(300): r,g,b = r+dr, g+dg, b+db draw.line((i,0,i,300), fill=(int(r),int(g),int(b)))+ f = cStringIO.StringIO()+ img.save(f, "PNG") print "Content-type: image/pngn"+ f.seek(0) print f.read() if __name__ == "__main__": randgradient()
  18. 18. Slightly More Advanced CGI• More useful applications of dynamic images: – CAPTCHAs – Resize images for web photo gallery – Sparklines – Dynamic graphs of log files
  19. 19. Slightly More Advanced CGI• Dynamically graph log files• Accept arguments through the query string (from forms, user input, etc.)• cgi module – cgi.FieldStorage()
  20. 20. Slightly More Advanced CGI• Read values out of the FieldStorage• If a value is not present we can return an error message or use a default valueif __name__ == "__main__": form = cgi.FieldStorage() if "filename" in form: if "color" in form: graph(form["filename"].value, form["color"].value) else: graph(form["filename"].value, "55d") else: print "Content-type: text/htmln" print """<html><body>No input file given</body></html>"""
  21. 21. Slightly More Advanced CGI• The script is now usable with forms• Simple form: <html> <head><title>Graph Log File</title></head> <body> <br> <form action="/cgi-bin/dynimg3.py"> <input maxlength="100" size="55" name="filename" value=""> <select name="color"> <option value="FF3333">Red <option value="33FF33">Green <option value="3333FF">Blue <option value="33AAAA">Teal </select> <input type="submit" value="Graph!"> </form> </body> </html>
  22. 22. Conclusion• Python’s features make it a great CGI language – Web frameworks extend its usefulness even more• Solid image & graphics libraries allow for quick writing of useful visual output
  23. 23. Questions?
  24. 24. Thank you :] Now..A small surprise..
  25. 25. A logo

×