Implementation example of lossless splicing of multiple pictures in Python

Time:2021-9-22
catalogue
  • Import Python Library
  • View the pictures to be spliced
  • Transverse splicing
  • Save the spliced picture
  • summary
    • Longitudinal splicing
    • Picture spacing

Import Python Library


import matplotlib.pyplot as plt
import skimage.io as io
import numpy as np

View the pictures to be spliced

Because of the need of work, we don’t use the pictures of last night.
I took two screenshots as an example.

First, let’s see what the picture looks like before splicing:

JZG = io. Imread ('jzg. JPG ') # NP. Ndarray, [h, W, C], value range [0, 255], RGB
Plt.imshow (JZG) # view pictures
plt.show()

Explanation: “JZG” saves an array of numpy.

jzg

LGZ = io. Imread ('lgz. JPG ') # NP. Ndarray, [h, W, C], value range [0, 255], RGB
plt.imshow(lgz)
plt.show()

lgz

Because I use jupyter notebook, the picture is not very clear.

Check the size of the image and the data type of the array elements.

Print (JZG. Shape) # view the size of the picture
Print (JZG. Dtype) # view array element data types
print(lgz.shape)
print(lgz.dtype)

Output:

(720, 1280, 3)
uint8
(720, 1280, 3)
uint8

(720, 1280, 3) represents the size of the array. The physical meaning is [h, W, C]. They are the height h of the picture, the width W of the picture, and the number of channels C of the picture.

It can be seen that the sizes of the two are exactly the same, and the data type of the array element is “uint8”.

To view the value range of an element in an array:


print([jzg.min(), jzg.max()])

Output:

[0, 255]

Transverse splicing

Create an array for splicing:

Pj1 = np.zeros ((7201280 + 1280,3)) # transverse splicing
Pj1 [:,: 1280,:] = JZG. Copy() # picture JZG on the left
Pj1 [:, 1280:,:] = LGZ. Copy() # picture LGZ on the right
Print (pj1. Dtype) # view array element types

Output:

float64

It can be seen that the data type after splicing is different, so it should be changed, otherwise the display is wrong.

Pj1 = NP. Array (pj1, dtype = NP. Uint8) # change the data type of pj1 array element to "uint8"
Plt.imshow (pj1) # view splicing
plt.show()

拼接后的图片

Save the spliced picture

Save the spliced pictures in the current directory or change to other paths.

Io. Imsave ('pj1. JPG ', pj1) # saves the spliced picture

summary

The codes for horizontal splicing are summarized as follows:

import matplotlib.pyplot as plt
import skimage.io as io
import numpy as np

JZG = io. Imread ('jzg. JPG ') # NP. Ndarray, [h, W, C], value range (0, 255), RGB
Plt.imshow (JZG) # view pictures
plt.show()

LGZ = io. Imread ('lgz. JPG ') # NP. Ndarray, [h, W, C], value range (0, 255), RGB
plt.imshow(lgz)
plt.show()

Print (JZG. Shape) # view the size of the picture
Print (JZG. Dtype) # view array element data types
print(lgz.shape)
print(lgz.dtype)

Pj1 = np.zeros ((7201280 + 1280,3)) # transverse splicing
Pj1 [:,: 1280,:] = JZG. Copy() # picture JZG on the left
Pj1 [:, 1280:,:] = LGZ. Copy() # picture LGZ on the right
Print (pj1. Dtype) # view array element types

Pj1 = NP. Array (pj1, dtype = NP. Uint8) # change the data type of pj1 array element to "uint8"

Plt.imshow (pj1) # view splicing
plt.show()

Io. Imsave ('pj1. JPG ', pj1) # saves the spliced picture

Longitudinal splicing

Of course, it can be spliced horizontally or vertically. Just change the array used for splicing to the following:

Pj2 = np.zeros ((720 + 7201280,3)) # transverse splicing

Change the splicing operation to:

Pj1 [: 720,:,:] = JZG. Copy() # picture JZG on
Pj1 [720:,:,:] = LGZ. Copy() # picture below

Then the other steps are the same.

Picture spacing

Sometimes seam splicing is required. At this time, the array used for splicing is enlarged horizontally or vertically, and the blank area is filled with “0” or “255” (I don’t know which of “0” and “255” represents white and which represents black. If necessary, I’ll experiment or query myself).

This is the end of this article about the implementation example of lossless splicing of multiple pictures in Python. For more information about lossless splicing of Python pictures, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]