Home arrow Python arrow Imagine Python

Imagine 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



Why would anyone want to do this with a language like Python?

Fortunately this is not a question I've had to ask myself since the answer seems pretty obvious to me. But basically that's what this article is all about. Over the next few pages we'll be looking at some of the different places you could use Python to automate and generally play with your images.

You will need to have at least basic knowledge of these subjects, so if you're new to Python you should probably read Vikram Vaswani's "Python 101" first and come back later!

Essentially when you do anything to an image what you're actually doing is writing/moving bytes around. You can do this manually, but when you have an excellent third party module like PIL (Python Imaging Library), why bother?

So let's add a little extra image processing power to Python! You can get the latest version of PIL from www.pythonware.com

Windows users should download the windows installer (.exe). Those installing PIL from source should probably take a look through "Installing Python Modules" in the Python docs, if only out of interest.

On to the Fun Stuff

Ok, now that we have the "basics" out of the way, it's time for the fun stuff! Something like converting a JPEG image to a GIF maybe. In most cases all you should have to do is open the image and save it with a different file extension and PIL will do the rest!

>>> import Image
>>> image Image.open('sample.jpg')
>>> image.save('sample.gif')

If this didn't work for you, don't worry; chances are all you have to do is add another line before saving.

>>> import Image
>>> image Image.open('sample.jpg')
>>> image.convert('RGB')
>>> image.save('sample.gif')

>>> 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: