Home arrow Python arrow Page 3 - Imagine Python

Image.Show - 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



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
= list(image.size)
image.crop((00size[0], size[0]))
image.resize((100100), Image.ANTIALIAS)
return image
if __name__ == '__main__':
image Image.open('sample1.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.


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):
imageY image.size
thumbY thumb.size
if thumbX imageX and thumbY imageY:
imageX - (thumbX 10)
imageY - (thumbY 10)
imageX 10
imageY 10
.paste(thumb, (point1point2point3point4))
__name__ == '__main__':
import Image

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

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: