Image Processing with QccPack and Python

 in
How to use QccPack to manipulate images with Python in code and from the Python prompt.
Essential Packages

Python, xv and the PIL package are essential for Python image processing programming. Run these commands to build PIL in Linux:

python setup.py build_ext -i
python selftest.py

Working with the Python Imaging Library

The most important class in the Python Imaging Library is the Image class, defined in the module with the same name. We create instances of this class in several ways: by loading images from files, processing other images or creating images from scratch.

To load an image from a file, use the open function in the Image module:

>>> import Image
>>> im = Image. open ("lenna.ppm")

The Python Imaging Library supports a wide variety of image file formats. The library automatically determines the format based on the contents of the file or the extension.

The next example (Listing 2) shows how the Image class contains methods to resize and rotate an image.

Color Transforms

The Python Imaging Library allows you to convert images between different pixel representations using the convert function—for example, converting between modes:

im = Image.open("lenna.ppm").convert ("L")

The library supports transformations between each supported mode and the L and RGB modes. To convert between other modes, you may have to use an intermediate image.

Filters

The ImageFilter module contains a number of predefined enhancement filters that can be used with the filter method. For example, from the Python prompt, do the following:

>>> import ImageFilter
>>> out = im.filter(ImageFilter.DETAIL)

Once you have imported the module, you can use any of these filters:

  • ImageFilter.BLUR

  • ImageFilter.CONTOUR

  • ImageFilter.DETAIL

  • ImageFilter.EDGE_ENHANCE

  • ImageFilter.EDGE_ENHANCE_MORE

  • ImageFilter.EMBOSS

  • ImageFilter.FIND_EDGES

  • ImageFilter.SMOOTH

  • ImageFilter.SMOOTH_MORE

  • ImageFilter.SHARPEN

Controlling the Decoder

Some decoders allow you to manipulate an image while reading it from a file. This often can be used to speed up decoding when creating thumbnails and printing to a monochrome laser printer. The draft method manipulates an opened but not yet loaded image so it matches the given mode and size as closely as possible. Reconfiguring the image decoder does this. See Listing 3 for an example of how to read an image in draft mode.

Listing 4 shows how the ImageDraw module provides basic graphics support for Image objects.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

image processing with scripting language

image processing's picture

Python Imaging Library have great features and your blog is like a teacher where every information is very vital and informative for me.thanks a lot providing me such kind of information.

Regards,
image processing

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Upcoming Webinar
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
11am CDT, April 29th
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot