Learning record 02: How to easily process images with python's PIL package

How to easily process images with python's PIL package


foreword

It is not recommended to use the cv2 image processing package to process images here, because he cannot access the pictures under the Chinese path, and the read value is NONE, which is sometimes very annoying, so today I will record how to use the PIL package Simple processing of images.


Tip: The following is the text of this article, and the following cases are for reference

1. What is PIL?

The Pillow library is a third-party library for Python.
In Python2, PIL (Python Imaging Library) is a very useful image processing library, but PIL does not support Python3, so someone (Alex Clark and Contributors) provided Pillow, which can be used in Python3.

2. Use steps

1. Install the library

Although Python3 provides Pillow, we still install PIL directly when installing.

pip install pillow

2. Import library

The code is as follows (example):

from PIL import Image

3. Read data

The code is as follows (example):

	img = Image.open("E:/数据集/0426b97_45d.jpg")	#这里都是绝对地址的写法,也可以写成E:\\数据集\\guofeng1\\0426b97_45d.jpg
	img.show()												#展示图片

4. Read the picture size

The picture read here has no shape, only size, which can be read as follows:

a = img.size		#输出为(1224, 1024)

It can also be assigned to other variables:

a = img.size		#a被赋值(1224, 1024)
a = img.size[0]		#a被赋值1224
a = img.size[1]		#a被赋值1024

Note that the number of channels cannot be viewed here. If you want to view it, you can view it through the following code:

    print(len(img.split()))		#这里将输出3,split将图像颜色通道分成单独的波段,统计其个数就可以得到通道数

5. Image scaling

resize(size, resample=BICUBIC, box=None, reducing_gap=None): Resize the image and return the resized image. There are 4 parameters.
size , the scaled size of the image, a tuple (width, height) with a length of 2 is passed in.
resample , resample, is an optional resample filter. Can pass in Image.NEAREST, Image.BOX, Image.BILINEAR, Image.HAMMING, Image.BICUBIC, Image.LANCZOS. The default filter is Image.BICUBIC. Always set to Image.NEAREST if the image's mode is '1' or 'P'.
box , the area to scale the image. Pass in a tuple (x 1 , y 1 , x 2 , y 2 ) with a length of 4. This area must be within the (0, 0, width, height) range of the original image. If it exceeds the range, an error will be reported. If no value is passed By default, the entire original image will be scaled. (x 1 , y 1 ) is equivalent to the coordinates of the upper left corner of the clipping area, and (x 2 , y 2 ) is equivalent to the coordinates of the lower right corner of the clipping area.
reducing_gap , reducing the gap. Pass in a floating-point number to optimize the zoom effect of the image. By default, no optimization is performed. When the value is greater than 3.0, the optimization effect is basically fair resampling.

img_resize = img.resize((500, 400), resample=Image.LANCZOS, box=(100, 100, 1200, 800), reducing_gap=5.0)

6. Save pictures

img.save('d:/dog.jpg')

Guess you like

Origin blog.csdn.net/qq_43180908/article/details/116201888