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

    Dell PowerEdge Servers

    Imagine Python - Batch Processing
    (Page 2 of 4 )



    Who ever said programming had to be hard work? With a little help from the OS module we can turn this into a nice piece of batch processing!


    #!/usr/bin/env python 

     
    import osImage
     
    def convert
    (pathformat):
     
    for each in os.listdir(path):
      
    if each.endswith(format[0]):
       
    try:
        name 
    os.path.join(patheach)
        save 
    os.path.splitext(name)
        Image
    .open(name).save(save[0] + format[1])
       except IOError
    None
     
    if __name__ == '__main__':
     

     convert
    ('', ('.jpg''.gif'))

    Ok, possibly a little scary at first glance but all this actually does is read a list of names from a given directory and loops over them performing some action. convert() just checks if 'each' ends with the extension we want before joining 'path' and 'each' together and splitting the extension from end. It then attempts to convert the image using PIL.

    And no, you're not limited to converting images between formats, or PIL would be pretty useless wouldn't it! Actually one of the things I like most about PIL is that it hides a lot of the complexities that pop up when you're working with images. That said let's have a look at some of the other things we can do!


    >>> import Image
    >>> image Image.open('sample.jpg')
    >>> image.size
    (200200)
    >>> image.format
    'JPEG'
    >>> image.mode
    'RGB'
    >>> image.show()
    >>> image.resize((100100))
    >>> image.show()
    >>> image.crop((005050))
    >>> image.show()
    >>> image.rotate(180)
    >>> image.show()
    >>>


    This shows off some nice features i.e. retrieving the image size, actual format, resizing, etc. The next part (after image.mode) does the actual work: resizing, cropping and rotating the image.

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