Python
  Home arrow Python arrow Page 3 - 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Imagine Python - Image.Show
    (Page 3 of 4 )

    You're probably wondering what all the image.show() calls are about; show() just creates a temporary image and opens it. This way we can see what's happening to our image step by step!

    Anyway let's just slip off topic for a second here and imagine that you're in charge of some online art gallery/community where members can upload images and have them displayed for the world to see. As part of the site's design every image has a square thumbnail that links to the full-sized image.

    We can think of this as a few steps:

    1. Loading the image and passing it to our function.
    2. Cropping our image so we end up with a square.
    3. Resizing (100 x 100) and saving our thumbnail.


    #!/usr/bin/env python
     
    import Image
     
    def thumb
    (image):
     size 
    = list(image.size)
     size
    .sort()
     image 
    image.crop((00size[0], size[0]))
     image 
    image.resize((100100), Image.ANTIALIAS)
     
    return image
     
    if __name__ == '__main__':
     
     
    image Image.open('sample1.jpg')
     image 
    thumb(image)
     image
    .save('sample2.jpg')

    This really just defines a new user function named thumb() which we can then use to create our thumbnails! It starts by converting the images size to a list and sorting it in place so we have the smallest dimension at the beginning of the list (this value is then used while cropping our image). The next part then crops and resizes our image using the ANTIALIAS filter (better quality) before returning the new image.

    Watermarking

    Next up we're going to do some watermarking and paste a smaller graphic onto our image. Which as far as I know makes this the only example of watermarking with PIL, even though once again this is incredibly easy!


    #!/usr/bin/env python
     
    def watermark(imagethumb):
     imageX
    imageY image.size
     thumbX
    thumbY thumb.size
     
    if thumbX imageX and thumbY imageY:
      point1 
    imageX - (thumbX 10)
      point2 
    imageY - (thumbY 10)
      point3 
    imageX 10
      point4 
    imageY 10
      image
    .paste(thumb, (point1point2point3point4))
    if 
    __name__ == '__main__':
     
     
    import Image
     
     image 
    Image.open('sample.jpg')
     thumb 
    Image.open('thumb.jpg')
     
     
    watermark(imagethumb)
     
     
    image.show()

    Quite proud of this for some reason; maybe because I've finally got my head around placing things where I want them on an image. Anyway what it is actually saying is that if 'thumb' is smaller than our image, then put it 10 pixels in from the bottom right hand corner of 'image' (since it would look pretty messed up in most cases otherwise).

    More Python Articles
    More By Mark Lee Smith


     

       

    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 3 hosted by Hostway