[Python] PIL module

Python self-built library, easier to remember in reptiles and other basic applications, do organize to prepare for self-examination.

table of Contents

Image Module

open type, Save type, format type, Mode type, convert the class, Size class, Info classes, new classes, Copy type, Crop type, Paste type, Filter-based, Blend type, Split type, Composite class, Eval class, Merge class , Draft type, Getbands class, Getbbox class, Getdata class, Getextrema class, Histogram-based, Load class, Paste type, putdata type, Resize class, Rotate class, Seek class, Tell class, Thumbnail class, Transpose class

Image module
from PIL import Image

open类
Image.open()
Save类
im.save(outfile, format, options…)

outfile is the path

Examples: area.save ( 'test2.png', im.format)

format class
im.format ⇒ string or None
Return to image format, if the image is not read from the file its value is None.

Mode class
im.mode ⇒ string
returned image mode, there is a common mode

"L" (luminance) grayscale image represented!
"RGB" for true color images,
"CMYK" image represents publication, indicating pixel format image use.
convert Class
im.convert ( "P", ** options ) ⇒ image
available options are:

Dither =. Dithering control. The default is FLOYDSTEINBERG, errors committed together with the adjacent pixels. This feature is not enabled, then assigned to NONE.
Palette =. Palette generation control. The default is the WEB, which is the standard 216-color "web palette". To optimize the use of the palette, then assigned ADAPTIVE.
Colors =. When the palette option is ADAPTIVE, for controlling the number of color palette. The default is the maximum value, i.e., 256 colors
of the current image to another mode, and returns the new images. A Matrix actually transform their own definition, is not commonly used

Size class
im.size ⇒ (width, height)
Output: (750,500) 
outputs a tuple, it is important crawlers

Info class
im.info ⇒ dictionary
dictionary storing image-related data. Using the dictionary file handle passed various non-image information read from the file. Not intuitive, not used

new class
create a blank canvas, similar plt; a handful of non-object function

Image.new (MODE, size, Color) ⇒ Image
Mode: above, three kinds of common
Size: a given width / height tuple, which is calculated according to the number of pixels.
color: For single-channel image, only given a variable color value; For multi-channel images, given a variable color tuple (a value per channel).
Note: The two representations of color

"red"
"#FF0000"
Copy类
im.copy() ⇒ image

 When open add .copy, does not affect the original image

Crop类
m.crop(box) ⇒ image

Screenshot. Is well known, the coordinates of the upper left - lower right coordinates, so that the variable is a four-membered box int tuple

Paste type
im.paste (image, box)

Referred to as PS. Variables given a box or a top left corner of the 2-tuple, or define the left, upper, right and lower 4-tuple pixel coordinates, or is empty (the (0,0) as)

NOTE: If the pattern does not match, the pasted image is converted into a current-mode image.
 

Filter class
opencv advantage, the correlation process is not recommended PIL in filter and other basic image processing

im.filter(filter) ⇒ image

Returns a copy of the given filter processing through use of the image. In particular reference to the application ImageFilter image filtering module in the module, a number of predefined enhancement filter can be used by a filter function (), the predefined filter comprising: BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES , SMOOTH, SMOOTH_MORE, SHARPEN. Wherein the mean filter is a BLUR, CONTOUR find contour, FIND_EDGES edge detection, when using the module, the need to import.

Blend类
Image.blend(image1,image2, alpha) ⇒ image

Transparency, unimportant

Split类
im.split() ⇒ sequence

And pictures related to Mode, RGB can spilt three, L mode can not spilt

from PIL import Image
im = Image.open("E:\mywife.jpg")
r,g,b = im.split()
Composite类
Image.composite(image1,image2, mask) ⇒ image

Fusion, useless

Eval类
Image.eval(image,function) ⇒ image

Using variable function corresponding to the function (the function should have a parameter) for each pixel in the image represented by the image processing variables. If the variable image represented by the image with a plurality of channels, action functions corresponding to the function the variables that each channel. Note: For each variable pixel processing function only once, can not use random generators and other components.

from PIL import Image
im = Image.open("E:\mywife.jpg")
def fun01(x):
return x*0.3
def fun02(y):
return y*2.0
im1_eval = Image.eval(im, fun01)
im2_eval = Image.eval(im, fun02)
im1_eval.show()
im2_eval.show()
Merge类
Image.merge(mode,bands) ⇒ image

Draft类
im.draft(mode,size)

Getbands类
im.getbands()⇒ tuple of strings

Getbbox类
im.getbbox() ⇒ 4-tuple or None

Getdata类
im.getdata() ⇒ sequence

Getextrema类
im.getextrema () ⇒ 2-tuple

Getpixel类
im.getpixel(xy) ⇒ value or tuple

Histogram类
im.histogram()⇒ list

Returns a histogram of the image. The histogram is a list regarding the number of pixels, each pixel value of the image corresponding member. If the image has multiple channels, all channels will histograms connected (e.g., Histogram "RGB" image values ​​768). Binary image (mode "1") as the gray scale image (mode "L") process.

Load类
im.load()

Allocate memory for the image file and load it from (or an image from a source, for operation lazy). Under normal circumstances, users do not need to call this method, because the first time you access the images, Image class will automatically load the opened image. In later versions 1.1.6 and process load () Returns a pixel to read and modify access object pixel. The access-dimensional objects, like a queue, such as:

pix = im.load() 
print pix[x, y] 
pix[x, y] =value

Through this method than the object access getpixel () and putpixel () much faster.

 

Paste类
im.paste(image,box, mask)

Using the template image mask corresponding to the variable corresponding to the filling region. You can use pattern of "1", "L" or "RGBA" image as a template image. The same image size as the template image corresponding to the image to be variable. If the variable mask value corresponding to the image 255, the value of the template image is directly copied; mask corresponding to the variable value is 0 if the image, the original value of the holding current image. Variable mask value corresponding to another image, the image will be the value of two transparent fusion, if the image corresponding to the variable "RGBA" image, i.e., the pasted image mode "RGBA", the alpha channel is ignored. Users can use the same image as the original image and the template image.


Putdata类
im.putdata(data) 
im.putdata(data, scale, offset)

Copy the data from the object to the current image sequence, starting from the upper left corner (0,0) position of the image. Variable scale and offset adjustment values ​​in the sequence to:

pixel = value*scale + offset

If the variable scale omitted, the default is 1.0. If the variable offset omitted, the default is 0.0.

Resize类
im.resize(size) ⇒ image 
im.resize(size, filter) ⇒ image

Returns a copy of the image size change. The variable size is the desired size, is a tuple: (width, height). Variable filter is NEAREST, BILINEAR, BICUBIC one or ANTIALIAS. If omitted, or the image mode is "1" or "P", the variable is set to NEAREST. In the current version of bilinear and bicubic filters are not well adapted downsampling large proportion (e.g., to generate a thumbnail). Users need to use ANTIALIAS, unless the speed is more important than quality.

Rotate类
im.rotate(angle) ⇒ image 
im.rotate(angle,filter=NEAREST, expand=0) ⇒ image

Back according to a given angle clockwise about the copy of the image after the image rotates. Variable filter is NEAREST, BILINEAR or one BICUBIC. If the variable is omitted or an image mode is "1" or "P", the default is NEAREST. The expand variable, if true, indicates that the output image is large enough, the rotated image may be loaded. If the default is false or, as large as the output image and the input image size.


Seek类
im.seek(frame)

Find frame specified in the given file sequence. If you look beyond the end of the sequence, it generates an EOFError exception. When the file is opened sequence, PIL library automatically assigned to the 0-th frame.

Tell class

im.tell() ⇒ integer

Returns the current frame location, counting from zero.


Thumbnail类
im.thumbnail(size) 
im.thumbnail(size, filter)

Modify the current image to include a self thumbnail, the thumbnail size not larger than a given size. This method calculates an appropriate thumbnail size to meet the current aspect ratio of the image, call the method draft () reads the configuration file, and finally change the size of the image. Variable filter should be NEAREST, BILINEAR, BICUBIC or one ANTIALIAS. If you omit this variable, the default is NEAREST. Note: In the current version of the PIL, the filter bilinear and bicubic not well adapted to generate thumbnails. Users should use ANTIALIAS, best image quality. If the processing speed is more important than the image quality, other filters may be selected. This method is modified on the original. If the user does not want to edit the original, may be used a method copy () copies of an image. This method returns null.
Transform class

im.transform(size,method, data) ⇒ image 
im.transform(size, method, data, filter) ⇒ image

Use of a given size to generate a new image, the original image has the same pattern, using a given conversion method to copy the original data to the new image. In the current version of PIL, the parameter is the EXTENT method (cut out a rectangular region), AFFINE (affine transformation), QUAD (converted to a rectangular square), the MESH (action mapping a plurality of square) or PERSPECTIVE. It defines the variable filter filtering the original image pixels. In the current version, the variable filter is NEAREST, one BILINEAR, BICUBIC or ANTIALIAS. If omitted, or the image mode is "1" or "P", the variable is set to NEAREST.

im.transform(size,EXTENT, data) ⇒ image 
im.transform(size, EXTENT, data, filter) ⇒ image

Cropping an area from the image. The variable data specified in the input image coordinate points two 4-tuples (x0, y0, x1, y1). Output image pixels as a result of sampling between two coordinate points. For example, if the input image is (x0, y0) to (0,0) of the output image, (x1, y1) is the same as a variable size. This method may be used in the current cropped image, zoom or an arbitrary rectangular mirror. It crop ratio method () slower, but fast as the resize operation.

im.transform(size, AFFINE, data) ⇒ image 
im.transform(size, AFFINE,data, filter) ⇒ image

The current image affine transformation, transformation results reflected in the new image of given size. Variable data is a six-tuple (a, b, c, d, e, f), comprising a first affine transformation matrix of two rows. Each pixel (x, y), the new value of the output image (ax + by + c, dx + ey + f) is generated by the pixel position of the input image, is approximated using the nearest pixel. This method for scaling an original image, translation, rotation and cropping.

im.transform(size,QUAD, data) ⇒ image 
im.transform(size, QUAD, data, filter) ⇒ image

Enter a quadrilateral (region defined by the four corners) rectangular image is mapped to a given size. Variable data is an 8-tuples (x0, y0, x1, y1, x2, y2, x3, y3), which comprises a source of a quadrangular upper left, lower left, upper right and lower right corners.

im.transform(size,PERSPECTIVE, data) ⇒ image 
im.transform(size, PERSPECTIVE, data, filter) ⇒ image

Perspective transformation of the current image, generating a new image of given size. Variable data is an 8-tuple (a, b, c, d , e, f, g, h), including a perspective transformation coefficients. For each pixel in the output image, the new values from the input position of the image (ax + by + c) / (gx + hy + 1), (d x + ey + f) / (gx + hy + 1 ) pixel, using pixels closest approximation. This method is an original image for 2D perspective.
 
wocao !!! gakki it is gone! ! !

Transpose类
im.transpose(method)⇒ image

Returns a copy of the current image flipped or rotated. The value of the variable method: FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM, ROTATE_90, ROTATE_180 , or ROTATE_270.
---------------------
Author: YZXnuaa
Source: CSDN
Original: https: //blog.csdn.net/YZXnuaa/article/details/92802636
Disclaimer: This article as a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/valorchang/p/11303637.html