Skip to content

Latest commit

 

History

History
198 lines (144 loc) · 6.36 KB

Image.rst

File metadata and controls

198 lines (144 loc) · 6.36 KB

:pyImage Module

The :py~PIL.Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.

Examples

The following script loads an image, rotates it 45 degrees, and displays it using an external viewer (usually xv on Unix, and the paint program on Windows).

Open, rotate, and display an image (using the default viewer)

from PIL import Image
im = Image.open("bride.jpg")
im.rotate(45).show()

The following script creates nice 128x128 thumbnails of all JPEG images in the current directory.

Create thumbnails

from PIL import Image
import glob, os

size = 128, 128

for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    im.thumbnail(size)
    im.save(file + ".thumbnail", "JPEG")

Functions

open

Warning

To protect against potential DOS attacks caused by "decompression bombs" (i.e. malicious files which decompress into a huge amount of data and are designed to crash or cause disruption by using up a lot of memory), Pillow will issue a DecompressionBombWarning if the image is over a certain limit. If desired, the warning can be turned into an error with warnings.simplefilter('error', Image.DecompressionBombWarning) or suppressed entirely with warnings.simplefilter('ignore', Image.DecompressionBombWarning). See also the logging documentation to have warnings output to the logging facility instead of stderr.

Image processing

alpha_composite

blend

composite

eval

merge

Constructing images

new

fromarray

frombytes

fromstring

frombuffer

Registering plugins

Note

These functions are for use by plugin authors. Application authors can ignore them.

register_open

register_mime

register_save

register_extension

The Image Class

PIL.Image.Image

An instance of the :py~PIL.Image.Image class has the following methods. Unless otherwise stated, all methods return a new instance of the :py~PIL.Image.Image class, holding the resulting image.

PIL.Image.Image.convert

The following example converts an RGB image (linearly calibrated according to ITU-R 709, using the D65 luminant) to the CIE XYZ color space:

rgb2xyz = (
    0.412453, 0.357580, 0.180423, 0,
    0.212671, 0.715160, 0.072169, 0,
    0.019334, 0.119193, 0.950227, 0 )
out = im.convert("RGB", rgb2xyz)

PIL.Image.Image.copy

PIL.Image.Image.crop

PIL.Image.Image.draft

PIL.Image.Image.filter

PIL.Image.Image.getbands

PIL.Image.Image.getbbox

PIL.Image.Image.getcolors

PIL.Image.Image.getdata

PIL.Image.Image.getextrema

PIL.Image.Image.getpalette

PIL.Image.Image.getpixel

PIL.Image.Image.histogram

PIL.Image.Image.offset

PIL.Image.Image.paste

PIL.Image.Image.point

PIL.Image.Image.putalpha

PIL.Image.Image.putdata

PIL.Image.Image.putpalette

PIL.Image.Image.putpixel

PIL.Image.Image.quantize

PIL.Image.Image.resize

PIL.Image.Image.rotate

PIL.Image.Image.save

PIL.Image.Image.seek

PIL.Image.Image.show

PIL.Image.Image.split

PIL.Image.Image.tell

PIL.Image.Image.thumbnail

PIL.Image.Image.tobitmap

PIL.Image.Image.tobytes

PIL.Image.Image.tostring

PIL.Image.Image.transform

PIL.Image.Image.transpose

PIL.Image.Image.verify

PIL.Image.Image.fromstring

PIL.Image.Image.load

PIL.Image.Image.close

Attributes

Instances of the :pyImage class have the following attributes: