Home arrow Python arrow Page 2 - Imagine Python

Batch Processing - Python

Quite a cryptic title, but if you havenít guessed, were talking about Images. This being a Python article thatís what we're using! If youíve never thought about it, or -- even better -- if you didnít know it was possible then youíre in for a nice surprise; not only can Python do this but itís pretty good at it, too. Actually, Python works well with graphics in general, but for now weíre sticking to the 2D kind.

  1. Imagine Python
  2. Batch Processing
  3. Image.Show
  4. Lock It Down
By: Mark Lee Smith
Rating: starstarstarstarstar / 50
May 03, 2004

print this article



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
for each in os.listdir(path):
if each.endswith(format[0]):
.open(name).save(save[0] + format[1])
   except IOError
if __name__ == '__main__':

('', ('.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
>>> image.format
>>> image.mode
>>> 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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: