Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python Image processing Library PIL

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "Python image processing library PIL how to use", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python image processing library PIL how to use" article.

1. Introduction 1. Basic introduction

Pillow is a basic image processing library in Python, which is mainly used for basic image processing, such as image cropping, image resizing and image color processing. Compared with Pillow, OpenCV and Scikit-image have more functions, so they are more complex to use. They are mainly used in machine vision, image analysis and other fields, such as the well-known "face recognition" applications.

2. Characteristics

Support a wide range of formats

Pillow supports a wide range of image formats, such as "jpeg", "png", "bmp", "gif", "ppm", "tiff" and so on. At the same time, it also supports the conversion between image formats. In short, Pillow can handle images in almost any format

Provide a wealth of functionality

Pillow provides rich image processing functions, which can be summarized into two aspects:

Image archiving, including creating thumbnails, generating preview images, image batch processing, etc., while image processing includes image resizing, image cropping, pixel processing, adding filters, image color processing, etc.

Image archiving

Image processing.

Use with GUI tool

3. Install pip install pillow guide package imoprt PIL 2, Image object 1, instantiate object 1.1 instantiate

Guide package

From PIL import Image

Use the open method

Im = PIL.Image.open (fp) # Import picture im.show () # Show picture

Fp: image path

Use the open method

Im = Image.new (mode,size,color) # create picture im.show () # Show picture

The parameters are described as follows:

Mode: image mode, string parameters, such as RGB (true color image), L (grayscale image), CMYK (color image printing mode), etc.

Size: image size. Tuple parameters (width, height) represent the pixel size of the image.

Color: picture color. The default value is 0 to indicate black. Parameter values support the triple number format, hexadecimal value of color and English words of color.

1.2 the image mode mode describes 11-bit pixels (value range 0-1), with 0 for black, 1 for white, and monochrome channel. L8-bit pixels (value range 0-255), grayscale map, monochromatic channel. P8-bit pixels, using color palettes to map to any other mode, monochrome channel. RGB3 x 8-bit pixels, true color, tricolor channel, the value range of each channel is 0-255mm. RGBA4 x 8-bit pixels, true color + transparent channel, four-color channel. CMYK4 x 8-bit pixels, four-color channel, can be adapted to print pictures. YCbCr3 x 8-bit pixels, color video format, tricolor channel. LAB3 x 8-bit pixels, L * a * b color space, tricolor channel HSV3 x 8-bit pixels, hue, saturation, value color space, tricolor channel. I 32-bit signed integer pixels, monochrome channel. F32-bit floating-point pixel, monochrome channel. 2. Object attribute import PIL.Imageim = PIL.Image.open (r "D:\ 35005\ Pictures\ Screenshots\ Wechat picture _ 20220302175157.jpg") print (im.size) # check the image size print (im.readonly) # check whether it is read-only, 1 is yes 0: no print (im.format) # View the format of the picture print (im.info) # View the related information of the picture print (im.mode) # View the mode 3 of the picture, format conversion 3.1 save method

The save method is used to save the image, which is stored in the default picture format when the file format is not specified, or in the specified format if the picture format is specified

Syntax:

Im = PIL.Image.open (r "D:\ 35005\ Pictures\ Screenshots\ Wechat picture _ 20220302175157.jpg") im.save (fp, format=None) # Save the picture

The parameters are described as follows:

Fp: the storage path of the picture, including the name of the picture and the string format

Format: optional parameter to specify the format of the image

3.2 convert method

Note that not all picture formats can be converted using the save () method. For example, saving a picture in PNG format to JPG format will cause an error if you directly use the save () method.

The error is caused by inconsistent PNG and JPG image modes. Where PNG is a four-channel RGBA mode, that is, red, green, blue, and Alpha transparent color, and JPG is a three-channel RGB mode. Therefore, in order to realize the conversion of picture format, it is necessary to change PNG into three-channel RGB mode.

The convert () method provided by the Image class can convert the image mode. This function provides several parameters, such as mode, matrix, dither, etc. The most important parameter is mode, and the rest parameters need not be concerned.

Syntax:

Im.convert (mode, params) # conversion mode im.save (fp) # Save the picture

Parameters:

Mode: refers to the image mode to be converted

Params: other optional parameters

4. Zoom the picture

In the process of image processing, it is often encountered to reduce or enlarge the image. The resize () method provided by the Image class can achieve arbitrary reduction and magnification of the image.

Syntax:

Im_new = im.resize (size, resample=image.BICUBIC, box=None, reducing_gap=None) # Note to reassign im_new.show () # zoomed image

Parameters:

Size: tuple parameter (width,height), the size of the scaled image

Resample: optional parameter, which refers to the image resampling filter, which is similar to the resample parameter of thumbnail () and defaults to Image.BICUBIC

Box: scales the specified area of the picture, and the parameter value of box is a pixel coordinate tuple of length 4, that is, (left, top, bottom right). Note that the designated area must be within the scope of the original image, and an error will be reported if it is out of range. When this parameter is not passed, the entire image is scaled by default

(0,0120,180) represents an image area whose width and height are (120180) with the upper left corner of the original image as the origin.

Reducing_gap: optional parameter, floating point parameter value, which is used to optimize the zoom effect of the image. The common parameter values are 3.0,5.0.

5. Create thumbnails

A thumbnail refers to an image that reduces the original image to a specified size (size). Create thumbnails to make images easier to display and browse

The Image object provides a thumbnail () method to generate thumbnails of images, proportional scaling.

Syntax:

Im.thumbnail (size,resample) # modify the im.show () # zoomed image directly based on the original image

Parameters:

Size: tuple parameter, which refers to the reduced image size

Resample: optional parameter, which refers to the image resampling filter. There are four filtering methods: Image.BICUBIC (double cubic interpolation), PIL.Image.NEAREST (nearest neighbor interpolation), PIL.Image.BILINEAR (bilinear interpolation) and PIL.Image.LANCZOS (downsampling filter interpolation). The default is Image.BICUBIC.

6. Image separation and merging

The image (refers to the digital image) is composed of many pixels, the pixel is the basic unit of the image, and each pixel can use a different color, and finally presents a colorful image, and the separation and merging of the image refers to the separation and merging of the image color.

6.1 split method im = PIL.Image.open (r "D:\ 35005\ Pictures\ Screenshots\ Wechat Picture _ 20220302175157.jpg") r, g, b = im.split () # split method is easy to use, separating channel r.show () g.show () b.show () 6.2 merge method

The merge () method provided by the Image class enables the merging of images. Note that image merging can be a single image merge or more than two images

Im_merge = PIL.Image.merge (mode, bands) im_merge.show ()

Parameters:

Mode: specifies the mode in which the picture is output

Bands: the parameter type is tuple or list sequence, and its element values are the color channels that make up the image. For example, RGB represents three color channels, which can be expressed as (r, g, b).

6.3 blend method

The Image class also provides a blend () method to mix pictures in RGBA mode (PNG format)

Syntax:

PIL.Image.blend (image1,image2, alpha)

Parameters:

Image1: picture object 1

Image2: picture object 2

Alpha: transparency. Values range from 0 to 1. When the value is 0, the output image is equivalent to the copy of image1, while when the value is 1, it is the copy of image2. Only when the value is 0.5, it is the integration of the two images. Therefore, the size of the change determines the degree of mixing of the two images.

7. Image processing 7.1 Image clipping

The crop () function provided by the Image class allows us to crop the original image in a rectangular region.

Syntax:

Im_crop = im.crop (box=None) # box represents the crop region im_crop.show ()

Box is a tuple parameter with four numbers (upper left, lower left, upper right, and lower right), representing the upper left x, y coordinates and lower right XMagy coordinates of the cropped rectangle, respectively. The default (zero zero) represents the origin of coordinates, the width is in the direction of x axis, the direction of height is in the direction of y axis, and each pixel represents a unit.

7.2 copy and paste

Copy and paste operations occur almost in pairs, and the Image class provides copy () and paste () methods to copy and paste images.

Copy the syntax:

Im_copy = im.copy () # copy the picture

Paste syntax:

Im_copy.paste (image, box=None, mask=None)

Parameters:

Image: refers to the pasted picture

Box: specifies the location or area where the picture is pasted. The parameter value is a tuple sequence with a length of 2 or 4. A length of 2 indicates a specific point (x, y). A length of 4 indicates the area where the picture is pasted, and the size of the area must be the same as the size of the pasted image.

Mask: optional parameter to add a mask effect to the image

Note:

The pasted picture mode will be automatically consistent and no additional conversion is required.

From PIL import Imageim = Image.open (r "D:\ 35005\ Pictures\ Screenshots\ Wechat Picture _ 20220302175157.jpg") # make a copy of the picture im _ copy = im.copy () # crop the copy im_crop = im_copy.crop ((0,0,200,100)) # create a new image as a mask, L mode Monochromatic value image_new = Image.new (200,100,200) # paste the cropped copy onto the copy image and add a mask im_copy.paste (im_crop, (100,100,300,200), mask=image_new) # to show the pasted image im_copy.show () 8, geometric changes

The geometric transformation of an image mainly includes image flipping, image rotation and image transformation operations. The Image class provides functions transpose (), rotate () and transform () to handle these operations.

8.1 transpose

This function can realize vertical and horizontal flipping of the image.

Syntax:

Im_out = im.transpose (method) # generate a new image object

Value of method:

Image.FLIP_LEFT_RIGHT: flip left and right horizontally

Image.FLIP_TOP_BOTTOM: flip vertically up and down

Image.ROTATE_90: the image is rotated 90 degrees counterclockwise

Image.ROTATE_180: the image is rotated 180 degrees

Image.ROTATE_270: image is rotated 270 degrees

Image.TRANSPOSE: image transpose

Image.TRANSVERSE: image flipping sideways

8.2 rotate

When we want to rotate the image at any angle, we can use the rotate () function

Syntax:

Im_out = im.rotate (angle, resample=PIL.Image.NEAREST, expand=None, center=None, translate=None, fillcolor=None) # returns the image object

Parameters:

Angle: indicates the angle of arbitrary rotation

Resample: resampling filter, defaults to PIL.Image.NEAREST nearest neighbor interpolation

Expand: optional parameter, indicating whether to expand the image. If the parameter value is True, the output image will be enlarged. If it is False or omitted, it will be output according to the original image size.

Center: optional parameter to specify the center of rotation. The parameter value is a tuple of length 2. It rotates at the center of the image by default.

Translate: the parameter value is a binary tuple, which means that the rotated image is translated with the upper left corner as the origin. The parameter value of translate can be negative.

Fillcolor: optional parameters, fill color, fill the area outside the image after the image is rotated

8.3 transform

This function can transform the image and generate a new image of specified size by the specified transformation mode.

Syntax:

Im_out = im.transform (size, method, data=None, resample=0) # returns the image object

Parameters:

Size: specify the size of the new picture

Method: specify how the picture changes, for example, Image.EXTENT represents a rectangular transformation

Data: this parameter is used to provide the required data for the transformation mode

Resample: image resampling filter. Default parameter value is PIL.Image.NEAREST.

III. ImageFilter1, introduction

With the continuous development of digital image technology, image denoising methods are becoming more and more mature. Constructing filters through some algorithms is the main way of image denoising. The filter can effectively suppress the generation of noise, and does not affect the shape, size and original topology of the processed image.

Pillow achieves the purpose of image denoising through the ImageFilter class, which integrates different kinds of filters and calls them to achieve image denoising operations such as image smoothing, sharpening, boundary enhancement and so on.

2. image denoising filter name description ImageFilter.BLUR fuzzy filtering, that is, mean filtering ImageFilter.CONTOUR contour filtering, finding image contour information ImageFilter.DETAIL detail filtering, making image display more refined ImageFilter.FIND_EDGES search boundary filtering (finding image boundary information) ImageFilter.EMBOSS embossed filtering Display image in relief form ImageFilter.EDGE_ENHANCE Boundary Enhancement filter ImageFilter. Edge _ ENHANCE_MORE depth Edge Enhancement filter ImageFilter.SMOOTH smooth filter ImageFilter. SMOOTH _ MORE depth smoothing filter ImageFilter.SHARPEN sharpening filter ImageFilter.GaussianBlur () Gaussian fuzzy ImageFilter.UnsharpMask () anti-sharpening mask filter ImageFilter.Kernel () convolution kernel filter ImageFilter.MinFilter (size) minimum filter, select the minimum pixel value from the area specified by the size parameter It is then stored in the output image. The ImageFilter.MedianFilter (size) median filter selects the median pixel value from the area specified by the size parameter and stores it in the output image. ImageFilter.MaxFilter (size) maximum filter ImageFilter.ModeFilter () mode filter 2.2 uses syntax

Syntax:

Im_ft = im.filter (filt_mode) # returns the image object in which the filter is passed

Example:

From PIL import Image, ImageFilterim = Image.open (r "D:\ 35005\ Pictures\ Screenshots\ Wechat picture _ 20220302175157.jpg") im_ft = im.filter (ImageFilter.EMBOSS) # add embossed filter im_ft.show ()

Equivalent to the filter added in PS

IV. ImageColor1, introduction

Pillow provides a color processing module ImageColor, which supports colors in different formats, such as color triples in RGB format, hexadecimal color names (# ff0000), and color English words ("red"). At the same time, it can also convert CSS (cascading style sheets, used to decorate web pages) style colors into RGB format.

The ImageColor module is not sensitive to the case of colors. For example, "Red" can also be written as "red".

2. Color processing 2.1 Color naming

ImageColor supports the naming of a variety of color modes (that is, the representation of faces in a fixed format), such as the well-known RGB color mode, as well as HSL (hue-saturation-lightness) and HSB (also known as HSV, hue-saturation-luminance) color mode. Here is a brief introduction to HSL:

H: Hue tone. Values range from 0 to 360. 0 means "red", 120th means "green", and 240indicates "blue".

S: Saturation saturation, which represents the purity of the color. The value is 0: 100%, where 0 represents gry, and 100% indicates the most saturated color.

L: Lightness brightness, with a value of 0,100%, where 0 indicates "black" black, 50% indicates normal color, and 100% indicates white.

Brightness and luminance are expressed in a similar way, with a specific description in the link: [https://www.zhihu.com/question/22077462]

The ImageColor module is relatively simple, providing only two common methods, namely, the getrgb () and getcolor () functions.

2.2 getrgb

Syntax:

Rgb = PIL.ImageColor.getrgb (color) # get the rgb value of the color

The color parameter can be either in English or in HSL and HSB mode 2.3

Application:

From PIL import Image, ImageColorim = Image.new (mode= "RGB", size= (100,100), color=ImageColor.getrgb ('HSL (0100% 50%)')) im.show () 2.3 getcolor

Syntax:

Val = PIL.ImageColor.getcolor (color, mode)

Parameters:

Color: a color name, string format, can be an English word for color, or a hexadecimal color name. If it is an unsupported color, a ValueError error will be reported

Mode: specifies the color mode. If it is an unsupported mode, a KeyError error will be reported.

5. ImageFont1, introduction

The ImageFont module defines a class with the same name, the ImageFont class. An instance of this class stores bitmap fonts for the text () method of the ImageDraw class

PIL uses its own font file format to store bitmap fonts. Users can use the pilfont toolkit to convert BDF and PCF font descriptors (Xwindow font format) to this format

2. Module function 2.1 load

Syntax:

Ft = PIL.ImageFont.load (font_file)

Loads a font from the specified file and returns the font object

2.2 load_path

Syntax:

Ft = PIL.ImageFont.load_path (font_file)

Same as the function load (), but if the current path is not specified, the specified font file is looked up from sys.path

2.3 truetype

Syntax:

Ft = PIL.ImageFont.truetype (file, size [, encoding=None])

Parameters:

File: load a TrueType or OpenType font file

Size: creates a font object for fonts of specified size

Encoding: font coding, the main font codes are: "unic" (Unicode), "symb" (Microsoft Symbol), "ADOB" (Adobe Standard), "ADBE" (Adobe Expert) and "armn" (Apple Roman)

2.4 load_default

Syntax:

Ft = PIL.ImageFont.load_default ()

Load a default font and return a font object

3. Module method 3.1 getsize

Syntax:

Size = ft.getsize (text)

Returns the width and height of the given text, with a return value of 2 tuples

3.2 getmask

Syntax:

Obj = ft.getmask (text,mode=None) # returns a bitmap for the given text. This bitmap is an example of PIL internal storage memory.

Parameters:

Text: the text to render.

Mode: it is used by some graphics drivers to indicate which mode the driver likes; if empty, the renderer may return to either mode. Note that the pattern is always a string

VI. ImageDraw1, introduction 1.1 Import

The ImageDraw module is also one of the main modules of the Pillow library, which can visualize arcs, draw horizontal lines, write words, etc.

Import:

From PIL import ImageDraw

Instanced the object:

From PIL import Image, ImageDrawim = Image.open (". / a.jpg") # create image object draw = ImageDraw.Draw (im) # instantiate the basic concept of Draw object 1.2 that can be drawn on a specified image

Coordinates

The drawing interface uses the same coordinate system as PIL, that is, (0J0) is the upper left corner.

Colours

To specify a color, the user can use numbers or tuples, and the corresponding user can use the function Image.new or Image.putpixel. For images with modes of "1", "L", and "I", use integers. For "RGB" images, use a triple of integers. For "F" images, use integers or floating-point numbers.

For palette images (mode "P"), use integers as the color index. After 1.1.4, users can also use RGB 3 tuples or color names. The paint layer automatically assigns a color index, as long as the user does not paint more than 256 colors.

Colours Names

A, hexadecimal color specifier defined as "# rgb" or "# rrggbb". For example, "# ff0000" stands for pure red.

B, RGB function, defined as "rgb (red, green, blue)", and the variables red, green, blue are integers between [0255]. In addition, the color value can also be three percentages between [0% gamma 100%]. For example, "rgb (255,0,0)" and "rgb (100%, 0%, 0%)" both represent pure red.

C, HSL (Hue-Saturation-Lightness) function, defined as "hsl (hue,saturation%, lightness%)", variable hue is [0360] an angle to represent color (red=0, green=120, blue=240), variable saturation is a value (gray=0%,full color=100%) between [0% gray=0%,full color=100%], variable lightness is a value between [0% hue,saturation%, lightness%] (black=0%, normal=50%, white=100%). For example, "hsl (0100%, 50%)" is pure red.

D, generic HTML color names, the ImageDraw module provides 140 standard color names, which are supported by Xwindow systems and most web browsers. Color names are not case-sensitive. For example, "red" and "Red" both stand for pure red.

In PIL 1.1.4 and later, users could use string constants when drawing "RGB" images. PIL supports the following string formats:

Fonts

PIL can use bitmap fonts or OpenType/TrueType fonts

2. Module function 2.1 arc

Syntax:

Draw.arc (xy, start, end, options)

Draw an arc between the start and end angles in a given area

Options: what can be seen in the source code

2.2 bitmap

Syntax:

Draw.bitmap (xy, bitmap, options) # options can add colors covered by fill

Draw the bitmap corresponding to the variable bitmap in the given area, and the non-zero part is filled with the value of fill in the variable options. The variable bitmap bitmap should be a valid transparent template (mode "1") or mask (mode "L" or "RGBA")

The variable xy is the coordinate value of the variable bitmap corresponding to the beginning of the bitmap, not a region

This method has the same function as Image.paste (xy, color, bitmap).

2.3 chord

Syntax:

Draw.chord (xy, start, end, options)

Same as the method arc (), but connect the starting point with a straight line

The outline of the variable options gives the color of the outline of the string; the fill gives the color of the inside of the string

2,4 ellipse

Syntax:

Draw.ellipse (xy, options)

Draw an oval in a given area

The outline of the variable options gives the color of the outline of the string; the fill gives the color of the inside of the string

2.5 line

Syntax:

Draw.line (xy, options)

Draw a line between the coordinates represented by the variable xy list

There are at least two coordinates in xy, which are represented by tuples and stored in a list [(x1, y1), (x2, y2)]

Width specifies the width, fill specifies the color of the line

2.6 pieslice

Syntax:

Draw.pieslice (xy, start, end, options)

Same as the method arc (), but draw a straight line between the end point and the center point in the specified area

2.7 point

Syntax:

Draw.point (xy, options)

Draw a dot that occupies only one pixel at a specified location

2.8 polygon

Syntax:

Draw.polygon (xy, options)

Draw a polygon

The outline of a polygon consists of a straight line between a given coordinate, and a straight line is added between the last coordinate and the first coordinate to form a polygon.

The coordinate list is made up of 2 tuples [(xPowery), …] Or the number [XBI y, …] That contains at least 3 coordinate values

The fill of the variable options the color inside a given polygon

2.9 rectangle

Syntax:

Draw.rectangle (xy, options)

Draw a long polygon

The variable xy contains 2 tuples [(xpeny), …] Or the number [XBI y, …] Any sequence object that should include 2 coordinate values

Note: when the rectangle is not filled, the second coordinate pair defines the point outside the rectangle.

The fill of the variable options gives the interior color of the long polygon

2.10 text

Syntax:

Draw.text (xy, string, options)

Draws a string at a given location. The variable xy gives the position of the upper-left corner of the text

The font of the variable option is used to specify the font used. It should be an instance of the class ImangFont that is loaded from the file using the load () method of the ImageFont module

The fill of the variable options the color of the given text

2.11 textsize

Syntax:

Draw.textsize (string, options)

Returns the size, in pixels, of a given string

The font of the variable option is used to specify the font used. It should be an instance of the class ImangFont that is loaded from the file using the load () method of the ImageFont module

7. Image and Numpyfrom PIL import Imageimport numpy as npim = Image.open (". / a.jpg") print (np.asarray (im)) # 3D array na = np.asarray (im) # convert a picture to an array na [0] [0] [0] = 0 # modify the value of the array im_new = Image.fromarray (na) # convert an array to a picture above is the content of the article "how to use PIL in Python Image processing Library" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report