PIL module __python of Python image processing

Source: Internet
Author: User
Tags glob string format

Original address: http://www.cnblogs.com/way_testlife/archive/2011/04/20/2022997.html

--------------

Image module in PIL

This article is an excerpt from PIL Handbook online and has done some simple translations

can only ensure that they understand, do not guarantee the quality of translation. You are welcome to give your opinion.

------------------------------------------------------

The image module provides a class with the same name (image), as well as some factory functions, including loading a picture from a file and creating a new picture. For example, the following script first loads a picture, rotates it 45 degrees, and displays it:

1 >>> from PIL import Image 2 >>> im = Image.open ("j.jpg") 3 >>> im.rotate (a). Show ()

The following script creates a thumbnail of all the. jpg-ending pictures in the current directory.

1 from PIL import Image 2 import glob, OS 3 4 size = 128, 128 5 to infile in Glob.glob ("*.jpg"): 6 file, ext = O S.path.splitext (infile) 7 im = Image.open (infile) 8 im.thumbnail (Size, Image.antialias) 9 im.save (file + ". Thumbnail", "JPEG")

the function in the Image class .

0. NEW: This function creates a picture of the given mode and size (size). If you omit the color argument, the picture you create is filled with black, and if the color parameter is none, the picture is not initialized.

1 image.new (mode, size) => image 2 Image.new (mode, size, color) => image

1. Open: Opens and identifies the provided image file. However, when you use this function, the real image data is not read before you do the data processing. You can use the LOAD function to force load. The mode argument can be omitted, but it can only be an "R" value.

1 Image.open (infile) => image 2 Image.open (infile, mode) => image

2. Blend: Create a new picture with two images given and a constant alpha. The two pictures must be the same size and mode.

1 image.blend (Image1, Image2, Alpha) => Image 2 # Results and Operations 3 # out = Image1 * (1.0-alpha) + Image2 * Alpha

3. Composite: Use two images given and a mask parameter similar to the Alpha parameter, the value can be: "1", "L", "RGBA". The size of both pictures must be the same.

1 Image.composite (Image1, Image2, mask) => Image

4. Eval: Use a function with one parameter for each pixel of a given picture. If a given picture has more than one band (band), the function also acts on each band. Note that the function is computed once per pixel, so you cannot use some random components or other generators.

1 image.eval (image, function) => Image

5. Frombuffer: (newly added in PIL 1.1.4) use standard "raw" decoders to create an image copy in pixel data or object cache. Not all patterns support this usage. The supported mode has "L", "RGBX", "RGBA", "CMYK".

Image.frombuffer (mode, size, data) => image

6. FromString: Note that this function decodes only pixel data, not an entire picture. If you have an entire picture in a string format, wrap it with Stringio and load it with the open function.

1 # Use string-type pixel data and standard decoder "raw" to create images 2 image.fromstring (mode, size, data) => Image 3 4 # ditto. However, you are allowed to use other PIL supported pixel decoders. 5 image.fromstring (mode, size, data, decoder, parameters) => Image

7. Merge: Create a new image using a series of single band (band) images. A band is a tuple or list of images, all band must have the same size.

1 image.merge (mode, bands) => Image

methods in the Image class :

0. Convert: Returns a copy of the converted image.

1 # If mode is omitted, a ' mode is ' chosed so ' all information in the ' image and the palette can be representedwithout a Palette. 2 # When from a colour image to black and white, the library uses the Itu-r 601-2 luma transfrom:3 # L = R * 299/1000 + G * 587/1000 + B * 114/1000 4 im.convert (mode) => Image 5 6 # Converts an ' RGB ' image to ' L ' or ' RGB ' using a conv Ersion Matrix. The matrix is 4-or 16-tuple. 7 Im.convert (mode, matrix) => image

Here is an example: convert RGB to XYZ.

1 rgb2xyz = (2 0.412453, 0.357580, 0.180423, 0, 3 0.212671, 0.715160, 0.072169, 0, 4 0.019334, 0.119193, 0.950227 , 0) 5 out = Im.convert ("RGB", rgb2xyz)

1. Copy: Copies the image. You can use this method if you want to paste something into the image, but still keep the original image.

1 im.copy () => image

2. Crop:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.