[Python] Getting started with the Pillow library

1 Introduction to Pillow

1.1 PIL and Pillow

PIL (Python Imaging Library) is a third-party image processing library for Python. It is highly praised for its rich functions and simple and easy-to-use API.

Since 2011, due to slow updates of the PIL library, it currently only supports the Python 2.7 version, which obviously cannot meet the needs of the Python3 version. So a group of volunteers from the Python community (main contributor: Alex Clark and Contributors) developed an image processing library that supports the Python3 version based on the PIL library, which is Pillow.

Pillow is not only a "copy" of the PIL library, but it also adds many new features based on the PIL library. Pillow has developed to this day and has become a more dynamic image processing library than PIL.

The original intention of Pillow was just to be a branch and supplement of the PIL library. Now it is "better than the blue".

In addition to the PIL and Pillow libraries, Python also provides some other image processing libraries:

  • Scikit-image: an image processing software package based on scipy scientific computing, which processes images in the form of arrays;
  • OpenCV: It is actually a C++ image processing library, but it provides a Python language interface.

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

Pillow library (sometimes also called PIL library) is the basic library for Python image processing. It is a free and open source third-party library , Pillow provides very powerful image processing functions, and it can easily complete some image processing tasks. Compared with other image processing libraries in Python (OpenCV, Scikit-image, etc.), the Pillow library is simple and easy to use, making it very suitable for beginners to learn.

The Pillow library provides a very rich set of functions, mainly including the following:

  • The Pillow library can easily read and save images in various formats;
  • The Pillow library provides a simple and easy-to-use API interface that allows you to easily complete many image processing tasks;
  • The Pillow library can be used with the GUI (graphical user interface) package Tkinter;
  • Image objects in the Pillow library can be converted to and from NumPy ndarray arrays.

The realization of rich functions benefits from the numerous modules provided by Pillow. There are more than twenty modules in the Pillow library, such as Image image processing module, ImageFont text adding module, ImageColor color processing module, ImageDraw drawing module, etc. Each module implements different functions, and at the same time, the modules can interact with each other. Cooperate. (Refer to Python Pillow official documentation:Pillow (PIL Fork) 10.1.0.dev0 documentation)

 1.2 Pillow version support

 Pillow supports cross-platform operation, such as Windows, Linux, MacOS, etc. Its latest version is Pillow 8.3.2, which supports Python 3.6 and above (recommended). A comparison table of supported versions of Pillow and Python is as follows:

Python version 3.10 3.9 3.8 3.7 3.6 3.5 2.7
Pillow>=8.3.2 support support support support support
Pillow8.0-8.3.1 support support support support
Pillow7.0-7.2 support support support support
Pillow6.2.1-6.22 support support support support support
Pillow6..0-6.2.0 support support support support

1.3 Pillow library features

As a commonly used library for image processing, the Pillow library mainly has the following three characteristics:

1) Supports a wide range of file formats

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

2) Provides rich functions

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

  • image archive
  • Image Processing

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

3) Use with GUI tools

The Pillow library can be used with the Python GUI (graphical user interface) tool Tkinter.

In addition to the above features, the Pillow library can also implement some more complex image processing operations, such as adding watermarks to images, synthesizing GIF dynamic renderings, etc.


2 Pillow download and installation

Pillow installation is very simple and convenient. You can install it through the Python package manager pip. This method is suitable for any platform. Just enter and execute the following command in the command line window:

pip install pillow

Note: The PIL library and the Pillow library are not allowed to coexist in the same environment. If you have installed the PIL library before, please uninstall it and then install Pillow.

 Finally, open the Python interpreter interactive environment on the CMD command line and enter the following code to verify whether Pillow is installed successfully.

# 导入Image类,该类是pillow中用于图像处理的重要类
from PIL import Image

As shown below, if the interpreter does not return an error, it proves that the installation has been successful.

 Note: PIL is used here to import, but the Pillow library is actually used. PIL here can be regarded as the abbreviation of Pillow library.


3 Pillow Image object 

3.1 Create Image object

The Image class is the most important class in the Pillow library. This class is defined in the Image module with the same name.

Use the following package import method to introduce the Image module:

from PIL import Image

3.2 Instantiate Image object

You can use the Image class to instantiate an Image object and process the image by calling a series of properties and methods of the object. Pilow provides two methods for creating Image instance objects. They are briefly introduced below.

1)open() 

 Using the open() method of the Image class, you can create an Image object. The syntax format is as follows:

image = Image.open(fp,mode="r")

Parameter Description:

  • fp: the abbreviation of filepath, indicating file path and string format;
  • mode: optional parameter. If this parameter appears, it must be set to "r", otherwise a ValueError exception will be raised.

Examples are as follows: 

# 导包
from PIL import Image
# 打开一个图片文件
image = Image.open("C:/Users/Lee/Desktop/美女01.jpg")
# 调用 show()方法,显示图像
image.show()

The image display results are as follows:

2)new()

 You can create a new Image object using the new() method provided by the Image class. The syntax format is as follows:

image = Image.new(mode, size, color)

 The parameter description is as follows:

  • mode: image mode, string parameters, such as RGB (true color image), L (grayscale image), CMYK (color map 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, which means black. The parameter value supports (R, G, B) triplet number format, the hexadecimal value of the color, and the English word of the color.

Examples are as follows:

image = Image.new(mode='RGB', size=(300, 200), color="green")
image.show()

 The output image looks like this:

 

 3.3 Image object properties

 The Image object has some commonly used basic attributes. These attributes can help us understand the basic information of the image. Here is a brief introduction to these attributes:

1) size: View the size of the image
from PIL import Image

image = Image.open("D:/users/Desktop/美女01.jpg")
# 打印image对象
print(image)
# 通过size查看
print("图像的大小size:", image.size)
# 或者直接查看宽、高
print("宽是%s高是%s" % (image.width, image.height))

Output result: 

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=192x120 at 0x26B75C87310>
图像的大小size: (192, 120)
宽是192高是120
 2) format: View the format of the image
from PIL import Image

image = Image.open("D:/users/Desktop/美女01.jpg")
print("图像的格式:", image.format)

Output result:

图像的格式: JPEG
3) readonly: whether the image is read-only
from PIL import Image

image = Image.open("D:/users/Desktop/美女01.jpg")
print("图像是否为只读:", image.readonly)

 The return value of this attribute is 0 or 1, corresponding to yes and no respectively. The output result is as follows:

图像是否为只读: 1
 4) info: View image related information
from PIL import Image

image = Image.open("D:/users/Desktop/美女01.jpg")
# 包括了每英寸像素点大小和截图软件信息
print("图像信息:", image.info)

The return value of this attribute is in dictionary format, and the output result is as follows:

 Image information: {'jfif': 257, 'jfif_version': (1, 1), 'dpi': (96, 96), 'jfif_unit& #39;: 1, 'jfif_density': (96, 96)}

 5) mode: image mode
from PIL import Image

image = Image.open("D:/users/Desktop/美女01.jpg")
print("图像模式信息:", image.mode)

 Output result:

图像模式信息: RGB

 The above involves the names of many picture modes, such as RGB, RGBA, etc. The following is a brief summary of commonly used picture modes, as shown in the following table:

Picture mode
mode describe
1 1-bit pixel (value range 0-1), 0 represents black, 1 represents white, monochrome channel.
L 8-bit pixel (value range 0 -255), grayscale image, monochrome channel.
P 8-bit pixels, using palette mapping to any other mode, monochrome channel.
RGB 3 x 8-bit pixels, true color, three color channels, each channel has a value range of 0-255.
RGBA 4 x 8-bit pixels, true color + transparency channel, four color channels.
CMYK 4 x 8-bit pixels, four color channels, suitable for printing pictures.
YCbCr 3 x 8-bit pixels, color video format, three color channels.
LAB 3 x 8-bit pixels, L*a*b color space, three color channels
HSV 3 x 8-bit pixels, hue, saturation, value color space, three color channels.
I 32-bit signed integer pixel, monochrome channel.
F 32-bit floating point pixels, monochrome channel.

4 Pillow image format conversion

 The Pillow library supports multiple image formats. You can directly use the open() method to read images without considering the type of image. At the same time, Pillow can easily convert between image formats.

There are two main methods for converting between image formats, which are introduced below:

 4.1 save()

 As the name suggests, the save() method is used to save images. When the file format is not specified, it will be stored in the default image format; if the image format is specified, the image will be stored in the specified format. The syntax format of save() is as follows:

Image.save(fp, format=None)

 The parameter description is as follows:

  • fp: The storage path of the picture, including the name of the picture and string format;
  • format: optional parameter, you can specify the format of the image.

Examples are as follows:

from PIL import Image

image = Image.open("D:/users/Desktop/美女01.jpg")
image.save('D:/users/Desktop/美女02.bmp')
image.save('D:/users/Desktop/美女03.png')

 At this time, there will be two more pictures in the path location "D:/users/Desktop/" specified by the computer: a picture in the format of "Beauty 02.bmp" and a picture in the format of "Beauty 03.png".

4.2 convert()+save()

Attention! Not all image formats can be converted using the save() method. For example, if you save a PNG format image into a JPG format, if you use the save() method directly, the following error will occur: a>

from PIL import Image

image = Image.open("D:/users/Desktop/城市01.png")
image.save('D:/users/Desktop/城市02.jpg')

The error message is as follows:

# 系统错误,RGBA不能作为JPEG图片的模式
OSError: cannot write mode RGBA as JPEG

The error caused by is caused by the inconsistency between PNG and JPG image modes. Among them, PNG is a four-channel RGBA mode, that is, red, green, blue, and Alpha transparent colors; JPG is a three-channel RGB mode. Therefore, in order to convert image formats, PNG must be converted into three-channel RGB mode.

The convert() method provided by the Image class can realize image mode conversion. This function provides multiple parameters, such as mode, matrix, dither, etc. The most critical parameter is mode, and the other parameters do not need to be concerned. The syntax format is as follows:

convert(mode,parms**)

Parameter description is as follows:

  • mode: refers to the image mode to be converted to;
  • params: other optional parameters.

The modified code looks like this:

from PIL import Image

image = Image.open("D:/users/Desktop/城市01.png")
# 此时返回一个新的image_1对象,转换图片模式
image_1 = image.convert('RGB')
# 调用save()保存
image_1.save('D:/users/Desktop/城市02.jpg')

Through the above code, you can successfully convert images in PNG format to JPG format.


5 Pillow image scaling operation

5.1 Free scaling

In the process of image processing, we often encounter the situation of reducing or enlarging the image. The resize() method provided by the Image class can achieve arbitrary reduction and enlargement of the image.

resize() The syntax format of the function is as follows:

resize(size, resample=image.BICUBIC, box=None, reducing_gap=None)

Parameter Description:

  • size: tuple parameter (width, height), the size of the image after scaling;
  • resample: Optional parameter, refers to the image resampling filter, similar to the resample parameter of thumbnail(), the default is Image.BICUBIC;
  • box: Scale the specified image area. The parameter value of box is a pixel coordinate tuple with a length of 4, that is, (left, top, right, bottom). Note that the specified area must be within the range of the original image. If it exceeds the range, an error will be reported. When this parameter is not passed, the entire original image will be scaled by default;
  • reducing_gap: Optional parameter, floating point parameter value, used to optimize the zoom effect of the image. Common parameter values ​​are 3.0 and 5.0.

Note: resize() will return a new image object. Here is a set of examples of zooming in on images:

from PIL import Image

image = Image.open("D:/users/Desktop/美女03.png")
try:
    # 放大图片
    image_1 = image.resize((576, 360))
    # 将新图像保存至桌面
    image_1.save("D:/users/Desktop/美女03放大图像.png")
    print("新图像的尺寸是:", image_1.size)
except IOError:
    print("放大图像失败!")

Output result:

新图像的尺寸是:(576, 360)

The enlarged picture effect. As follows:

 Zoom in on a part of the picture. Examples are as follows:

from PIL import Image

image = Image.open("D:/users/Desktop/美女03.png")
try:
    # 选择放大的局部位置,并选择图片重采样方式
    # box四元组指的是像素坐标 (左,上,右,下)
    # (0,0,120,180),表示以原图的左上角为原点,选择宽和高分别是(192,120)的图像区域
    image_1 = image.resize((576, 360), resample=Image.LANCZOS, box=(0, 0, 96, 60))
    image_1.show()
    # 将局部放大的新图像保存至桌面
    image_1.save("D:/users/Desktop/美女03局部放大图像.png")
    print("新图像的尺寸是:", image_1.size)
except IOError:
    print("放大图像失败!")

Output result:

新图像的尺寸是: (576, 360)

The image enlargement effect is as follows:

 5.2 Create thumbnails

Thumbnail image refers to an image that reduces the original image to a specified size (size). Make your images easier to display and browse by creating thumbnails. The Image object provides a thumbnail() method for generating thumbnails of images.

thumbnail() The syntax format of the function is as follows:

thumbnail(size,resample)

The parameter description is as follows: 

  • size: tuple parameter, which refers to the reduced image size;
  • resample: Optional parameter, refers to the image resampling filter. There are four filtering methods, namely Image.BICUBIC (bicubic interpolation method), PIL.Image.NEAREST (nearest neighbor interpolation method), PIL.Image.BILINEAR (bicubic interpolation method). Linear interpolation method), PIL.Image.LANCZOS (downsampling filter interpolation method), the default is Image.BICUBIC.

Usage examples are as follows:

from PIL import Image

image = Image.open("D:/users/Desktop/美女03.png")
image.thumbnail((96, 60))
print("缩略图尺寸:", image.size)
# 将缩略图保存至桌面
image.save("D:/users/Desktop/美女03缩略图.png")

Output result: 

缩略图尺寸: (96, 60)

Note: The size of the thumbnail may not be consistent with the size you specify. This is because Pillow will reduce the length and width of the original image in equal proportions. When the specified size does not meet the size specifications of the image, the thumbnail creation will fail. For example, the specified size exceeds the size specification of the original image.

5.3 Modify image size in batches

In the image processing process, for some links that do not require detailed processing, we often use batch processing methods, such as batch conversion of formats, batch modification of size, batch addition of watermarks, batch creation of thumbnails, etc. This is a An effective way to improve work efficiency, it avoids single and repeated operations. Image sizes can be modified in batches through the Image.resize() method provided by Pillow. Let’s look at a set of simple examples below.

First find some pictures of the same type but different sizes and put them into the OldImage folder on the desktop. As shown below:

Let’s start writing code:

from PIL import Image
import os

# 指定 存放批量处理后的图片目录,如果目录不存在,则创建目录
if not os.path.exists('D:/users/Desktop/NewImage/'):
    os.mkdir('D:/users/Desktop/NewImage/')
# 设定统一的目标尺寸
width = 320
height = 320
# 读取待处理图片的目录
fileName = os.listdir('D:/users/Desktop/OldImage/')
print(fileName)
# 循环读取每一张图片
for img in fileName:
    old_image = Image.open('D:/users/Desktop/OldImage/' + img)
    new_image = old_image.resize((width, height), Image.BILINEAR)
    print(new_image)
    # 保存新图片到指定目录
    new_image.save('D:/users/Desktop/NewImage/' + img)

The output is as follows:

['98.jpg', 'hao.jpg', '古典美女.jpg', '小龙女.jpg', '旅行01.jpg', '画像.jpg']
<PIL.Image.Image image mode=RGB size=320x320 at 0x156D8807FD0>
<PIL.Image.Image image mode=RGB size=320x320 at 0x156D8807280>
<PIL.Image.Image image mode=RGB size=320x320 at 0x156D8807FA0>
<PIL.Image.Image image mode=RGB size=320x320 at 0x156D8807F40>
<PIL.Image.Image image mode=RGB size=320x320 at 0x156D8807F70>
<PIL.Image.Image image mode=RGB size=320x320 at 0x156D8807280>

The contents of the NewImage directory are as follows:

 You can see that the images in the folder have been scaled to a uniform width and height.


6 Pillow image separation and merging

We know that images (referring to digital images) are composed of many pixels. Pixels are the basic units of images, and each pixel can use different colors, ultimately presenting a colorful image. In the previous section "Pillow Image Object Properties", we introduced some picture modes. Their essence is the rules that need to be followed when pictures present colors, such as RGB, RGBA, CYMK, etc. The separation and merging of images refer to Separation and merging of image colors.

The Image class provides methods for splitting and merging images, the split() and merge() methods. Normally, these two methods are used together.

6.1 split()

split() is relatively simple to use, and is used toseparate color channels. We use it to process the following picture of a beautiful woman:

 Write the code as follows:

from PIL import Image

image = Image.open("D:/users/Desktop/美女3号.jpg")
# 修改图像大小,以适应图像处理
image_1 = image.resize((960, 600))
image_1.save("D:/users/Desktop/美女3号_1.jpg")
# 分离颜色通道,产生三个 Image对象
r, g, b = image_1.split()
r.show()
g.show()
b.show()

 The output results are shown in sequence as follows:

6.2 merge() 

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

The syntax format of the merge() method is as follows:

Image.merge(mode, bands)

Parameter description is as follows:

  • mode: Specify the mode of output images
  • bands: The parameter type is a tuple or a list sequence, and its element value is the color channel that makes up the image. For example, RGB represents three color channels, which can be expressed as (r, g, b).

Note: This function will return a new Image object.

The two types of image merging are introduced below:

1) The merging of a single image refers to the recombination of color channels to obtain different The same picture effect, the code is as follows:

from PIL import Image

image = Image.open("D:/users/Desktop/美女3号.jpg")
# 修改图像大小,以适应图像处理
image_1 = image.resize((960, 600))
image_1.save("D:/users/Desktop/美女3号_1.jpg")
# 分离颜色通道,产生三个 Image对象
r, g, b = image_1.split()
# 重新组合颜色通道,返回新的Image对象
image_merge = Image.merge('RGB', (g, b, r))
image_merge.show()
# 保存新图像至桌面
image_merge.save("D:/users/Desktop/美女3号_2.jpg")

The newly synthesized image looks like this:

 2) The merging operation of two pictures is not complicated, but the mode and image size of the two pictures must be consistent, otherwise they cannot be merged. Therefore, preprocessing is required for images with different modes and sizes.

Next, we merge the beauty number 3 above with another beauty number 5. The original picture of beauty number 5 is as follows:

 Write the code as follows:

from PIL import Image

# 打开图1和图2
image_1 = Image.open("D:/users/Desktop/美女3号.jpg")
image_2 = Image.open("D:/users/Desktop/美女5号.jpg")
# 因为两种图片的图片格式一致,所以仅需要处理图片的大小,让它们保持一致
# 让 image_2 的图像尺寸与 image_1 一致,注意此处新生成了 Image 对象
image_2n = image_2.resize(image_1.size)
# 接下来,对图像进行颜色分离操作
r1, g1, b1 = image_1.split()
r2, g2, b2 = image_2n.split()
# 合并图像
image_3 = Image.merge('RGB', [r2, g1, b2])
image_3.show()
image_3.save("D:/users/Desktop/合成美女35号.jpg")

Image synthesis result:

 6.3 blend() blends images

The Image class also provides the blend() method to blend RGBA mode images (PNG format). The syntax of the function is as follows:

Image.blend(image1,image2, alpha)

Parameter description is as follows:

  • image1, image2: Represents two Image objects.
  • alpha: represents transparency, the value range is 0 to 1. When the value is 0, the output image is equivalent to a copy of image1, and when the value is 1, it is a copy of image2. Only when the value is 0.5, the output image is equivalent to a copy of image1. is the union of the two images. The size of this value therefore determines how blended the two images are.

Compared with RGB mode, RGBA adds transparency to RGB, and determines the degree of blending of the two images through the Alpha value. Examples are as follows:

from PIL import Image

# 打开图片1
image_1 = Image.open("D:/users/Desktop/美女3号.png")
# 打开图片2
image_2 = Image.open("D:/users/Desktop/美女5号.png")
# 让图片2的尺寸和图片1保持一致
image_2n = image_2.resize(image_1.size)
# 设置 混合值alpha 为 0.5
Image.blend(image_1, image_2n, 0.6).save("D:/users/Desktop/混合美女53号.png")

The output is as follows:


7 Pillow image cropping, copying and pasting operations

Cropping, copying, and pasting of images are basic operations often used in image processing. The Pillow Image class provides a simple and easy-to-use API interface that can help you quickly implement these simple image processing operations.

7.1 Image cropping operation

The crop() function provided by the Image class allows us to crop the original image in a rectangular area. The syntax format of the function is as follows:

crop(box=None)

Parameter Description:

  • box: represents the cropping area, the default is None, which means copying the original image.

Note: box is a tuple parameter with four numbers (x_upper left, y_lower left, x1_upper right, y1_lower right), which respectively represent the x and y coordinates of the upper left corner of the cropped rectangular area. and the x, y coordinates of the lower right corner. The default (0,0) represents the coordinate origin, the width direction is the x-axis, the height direction is the y-axis, and each pixel represents a unit.

The crop() function will return an Image object. The usage example is as follows:

from PIL import Image

image = Image.open("./image/美女3号.jpg")
box = (0, 0, 960, 600)
im_crop = image.crop(box)
im_crop.show()
im_crop.save("./image/美女3号裁剪.jpg")

The output image is shown below:

Finally, an image with a pixel size of 960 * 600 is cropped based on the original image.

7.2 Image copying and pasting

Copy and paste operations almost appear in pairs. The Image class provides the copy() and paste() methods to copy and paste images. Among them, the copy operation (i.e. copy() method) is relatively simple. The following mainly introduces the paste() paste method. The syntax format is as follows:

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

The function of this function is to paste one picture into another picture. Note that the pasted image mode will automatically remain consistent and no additional conversion is required.

Parameter description is as follows:

  • image: refers to the picture being pasted;
  • box: specifies the position or area where the picture is pasted. Its parameter value is a tuple sequence of length 2 or 4. When the length is 2, it represents a specific point (x, y) and the starting coordinate of the pasting; the length is 4 It indicates the area where the image is pasted. At this time, the size of the area must be consistent with the size of the pasted image, otherwise an error will be reported.
  • mask: optional parameter, adds a mask effect to the image.

Next, copy a copy of the original image, crop and paste the copy, the code is as follows:

from PIL import Image

image = Image.open("./image/美女3号.jpg")
# 复制一张图片副本
image_copy = image.copy()
# 对副本进行裁剪
im_crop = image_copy.crop((480, 300, 1440, 900))
# im_crop.show()
# 创建一个新的图像作为蒙版,L模式,大小为(960, 600),单颜色值
image_new = Image.new('L', (960, 600), 100)
# image_new.show()
# 将裁剪后的副本粘贴至副本图像上,并添加蒙版
image_copy.paste(im_crop, (200, 100, 1160, 700), mask=image_new)
# image_copy.paste(im_crop, (200, 100), mask=image_new)  这样写也可以,就不用考虑大小匹配问题,指定好左上角要放置的坐标点
# 显示粘贴后的图像
image_copy.show()
# 保存粘贴后的图像
image_copy.save("./image/美女3号裁剪粘贴.jpg")

 The output display results are as follows:

 

 


8 Pillow image geometric transformation

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

8.1 transpose() flip operation

This function can realize vertical and horizontal flipping of images. The syntax format is as follows:

Image.transpose(method)

The method parameter determines how to flip the image. The parameter values ​​are as follows:

  • Image.FLIP_LEFT_RIGHT: Flip left and right horizontally;
  • Image.FLIP_TOP_BOTTOM: Flip vertically up and down;
  • Image.ROTATE_90: Rotate the image 90 degrees;
  • Image.ROTATE_180: Rotate the image 180 degrees;
  • Image.ROTATE_270: Rotate the image 270 degrees;
  • Image.TRANSPOSE: image transpose;
  • Image.TRANSVERSE: Flip the image horizontally.

Usage examples are as follows:

from PIL import Image

image = Image.open("./image/美女3号.jpg")
# 图像翻转后,返回一个新的Image对象
image_t = image.transpose(Image.FLIP_TOP_BOTTOM)
image_t.show()
image_t.save("./image/美女3号翻转.jpg")

The image shows the results as follows:

 

8.2 rotate() rotate at any angle

When we want to rotate the image to any angle, we can use the rotate() function. The syntax is as follows:

Image.rotate(angle, resample=PIL.Image.NEAREST, expand=None, center=None, translate=None, fillcolor=None)

Parameter description is as follows:

  • angle: represents the angle of any rotation;
  • resample: resampling filter, the default is PIL.Image.NEAREST nearest neighbor interpolation method;
  • expand: Optional parameter, indicating whether to expand the image. If the parameter value is True, the output image will be expanded. If it is False or omitted, it will be output according to the original image size;
  • center: Optional parameter, specifying the rotation center. The parameter value is a tuple of length 2. By default, the image center is used for rotation;
  • translate: The parameter value is a tuple, which means to translate the rotated image, with the upper left corner as the origin;
  • fillcolor: optional parameter, fill color, after the image is rotated, the area outside the image is filled.

Usage examples are as follows:

from PIL import Image

image = Image.open("./image/美女3号.jpg")
# translate的参数值可以为负数,fillcolor将旋转图之外的区域填充为绿色
# 旋转后返回同一个新的Image对象
image_r = image.rotate(30, translate=(-50, -30), fillcolor="yellow")
image_r.show()
image_r.save("./image/美女3号旋转.jpg")

Output result:

 

8.3 transform() image transformation

This function can transform an image and generate a new image of a specified size through the specified transformation method. The syntax format is as follows:

Image.transform(size, method, data=None, resample=0) 

Parameter Description:

  • size: Specify the size of the new image;
  • method: Specify how the image changes, for example, Image.EXTENT represents rectangular transformation;
  • data: This parameter is used to provide the required data for the transformation method;
  • resample: Image resampling filter, the default parameter value is PIL.Image.NEAREST.

Usage examples are as follows:

from PIL import Image

image = Image.open("./image/美女3号.jpg")
# 设置图像大小500*500,并根据data的数据截取原图像的区域,生成新的图像
image_tf = image.transform((500, 500), Image.EXTENT, data=[image.width // 3, image.height // 2, 1200, 955])
image_tf.show()
image_tf.save("./image/美女3号变换.jpg")

The output image is shown below:


 

9 Pillow image noise reduction processing

Due to the influence of imaging equipment, transmission media and other factors, there will always be more or less unnecessary interference information in the image. We refer to these interference information collectively as "noise", such as the common "noise" in digital images. "Salt and pepper noise" refers to some white and black pixels that appear randomly in the image. Image noise not only affects the quality of the image, but also hinders people's visual appreciation. Therefore, noise processing is one of the essential links in the image processing process. We call the process of processing image noise "image noise reduction".

With the continuous development of digital image technology, image noise reduction methods have become increasingly mature. Constructing filters through certain algorithms is the main method of image noise reduction. The filter can effectively suppress the generation of noise without affecting the shape, size and original topology of the processed image.

Pillow achieves the purpose of image noise reduction through the ImageFilter class. This class integrates different types of filters. By calling them, image smoothing, sharpening, boundary enhancement and other image noise reduction operations can be achieved. . Common noise reduction filters are shown in the following table:

Image Noise Reduction Filter
name illustrate
ImageFilter.BLUR Fuzzy filtering, that is, mean filtering
ImageFilter.CONTOUR Contour filtering, looking for image contour information
ImageFilter.DETAIL Detail filtering makes the image display more detailed
ImageFilter.FIND_EDGES Find boundary filtering (find the boundary information of the image)
ImageFilter.EMBOSS Relief filtering, displaying images as relief images
ImageFilter.EDGE_ENHANCE Boundary enhancement filtering
ImageFilter.EDGE_ENHANCE_MORE Deep edge enhancement filtering
ImageFilter.SMOOTH Smoothing filter
ImageFilter.SMOOTH_MORE Deep smoothing filter
ImageFilter.SHARPEN sharpening filter
ImageFilter.GaussianBlur() Gaussian blur
ImageFilter.UnsharpMask() Unsharp Mask Filter
ImageFilter.Kernel() Convolution kernel filtering
ImageFilter.MinFilter(size) Minimum filter that selects the smallest pixel value from the area specified by the size parameter and stores it in the output image.
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) max filter
ImageFilter.ModeFilter() Pattern filtering

Select several methods from the above table for example demonstration. Here is the original image waiting to be processed:

 9.1 Blurring

# 导入Image类和ImageFilter类
from PIL import Image, ImageFilter

image = Image.open("./image/风景.jpg")
# 图像模糊处理
image_blur = image.filter(ImageFilter.BLUR)
image_blur.show()
image_blur.save("./image/风景_模糊.jpg")

The output image is as follows:

 9.2 Contour drawing

from PIL import Image, ImageFilter

image = Image.open("./image/风景.jpg")
# 生成轮廓图
image_contour = image.filter(ImageFilter.CONTOUR)
image_contour.show()
image_contour.save("./image/风景_轮廓.jpg")

The output image is as follows:

 9.3 Edge detection

from PIL import Image, ImageFilter

image = Image.open("./image/风景.jpg")
# 边缘检测
image_edges = image.filter(ImageFilter.FIND_EDGES)
image_edges.show()
image_edges.save("./image/风景_边缘检测.jpg")

The output image is as follows:

 

9.4 Relief images

from PIL import Image, ImageFilter

image = Image.open("./image/风景.jpg")
# 浮雕图
image_contour = image.filter(ImageFilter.EMBOSS)
image_contour.show()
image_contour.save("./image/风景_浮雕图.jpg")

The output image is as follows:

 

9.5 Smooth images

from PIL import Image, ImageFilter

image = Image.open("./image/风景.jpg")
# 平滑图像
image_contour = image.filter(ImageFilter.SMOOTH)
image_contour.show()
image_contour.save("./image/风景_平滑.jpg")

The output image is as follows: 

 If you have used PhotoShop (abbreviated as Ps, a professional image processing software), Fireworks (abbreviated as Fw, a professional image processing software) or mobile phone beauty software, it is not difficult to find that the above operation is to add a "Filter" changes the appearance of a picture by adding filters, thereby affecting our sensory experience of the picture.


 

10 Pillow image color processing

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.

Note: The ImageColor module is not sensitive to the size of the color. For example, "Red" can also be written as "red".

10.1 Color naming

ImageColor supports the naming of multiple color modes (that is, using a fixed format to represent appearance), such as the well-known RGB color mode. In addition, there are also HSL (Hue-Saturation-Brightness), HSB (Also Called HSV, Hue-Saturation-Brightness) color mode. The following is a brief introduction to HSL:

  • H: Hue hue, with a value range of 0 -360, where 0 represents "red", 120 represents "green", and 240 represents "blue";
  • S: Saturation, which represents the purity of the color and ranges from 0 to 100%, where 0 represents gray and 100% represents the most saturated color;
  • L: Lightness, the value is 0~100%, where 0 means "black", 50% means normal color, and 100% means white.

The following uses the HSL color mode to represent red, with the following format:

HSL(0,100%,50%)

The color at this time is "pure red", which is equivalent to RGB (255,0,0). If you want to learn more about HSL/HSB, click on thelink to go.

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

10.2 getrgb() method

As the name suggests, this function is used to get the RGB value of the color. The syntax format is as follows:

PIL.ImageColor.getrgb(color)

Usage examples are as follows:

from PIL import ImageColor

# getrgb()方法
color1 = ImageColor.getrgb("blue")
print(color1)
color2 = ImageColor.getrgb('#DCDCDC')
print(color2)
# 使用HSL模式红色
color3 = ImageColor.getrgb('HSL(0,100%,50%)')
print(color3)

The output is as follows:

(0, 0, 255)
(220, 220, 220)
(255, 0, 0)

 You can create a new image through the new() method, and you can also use ImageColor.getrgb() at this time, as shown below:

from PIL import Image

# 使用new()绘制新的图像
image = Image.new("RGB", (200, 200), ImageColor.getrgb("#A214B4"))
image.save("./image/新建01.jpg")

The display picture is as follows:

 

10.3 getcolor()

This method is similar to getrgb() and is also used to obtain color values, but it has one more parametermode, so this function can obtain the color value of the specified color mode. The syntax format is as follows:

PIL.ImageColor.getcolor(color, mode)

Parameter description is as follows:

  • color: a color name, in string format, which can be the English word of the 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.

Usage examples are as follows:

color4 = ImageColor.getcolor('#A214B4', 'L')
print(color4)
color5 = ImageColor.getcolor('yellow', 'RGBA')
print(color5)

Output result:

81
(255, 255, 0, 255)

11 Pillow adds watermark to pictures

Adding watermarks to images can prevent others from abusing your images to a certain extent. This is an effective way to protect image copyright. Therefore, when you share pictures on some public platforms such as Weibo or blogs, it is recommended that you add a watermark to your pictures to prove that the pictures belong to you.

There are many ways to add watermarks. For example, you can add watermarks through some image processing software or mobile phone beauty software. However, this operation is more complicated, and even some software is not Free.

The Pillow library provides a method of adding watermarks, which is simple to operate, easy to learn, and easy to use. Let's learn how to use PIilow to add watermarks to images.

We know that a watermark is a piece of text information attached to the original image, so the process of adding a watermark involves two issues:

  • First, how to attach text information to pictures;
  • Second, how to draw text information.

As long as these two problems are solved, the watermark can be added successfully. The ImageDraw and ImageFont modules provided by Pillow successfully solve the above problems.

11.1 ImageDraw

The PIL.ImageDraw module provides a series of drawing methods. Through this module, you can create a new graphic or draw another graphic on an existing image, thereby annotating and modifying the original image. .

The following creates an ImageDraw object and gives a brief introduction to how to use the object:

draw = ImageDraw.Draw(image)

 The above method will return an ImageDraw object, and the parameter image represents the Image object. Here we can understand the Image object as a canvas, and by calling some methods of the ImageDraw object, we can draw new graphics on the canvas. Commonly used methods of the ImageDraw object are shown in the following table:

ImageDraw common methods
method illustrate
text Draw text on image
line Draw straight lines and line segments
eclipse Draw an ellipse
rectangle Draw a rectangle
polygon Draw polygon

 Note: The first method text() in the table needs to be used with the ImageFont module, which will be introduced in detail below.

 The syntax format for drawing a rectangular graph is as follows:

draw.rectangle(xy, fill=None, outline=None)

 The parameter description is as follows:

  • xy: tuple parameter value, with the upper left corner of the image as the origin of the coordinates, indicating the position of the rectangular image and the coordinate sequence of the size of the image, in the form of ((x1,y1,x2,y2));
  • fill: the background fill color of the rectangle;
  • outline: the border line color of the rectangular chart.

Let’s look at a simple set of examples:

from PIL import Image, ImageDraw

# 创建 Image 对象,当做背景图
image = Image.new('RGB', (200, 200), color='gray')
# 创建 ImageDraw 对象
draw = ImageDraw.Draw(image)
# 以左上角为原点,绘制矩形。元组坐标序列表示矩形的位置、大小;fill设置填充色为红色,outline设置边框线为黑色
draw.rectangle((100, 50, 150, 100), fill=(255, 0, 0), outline=(0, 0, 0))
# 查看原图片
image.show()
# 保存图片
image.save("./image/添加矩形图.png")

The graphic display results are as follows:

 

11.2 ImageFont

The PIL.ImagreFont module draws different types of text on images by loading font files in different formats, such as TrueType and OpenType fonts.

The syntax format for creating a font object is as follows:

font = ImageFont.truetype(font='字体文件路径', size=字体大小)

If you want to add text to the image, you also need to use the ImageDraw.text() method. The syntax is as follows:

draw =  ImageDraw(image)
draw.text((x,y), "text", font, fill)

Parameter description is as follows:

  • (x,y): The upper left corner of the image is the coordinate origin, (x,y) represents the starting coordinate position of adding text;
  • text: string format, text content to be added;
  • font: ImageFont object;
  • fill: text fill color.

Let's look at a set of usage examples, as shown below:

from PIL import Image, ImageFont, ImageDraw

# 打开图片,返回 Image对象
image = Image.open("./image/风景.jpg")
# 创建画布对象
draw = ImageDraw.Draw(image)
# 加载计算机本地字体文件
font1 = ImageFont.truetype('C:/Windows/Fonts/msyh.ttc', size=36)
font2 = ImageFont.truetype('C:/Windows/Fonts/LHANDW.TTF', size=28)
# 在原图像上添加文本
draw.text(xy=(220, 150), text='o0o江山如此多娇o0o', fill=(255, 0, 0), font=font1)
draw.text(xy=(260, 250), text='China123', fill=(255, 100, 50), font=font2)
image.show()
image.save("./image/风景_水印.png")

The image display results are as follows:

 

11.3 Add image watermark

Through the study of the above knowledge, we have a general understanding of the ImageDraw and ImageFont modules, and also solved the two problems of how to add watermarks to pictures. a key question. The following example shows the detailed process of adding a watermark to an image. The code is as follows:

from PIL import Image, ImageFont, ImageDraw

font = ImageFont.truetype('C:/Windows/Fonts/msyh.ttc', size=36)


def creating_watermark(image, text, font=font):
    # 后面给水印添加透明度,因此需要先转换图片的格式
    image_rgba = image.convert('RGBA')
    print(image_rgba.size)
    # 按照传入的image对象的尺寸,新建一个RGBA模式的白色图片
    im_text_canvas = Image.new('RGBA', image_rgba.size, (255, 255, 255, 0))
    print(im_text_canvas.size)
    # 用刚刚创建的RGBA图片创建画布对象,用于放着水印内容,即text内容
    draw = ImageDraw.Draw(im_text_canvas)

    # 获取文本框的宽度和高度
    # draw.textbbox 返回四元组,表示文本内容的左上角和右下角的两个坐标位置(x0, y0, x1, y1)
    bbox = draw.textbbox((0, 0), text, font=font)
    text_width = bbox[2] - bbox[0]
    text_height = bbox[3] - bbox[1]
    print(text_width, text_height)
    # 计算得到添加文本的起始坐标位置
    text_xy = (image_rgba.size[0] - text_width - 20, image_rgba.size[1] - text_height - 20)  # -20是微调动作,可以不减
    print(text_xy)
    # 在画布对象上添加文本,并设置文本颜色(白色)和透明度(半透明) fill参数的最后一位数值决定透明程度。
    draw.text(text_xy, text, font=font, fill=(255, 255, 255, 120))
    # 将原图片与文字画布复合
    image_text = Image.alpha_composite(image_rgba, im_text_canvas)
    return image_text


image = Image.open("image/风景.jpg")
# image.show()
image_water = creating_watermark(image, '@江山如此多娇')
image_water.show()
image_water.save("image/风景_Watermark.png")

Rendering after adding watermark:


 

12 Pillow and ndarray arrays

NumPy is the basic data package for Python scientific computing. It is widely used in the field of machine learning, such as image recognition, natural language processing, data mining, etc. NumPy is a third-party library that needs to be downloaded and installed in advance.

ndarray is an array type in NumPy, also known as ndarray array. This array can be converted to and from Pillow's PIL.Image object.

12.1 Create images from ndarray array

The following constructs an Image object through the ndarray array and displays the image. Examples are as follows:

# 使用numpy之前需要提前安装
import numpy as np
# 导入相关的包
from PIL import Image

# 创建 300*400的图像,3个颜色通道
array = np.zeros([300, 400, 3], dtype=np.uint8)
# rgb色彩模式
array[:, :200] = [255, 255, 0]
array[:, 200:] = [0, 255, 0]
img = Image.fromarray(array)
img.show()
img.save("./image/数组生成图像.png")

The output is as follows:

 

 12.2 Convert image to ndarray array

The image is output as an ndarray array. The example is as follows:

import numpy as np
from PIL import Image

img = Image.open("./image/风景.jpg")
img.show()
# Image图像转换为ndarray数组
img_2 = np.array(img)
print(img_2)
# ndarray数组转换为Image图像
arr_img = Image.fromarray(img_2)
# 显示图片
arr_img.show()
# 保存图片
arr_img.save("./image/风景_arr.jpg")

Picture display results:

 The pixel array that makes up the image is as follows:

[[[135 178 213]
  [133 178 211]
  [131 177 210]
  ...
  [205 177 173]
  [206 178 174]
  [207 179 175]]

 [[136 174 210]
  [136 174 210]
  [134 174 209]
  ...
  [207 178 174]
  [208 179 175]
  [209 180 176]]

 [[139 175 211]
  [140 176 212]
  [140 176 212]
  ...
  [210 179 176]
  [211 180 175]
  [212 181 176]]

 ...

 [[ 72  97 119]
  [ 71  97 120]
  [ 63  93 117]
  ...
  [122 127 130]
  [ 73  77  80]
  [123 122 128]]

 [[ 59  87 108]
  [ 58  86 107]
  [ 54  85 106]
  ...
  [ 99 104 107]
  [ 85  88  93]
  [ 84  83  89]]

 [[ 56  87 107]
  [ 62  90 111]
  [ 64  92 114]
  ...
  [ 72  77  80]
  [ 52  55  60]
  [ 79  78  86]]]


13 Pillow generates GIF animations

GIF (Graphics Interchange Format, Graphics Interchange Format) is a "bitmap" image format, which uses .gif as the extension of the image. GIF images are very suitable for use on the Internet because they use image pre-compression technology. The application of this technology reduces the time consumed by image propagation and loading to a certain extent.

Compared with images in other formats, GIF has another very important application, which is to generate dynamic images. We know that Pillow can handle a variety of image formats, including GIF format, and it can synthesize static format pictures (png, jpg) into GIF animations. 

Note: Pillow always reads GIF files in grayscale mode (L) or palette mode (P).

Let’s look at a set of examples: How to use Pillow to generate GiF dynamic graphics.

Original material picture:

 The complete code is as follows:

import os
import random
from PIL import Image


def png_to_gif(png_path, gif_path):
    """png合成gif图像"""
    img_frames = []
    # 返回文件夹内的所有静态图的列表
    png_files = os.listdir(png_path)
    # 打印返回的列表
    print(png_files)
    # 读取文件内的静态图,并随机选取到列表中,也可以不随机,就按顺序取
    for i in range(0, len(png_files)):
        img = Image.open(os.path.join(png_path, random.choice(png_files)))  # 随机入列
        # img = Image.open(os.path.join(png_path, png_files[i]))   # 按原本的顺序入列
        img_frames.append(img)
    # 以第一张图片作为开始,将后续的所有图片合并成 gif 动态图
    img_frames[0].save(gif_path, save_all=True, append_images=img_frames[1:], transparency=0, duration=500, loop=0, disposal=2)
    # 参数说明:
    # save_all 保存图像;    transparency 设置透明背景色;   duration 单位毫秒,动画持续时间,
    # loop=0 无限循环;  disposal=2 恢复原背景颜色。参数详细说明,请参阅官方文档,网址见文章末尾处。


# 调用函数,传入对应的参数
png_path = "D:/PycharmProjects/Pillow练习/image/pngs"
gif_path = "D:/PycharmProjects/Pillow练习/image/悟空.gif"
png_to_gif(png_path, gif_path)

Dynamic renderings, as shown below:

 

To learn more about Pillow, please refer to the official documentation:Click to go.


Guess you like

Origin blog.csdn.net/weixin_43498642/article/details/131934074