Python:Image module

From AIMSWiki

This page is under construction

Table of contents


import Image

Creating a new image

new(mode, size, color = 0)
  • mode is one of Image.MODES, ['1', 'CMYK', 'F', 'I', 'L', 'P', 'RGB', 'RGBA', 'RGBX', 'YCbCr']:
    • 1: Black and white (1-bit, stored as 8-bit pixels)
    • CMYK: Cyan-Magenta-Yellow-Black model
    • F: (32-bit floating point pixels)
    • I: (32-bit signed integer pixels)
    • L: (8-bit pixels, black and white)
    • P: (8-bit pixels, mapped to any other mode using a colour palette)
    • RGB: Red-Green-Blue model
    • RGBA: Red-Green-Blue-Alpha model (alpha is for transparency)
    • RGBX: (true colour with padding)
    • YCbCr: (3x8-bit pixels, colour video format)
  • size is a length 2 integer list specifying the x and y dimensions of the image.
  • color is the colour with which to initialize the image--the background colour.

Example:

im = Image.new('RGB', [100, 200])

This creates a 100×200 red-green-blue image with a black (the default colour) background.

Displaying an image

im.show(title = None, command = None)

This might not always work: help says "for debug purposes only"

  • title is the title of the window in which the image is displayed.
  • command is the name of the program which should display the image.

Example:

im.show(title = 'My Picture', command = 'xli')

Editing an image

getpixel(self, xy)
putpixel(self, xy, value)

Saving and loading

save(self, fp, format=None, **params)
fp = file('test.jpg', 'w')   # Open the file for writing
im.save(fp, [])
fp.close()

The format is determined from the supplied filename.

open(fp, mode='r')
Open an image file, without loading the raster data
load(self)
Explicitly load pixel data.

Return to the Python index