Python learning, which is probably the most detailed article the basic concept of PIL library

PIL has the following modules: Image Module, ImageChops module, ImageCrackCode module, ImageDraw module, ImageEnhance module, ImageFile module, ImageFileIO module, ImageFilter module, ImageFont module, ImageGrab module, ImageOps module, ImagePath module, ImageSequence module, ImageStat module, ImageTk module, ImageWin module, PSDraw module

So many modules ah ~ ~ ~! ! ! !

Do not worry, the next you explain one by one

Image Module

Image module provides a class of the same name, i.e. image class for representing an image PIL.

Image PIL module is the most important module, such as create, open, display, and other functions to save the image synthesis, clipping, filtering and other functions, function attribute acquiring an image, such as an image histogram, number of channels.

Using Image module are as follows:

ImageChops module

ImageChops module contains some graphic arithmetic operations that can be used for many purposes, such as image effects, image composition, etc. drawing algorithm, the channel is only used for 8-bit images.

Use ImageChops module are as follows:

Since the image is im_dup im, copied, so their difference is zero in FIG black, im_diff image display.

ImageCrackCode module

ImageCrackCode module allows the user to detect and measure various characteristics of the image. This module is present only in the PIL Plus package.

PIL because my current installation is not included in this module. So not described in detail

ImageDraw module

ImageDraw module provides the basic functionality for processing graphics image object. For example, it can create a new image, comments, or retouch an existing image, produce a variety of graphics for web applications in real time.

Use ImageDraw module are as follows:

Del displayed in the image before and after the draw im is exactly the same, the two diagonal lines are drawn on the original image.

Xiao Bian forgive error

ImageEnhance module

ImageEnhance modules include a class for image enhancement. They are Color class, Brightness class, Contrast and Sharpness class category.

Use ImageEnhance module are as follows:

Im0 luminance image is half of the image im.

ImageFile module

ImageFile open and save function module provides related support functions as an image. Further, it provides a Parser class that may be a piece of an image is decoded (e.g., an image receiving network connection). This interface standard classes and interfaces xmllib sgmllib same module.

Use ImageFile module are as follows:

Since the opened image size is larger than 1024 byte, it is given: The image is incomplete.

So we want to see their own can find a little bit of a look at the map

ImageFileIO module

ImageFileIO means for reading an image from a stream socket or other device. This module is deprecated. ImageFile code used in the new module will Parser class to replace it.

ImageFilter module

ImageFilter module comprises a predefined set various filters, filter for use with the method of the Image class. This module contains the image enhancement filters: BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE and SHARPEN.

Use ImageFilter module are as follows:

ImageFont module

ImageFont module defines a class of the same name, i.e. ImageFont class. Examples of this class is stored in the bitmap font, designed for use with text ImageDraw class methods.

PIL uses its own font file format for storing bitmap fonts. The user can use the kit to pilfont BDF and PCF font descriptors (Xwindow font format) is converted to this format.

PIL Plus package will support vector fonts.

ImageGrab module

ImageGrab means for copying the contents of the screen image to a PIL memory. The current version can only work on windows operating system.

Use ImageGrab module are as follows:

Im notebook image shows the contents of the current window is similar to the screenshot tool

ImageOps module

ImageOps module comprises a number of "ready-made" image processing operation. It can be done histogram equalization, cutting, quantization, mirroring operation. Most of the operations work only on L and RGB images.

Use ImageOps module are as follows:

Im im_flip image is an image in the vertical direction of the image.

ImagePath module

ImagePath means for storing and manipulating the two-dimensional vector data. The method will be passed to Path object ImageDraw module.

Use ImagePath module are as follows:

ImageSequence module

ImageSequence module includes a wrapper class that provides iterators for each frame image in the sequence.

Use ImageSequence module are as follows:

Two rear show () function call, show a first and a second image 11.

ImageStat module

ImageStat global statistics module calculates a value of an image or an image area.

Use ImageStat module are as follows:

ImageTk module

ImageTk module for creating and modifying BitmapImage and PhotoImage object Tkinter.

Use ImageTk module are as follows:

PSDraw module

PSDraw module provides basic printing support for Postscript printers. Users can print fonts, graphics and images through this module.

Basic Basic Concepts PIL

PIL basic concepts involved has the following: a channel (bands), mode (mode), dimension (size), the coordinate system (coordinate system), the palette (palette), information (info) and a filter (Filters ).

aisle

Each image is composed of one or more data channels. PIL allow synthesis of the same dimension and depth of the plurality of channels in a single image.

In an RGB image, for example, each picture is composed of three data channels, respectively, R, G and B channels. For grayscale images, only one channel.

For the channel number and name of a picture can be obtained by getbands () method. getbands () method is a method Image module, it will return a string tuple (tuple). The tuple includes the name of each channel.

Python tuple is similar to the list, except that the elements of the tuple can not be changed, tuples use parentheses, square brackets lists, tuples create very simple, only need to add elements in parentheses, and that is to use a comma can.

Use getbands () method is as follows:

mode

Mode image pixel defines the bit width and the type of image. Currently supports the following modes:

1: 1 pixel indicates black and white, but when stored for each pixel is stored as 8bit.

L: 8-bit pixel representing black and white.

P: 8-bit pixel, palette mapping to other modes.

I: 32-bit integer pixels.

F: 32-bit floating point pixels.

RGB: 3x8-bit pixels, true color.

RGBA: 4x8-bit pixels, true color with a transparent channel.

CMYK: Separation 4x8 bits pixel color.

YCbCr: 3x8-bit pixels, color video format.

PIL also supports some special modes, including RGBX (there is padding the true color) and RGBa (have premultiplied alpha true color).

Mode image can be read by the mode attribute. The return value is a character string pattern described above.

Use mode attribute are:

size

Size Image can be obtained by size property. This is a tuple comprising a number of pixels in the horizontal and vertical directions.

Use mode attribute are:

coordinate system

PIL pixel Cartesian coordinate system, coordinates (0,0) is the upper left corner. Note: represents the angular coordinate value of a pixel; center pixel at coordinates (0,0) is actually located at (0.5, 0.5).

Tuple coordinate is often used in (x, y). It said rectangular four-tuple, the coordinates of the upper left corner in front. For example: covering a 800x600 pixel image is represented as a rectangle (0,0,800,600).

Palette

Palette mode ( "P") using a specifically defined color palette of color values ​​for each pixel

information

Properties can use the info to add some additional information for the picture. This is a dictionary object. When loading and saving image files, depending on how much information needs to deal with file format.

The info attribute as follows:

filter

For the plurality of input pixels mapped to an output pixel geometry operation, PIL provides four different sampling filter:

NEAREST: Recently filtering. Selecting the nearest pixel from the input image as an output pixel. It ignores all the other pixels.

BILINEAR: bilinear filtering. Linear interpolation is performed on the 2x2 matrix of the input image. Note: The current version of the PIL, doing the down-sampling filter uses a fixed input template.

BICUBIC: bicubic filter. Cubic interpolation performed on the input image is a 4x4 matrix. Note: The current version of the PIL, doing the down-sampling filter uses a fixed input template.

ANTIALIAS: smoothing filter. This is the PIL 1.1.3 version of the new filter. High quality resampling filter for all input pixels may affect the output pixel, to calculate the output pixel value. In the current version of PIL, this filter only for changing the size of thumbnails and methods.

Note: In the current version of PIL, AntiAlias ​​filter is only correct when downsampling filter (e.g., the image is converted to a large panel). BILIEAR BICUBIC and fixed input filter template, for a fixed ratio of the geometric transformation and sampling is the best. Method a resize () and thumbnail () Image filter module uses.

Define a resize () method is: resize (size, filter = None) => image

Using a resize () method is as follows:

Parameter is not assigned, then filter, resize () method uses the default NEAREST filter. If another filter may be realized by the following method:

Thumbnail The define () method is: im.thumbnail (size, filter = None)

Use thumbnail The () method is as follows:

It is noted that the method thumbnail The () need to maintain the aspect ratio, for the size (200,200) = input parameters, the final size of thumbnails (182, 200).

Filter parameter is not assigned, then thumbnail The method () using the default NEAREST filter. If another filter may be realized by the following method:

Recommended reading:

Basics zero Python's most detailed source of tutorials

2019 Python Reptile Learning Roadmap full version

Why Python can be firmly secured the first card AI Artificial Intelligence Language

Python rise, TIOBE list of programming languages ​​a new high!

Guess you like

Origin blog.csdn.net/meiguanxi7878/article/details/93655064