Python Image Library Module



Image module

Image module in Python Image Library is the fundamental and most important module which is used for creating the module from scratch or loading the image that already exists.

Various operations can be performed which Image module like cropping, copying, creating, converting the format, rotating, resizing, color transformation.

If the file cannot be opened, an IOError exception is raised.

Importing Image module

from PIL import Image


Image module functions
function name Description
Image.new(mode, size, color) open function loads the existing image and makes the image ready for further processing
Image.blend(image1, image2, alpha) Blend function creates new image by combing two images. Alpha can be any value in between 0.0 to 1.0.
Image.composite(image1, image2, mask) Creates a composite image from two equal-sized images i1 and i2, where mask is a mask image with mode "1", "L", or "RGBA" of the same size.
Image.eval(image, function) Applies the function (which should take one argument) to each pixel in the given image.
Image.frombuffer(mode, size, data) Creates an image memory from pixel data in a string or buffer object, using the standard raw decoder.
Image.fromstring(mode, size, data) Creates an image memory from pixel data in a string, using the standard raw decoder.
Image.merge(mode, bands) Creates a new image from a number of single band images.

Methods

Below table contains complete list of methods of Image class that are used for various purposes.
All the methods return the modified form of instance of Image class.

Method name Syntax
convert im.convert(mode, matrix))
copy im.copy()
crop im.crop(box)
draft im.draft(mode, size)
filter im.filter(filter)
fromstring im.fromstring(data, decoder, parameters)
getbands im.getbands()
getbbox im.getbbox()
getcolors im.getcolors()
getdata im.getdata()
getextrema im.getextrema()
getpixel im.getpixel(xy)
histogram im.histogram()
load im.load()
offset im.offset(xoffset, yoffset)
paste im.paste(image, box)
point im.point(table)
im.point(table, mode)
im.point(function, mode)
putalpha im.putalpha(band)
putdata im.putdata(data)
im.putdata(data, scale, offset)
putpalette im.putpalette(sequence)
putpixel im.putpixel(xy, colour)
quantize
resize im.resize(size)
im.resize(size, filter)
save im.save(outfile)
im.save(outfile, options)
im.save(outfile, format, options)
seek im.seek(frame)
show im.show()
split im.split()
tell im.tell()
thumbnail im.thumbnail(size)
im.thumbnail(size, filter)
tobitmap im.tobitmap()
tostring im.tostring()
im.tostring(encoder, parameters)
transform im.transform(size, method, data)
im.transform(size, method, data, filter)
im.transform(size, EXTENT, data)
im.transform(size, EXTENT, data, filter)
im.transform(size, AFFINE, data)
im.transform(size, AFFINE, data, filter)
im.transform(size, QUAD, data)
im.transform(size, QUAD, data, filter)
transpose im.transpose(method)
verify im.verify()

* im - im is the Image object that is supposed to be created before using any of the above method.
Advertisements