Python
  Home arrow Python arrow Page 4 - Imagine Python
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Imagine Python
By: Mark Lee Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 50
    2004-05-03

    Table of Contents:
  • Imagine Python
  • Batch Processing
  • Image.Show
  • Lock It Down

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Imagine Python - Lock It Down
    (Page 4 of 4 )

    Last but not least: You may or may not have noticed those funky little images some websites use to stop automated signups and etc. No? Never mind if you haven't because that's what we're about to do.


    #!/usr/bin/env python
     
    import ImageImageDrawImageFontrandom
     
    def sample
    ():
     ascii 
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
     
    return ''.join(random.sample(ascii5))
     
    def verify():
     image 
    Image.new('RGB', (12540), (255255255))
     font 
    ImageFont.truetype('Verdana.ttf'25)
     draw 
    ImageDraw.Draw(image)
     draw
    .text((55), sample(), font fontfill = (000))
     image
    .save('verify.gif')
     
    if 
    __name__ == '__main__'verify()

     

    There are a few differences between this example and the others we've talked about so far. Primarily the three modules ImageDraw, ImageFont and random and the two functions sample() and verify().

    Why have two functions? We could have just as easily done this inside verify couldn't we?

    The thing is, sooner or later everyone ends up rewriting something they wrote for some other project. One of the nice things about using Python is it encourages you to write clean, reusable, modular code. Which is of course the main reason for the sample() function; since it just returns a string of random letters you could also use it to automate password generation!

    Short and sweet, all sample() does is pick five letters at random from the string 'ascii' using random.sample() and returns them as a string.

    verify() on the other hand has a little more going for it. This one unlike any of the other examples we've looked at starts off by creating a brand spanking new image instead of opening one! We're going to write our random letters onto this in a second, but first we need to load the font we want to use, for this we're using the ImageFont module. We then create a new instance of ImageDraw.Draw() which is used to draw the sample() string on the image!

    Anyway it's pretty safe to assume you have a good idea why this could be useful by now. You should also know enough to get started using PIL with your own images. Slap open up your Python shell and get playing!

    Liked this article? Then you'll probably find these links interesting too!

    Python homepage
    Python tutorial
    Installing Python modules
    PIL homepage
    PIL tutorial
    PIL documentation

    Note: All the sample programs shown and discussed in this article where tested on Windows XP running Python 2.3 with PIL 1.4 and are meant only as examples.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway