Python provides PIL (Python Image Library) image library, to meet the developers of image processing functions, the library provides a wide range of file formats support, including common JPEG, PNG, GIF, it provides image creation, image display, image processing and other functions.

The basic concept

To learn how to use PIL image library, we must first understand some basic concepts about image, including depth, bands, mode, coordinate system, etc.

Depth of image

The number of bits of pixels in an image is the depth of the image, for example:

Binary image: the pixel of the image is either 0 or 1 (the image is either black or white), the number of pixels of the image is 1 bit, and the depth of the image is 1, also known as a bitmap.

Grayscale image: The pixels of the image are between 0 and 255 (0 represents all black, 255 represents all white, 255 levels of grayscale are inserted between 0 and 255). 2^8=255, the number of pixels in the image is 8, and the depth of the image is 8.

By analogy, the number of bits in a computer used to store a single pixel is called the depth of the image.

Image channel

Each image is composed of one or more data channels, such as RGB is the basic three primary colors (red, green and blue), if we use 8 bits to represent a color, then the maximum value of each color is 255, so that the color value range of each pixel point is (0-255, 0-255, 0-255). The channel of such a picture is 3. The number of channels in grayscale image is 1.

Patterns of images

The image is actually a rectangular graph of pixel data. The pattern of the image defines the type and depth of pixels in the image, and each type represents a different depth. In PIL, we call it the pattern of the image. Common patterns are as follows:

1:1 bit pixel, representing black and white, accounting for 8 bits, in the image representation is called a bitmap.

L: represents the gray level between black and white, accounting for 8 bit pixels.

P: 8 bit pixels, using palette mapping.

RGB: true color, occupying 3×8 pixels, where R is red, G is green, and B is blue. Color changes formed by the superposition of the three primary colors. For example, if all three channels are 0, it represents black, and 255 represents white.

RGBA: True color with transparent mask, where the A is alpha transparency, occupying 4×8-bit pixels

There are also CMYK, YCbCr, I, F and other uncommon modes, which will not be introduced here.

Coordinate system of image

The coordinates of the image in PIL start from the top left corner and extend to the bottom right corner, passing in the form of a binary (x, y), X-axis from left to right, Y-axis from top to bottom, i.e. the coordinates of the top left corner are (0, 0). Then rectangles can be represented as quads, for example a 450 x 450 pixel rectangular image can be represented as (0, 0, 450, 450).

The installation of the PIL

Like the other libraries, PIL is simple to install:

pip3 install pillow

PIL image module function

Open the image

We can open files from a local directory, or we can open images from a file stream. The file can be opened as follows:

Image.open(file,mode)

To read image files, mode can only be ‘r’, so we can omit this parameter as well.

from PIL import Image
from io import BytesIO
import requests

# Open the image file
im = Image.open('cat.jpg')

Open the image from the file stream
r = requests.get('http://f.hiphotos.baidu.com/image/pic/item/b151f8198618367aa7f3cc7424738bd4b31ce525.jpg')
im2 = Image.open(BytesIO(r.content))

# Show images
im.show()
im2.show()

# Flip 90 degrees display
im.rotate(90).show()
Copy the code

We first open the cat.jpg image in this directory, and then request an image from Baidu Image, and open it in the way of file stream. Use the show method to display images. We can also use the Rotate method to rotate the image. Running the program, we will see three images pop up, one is the corresponding image of cat.jpg, one is the image of Baidu pictures, and one is the image of cat.jpg after turning it 90 degrees.

Create an image

Image.new(mode,size,color)

We can create a new image with a given pattern, size, and color. The size is given as a binary (width, height) in pixels; Colors are given as a single value of a single-band image and as a tuple of a multi-band image (one value for each band), using a color name such as’ red ‘, using a hexadecimal ‘#FF0000’, or using numbers (255,0,0).

from PIL import Image

im = Image.new('RGB', (450, 450), (255, 0, 0))
im1 = Image.new('RGB', (450, 450), 'red')
im2 = Image.new('RGB', (450, 450), '#FF0000')
im.show()
im1.show()
im2.show()
Copy the code

In the example above, we created a 450×450 RGB mode image with red color in three different ways. The final image effect is the same.

Transformation format

Image.save(file)

We directly use the save method, modify the saved file name can be converted image format.

from PIL import Image

# to load the JPG
im = Image.open('cat.jpg'.'r')

Print the image type
print(im.format)

Save as a PNG image
im.save('cat.png')

Load a newly saved PNG image
im2 = Image.open('cat.png'.'r')

Print a new saved image type
print(im2.format)


# output result
JPEG
PNG
Copy the code

In this example, we will open the cat.jpg image and save a new image of type PNG. We can see both formats by printing.

Creating thumbnails

Image.thumbnail(size, resample=3)

Modify the current image to make a thumbnail that is no larger than the given size. This method calculates an appropriate thumbnail size to match the aspect ratio of the current image, calls the method Draft () to configure the file reader, and finally changes the size of the image.

The size parameter represents the final thumbnail size given.

Resample parameter is a filter and can only be one of NEAREST, BILINEAR, BICUBIC or ANTIALIAS. If this variable is omitted, it defaults to NEAREST.

Note: In the current version of PIL, the filters BILINEAR and BICUBIC are not well adapted for thumbnail generation. Users should use ANTIALIAS for the best image quality. If processing speed is more important than image quality, other filters can be used. This method is modified on the original image.

from PIL import Image

# load image
im = Image.open('cat.png')

# Show images
im.show()

# Image size
size = 128, 128
# Zoom image
im.thumbnail(size, Image.ANTIALIAS)

# Show images
im.show()
Copy the code

We scaled a 450×450 image to a 128×128 image, and the program ran as follows:

The fusion image

Image.blend(image1, image2, alpha)

Image imagE1 and image IM2 are fused according to the alpha value, and the formula is:

Out = image1 * (1.0-alpha) + image2 * alpha

Image1 and image2 represent two images of the same size and mode, and alpha is a value between 0 and 1. If alpha is 0, return image1 image, if alpha is 1, return image2 image.

from PIL import Image

# Blue image
image1 = Image.new('RGB', (128, 128), (0, 0, 255))
# Red image
image2=Image.new('RGB', (128, 128), (255, 0, 0))
# take the middle valueIm = image.blend (image1, image2, 0.5) image1.show() image2.show()# Display purple image
im.show()
Copy the code

We fuse a blue image and a red image at a fusion degree of 0.5 for each image, and finally get a purple image (because red superimposed blue will be blended into purple). The display image is as follows:

Pixel processing

Image.eval(image, *args)

Each pixel of the image is processed according to the function passed in. The first parameter, image, is the image object to be processed, and the second parameter, function object, takes an integer.

If the image represented by the variable image has more than one channel, the function applies to each channel. Note: The function processes each pixel only once, so you can’t use random components and other generators.

from PIL import Image

im = Image.open('cat.jpg')
im.show()

# Double the value of each pixel (equivalent to doubling the brightness)
evl1 = Image.eval(im, lambda x: x*2)
evl1.show()

# Halve each pixel value (equivalent to halving brightness)
evl2 = Image.eval(im, lambda x: x/2)
evl2.show()
Copy the code

We double and halve the pixel value of the image respectively, and the display effect is shown as follows:

A composite image

Image.composite(image1, image2, mask)

Create a new image using the given two images and the mask image as opacity. The mode of the variable mask image can be “1”, “L” or “RGBA”. All images must be of the same size.

from PIL import Image

# open the PNG
image1 = Image.open('cat.png')

# open flower. JPG
image2 = Image.open('flower.jpg')

# Separate channels for image1
r, g, b = image1.split()

# Compositing image to get cat + flower
im = Image.composite(image1, image2, mask=b)

image1.show()
image2.show()
im.show()
Copy the code

In the above example, we combined an image of a cat (cat.png) and an image of a flower (flower.jpg) with a mask formed by one of the channels of the image cat. Just like in PHOTOSHOP, we finally got the image of cat + flower, as shown below:

Create images through a single channel

Image.merge(mode,bands)

Combine a set of single-channel images into multi-channel images. The parameter mode is the mode of the output image, and bands is the sequence of each channel in the output image.

from PIL import Image

im = Image.open('cat.png')
# Split the three channels
im_split = im.split()

# Display three single-channel images respectively
im_split[0].show()
im_split[1].show()
im_split[2].show()

# merge the three channels again
im2 = Image.merge('RGB', im_split)
im2.show()

# Open the second image
im3 = Image.open('flower.jpg')
# Split the three channels of the second image
im_split2 = im3.split()

# Combine the first channel of the second image and the second and third channels of the first image into one image
rgbs = [im_split2[0], im_split[1], im_split[2]]
im4 = Image.merge('RGB', rgbs)
im4.show()
Copy the code

In the above example, we first split the three channels of the cat.jpg image into three images, as shown below:

Then we separated the three channels of the flower. JPG image, and finally took the R channel image of flower. JPG and the G and B channel image of cat. JPG respectively to create a new image, the final result is as follows:

conclusion

This section introduces you to some of the basic concepts related to images in the Python Pillow library, as well as some of the common features for processing images in the PIL module. After mastering these functions, we can open and create images, and also do some common operations such as splitting, synthesis and fusion of images. These are the basis of image processing, and we need to understand and master them well.

The resources

www.osgeo.cn/pillow/refe…