Python language PIL library Image.fromarray() realizes multi-image splicing

    Image.fromarray() provided by the PIL library can convert a digital matrix into a picture and realize the conversion of matrices and pictures. If there are multiple picture matrices in the matrix, it is equivalent to picture splicing. 

    As shown below, is an example of using Image.fromarray() by the PIL library in python to splice pictures:

import cv2
import numpy as np
from PIL import Image

img = cv2.imread("lenna.png", 0)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_blur = cv2.GaussianBlur(img_gray, (3, 3), 0)
edge = cv2.Canny(img_blur, 50, 150)
contrast = np.concatenate([img, edge], 1)
img_out = Image.fromarray(contrast)
img_out.show()

    Run this code and you can see the display image:

 

   In this code, the name of the last generated image is not specified and it is not saved, so what is finally displayed is a temporary image file, as can be seen from the name displayed in the file.

    Before the Image.fromarray() method call above, the np.concatenate() method is also used, here is the method of stitching multiple matrices together. By default, np.concatenate() is spliced ​​in the vertical direction, that is, the second parameter axis = 0 here. If you want to splice horizontally, you need to specify axis=1 here. If you specify axis=None, it will flatten the matrix, that is, break up all the elements of the matrix and merge them into a new list set. As shown below, np.concatenate example:

    In the previous example of picture splicing display, if we pass in axis=0, the effect will be as follows:

 

    When running Image.fromarray() in the editor to display an image, you need to first return an image img_out, and then call the img_out.show() display method of the image before the image can be displayed.

img_out = Image.fromarray(contrast)
img_out.show()

Guess you like

Origin blog.csdn.net/feinifi/article/details/131095950