I Project introduction
1. Mission objectives
Based on several photos, several simulation videos containing a certain number of target movements should be made: for example, aircraft moving on the runway, ships sailing in the river, etc.
2. Implementation ideas
It is planned to intercept samples of targets (such as aircraft, ships, etc.) from existing pictures, and select background pictures (such as airport and river pictures as the background). Paste the target pictures into the background pictures in batches. Each time you paste, the target pasting position will be changed in pixels according to the picture sequence. Finally, the generated pictures will be synthesized into video, and the target will move on the background during projection.
3. Design process
(1) First, use PS software to deduct the target from the background and save it as a separate file. In addition, if necessary, use PS to fine tune the background picture
(2) The function in the image processing library PILLOW in Python is used to paste the target onto the background picture, and the batch operation can be realized by using the for loop
(3) Use the cv2 Library (OpenCV, OpenCV Python) to synthesize the picture synthesized in step (2) into video. After checking that it is correct, the design is completed
II Detailed explanation of implementation process (with code)
1. Picture processing process
(1) Intercept target from existing image
Use PS to complete this step. Open the original picture with PS and zoom in (press and hold the left Alt key and then use the mouse pulley to zoom in or out the picture). Use the lasso tool (Polygonal Lasso tool is recommended. Although the magnetic lasso tool is highly automated, the frame selection target is inaccurate) to select the target box to be selected in the picture, Then press Ctrl+J to quickly change the target list selected in the lasso box into a new layer (the default name is "layer 1").
Select the generated new layer and use the clipping tool in the toolbar on the left to clip out the target in layer 1 (the range of the clipping box should fit the target as much as possible, and the redundant blank parts should be boxed in as little as possible)
After clipping, right-click the clipped layer and select "export as...", In the pop-up window, select Save as PNG format. This is because the intercepted targets are basically irregular shapes. If you choose to save them in other formats (such as JPG), the system will automatically fill the picture into a rectangular shape with white when saving, and the white area around the target will be pasted on the background in the subsequent pasted pictures, Saving the picture in PNG format will not appear this redundant filling part.
(2) Background image processing
Above, we save the intercepted target in PNG format. Below, we also save the background picture in PNG format. Just select to open the selected background picture in PS and export it to PNG format. During the process of saving as, you can also modify the size of the background picture according to the situation (generally, you can enlarge or reduce the width and height pixels of the background picture in equal proportion).
If there are other objects we don't want to appear in the background picture, we can use a simple PS tool to smear and cover them. For example, after the rectangular box selection tool in the toolbar is selected, Ctrl+J creates a new layer and moves the layer to cover the objects we don't want to appear. You can also use the imitation stamp tool in the toolbar to smear and cover the unsatisfied parts of the image. After the above processing, we can export the modified picture.
2. Code implementation
(1) Target code pasted onto the background
In this part of the code, I mainly call the image processing function in the PIL library for processing.
PIL, the full name of Python Imaging Library, is a very powerful and easy-to-use image processing library on the python platform. However, since PIL only supports Python 2.7 and is in disrepair for a long time, a group of volunteers created a Python 3 compatible version based on PIL, named pilot. We can use PIL by installing pilot.
The following is the code implementation of the function that pastes the target onto the background:
from PIL import Image def mix(src,dst,position): im1 = src im2 = dst layer = Image.new('RGBA', im1.size, (0, 0, 0, 0)) layer.paste(im2,position) out = Image.composite(layer,im1,layer) return out
src is the background picture, dst is the target, and position is to set the coordinates of the target pasted on the background picture.
I didn't use the paste method in the PIL library directly to paste the target onto the background image, because there would be white parts around the pasted target. So here I use image New() generates a blank image layer with the same size as the background image, pastes the target image onto the blank image layer with the paste method, and finally uses the composite function to overlay the pasted layer with the background image to form a new image and return it. The final effect is to paste the target to the specified position of the background without a white bottom edge.
(2) Use the for loop to generate a series of images
Because my idea is to change the pasting position of the target a little each time (for example, only one pixel), then a series of generated images will be combined into a video, which will have the effect of the target moving on the background. Based on the above target paste function, you can automatically generate this series of images by repeatedly calling the above paste function with a for loop (you only need to change the paste position parameter each time).
Code examples are as follows:
#Generate images in frames 0 ~ 229 x_bias=0 y_bias=0 for i in range(0,230): transit = mix(im1, im2, (600+x_bias, 920-y_bias)) transit.save('E:/temp/video1/generator1'+'/'+str(i)+'.png') y_bias+=1 if y_bias % 3 == 0: x_bias+=1 if (i+1)%10 == 0: print('Now saved' + str(i + 1) + 'Picture') print('Ship trajectory: 0~229 End of frame generation')
(3) Synthesize a series of generated images into video
After the above image is generated, it can be generated into a video. Here I use the functions in the cv2 Library (install the cv2 Library in the environment first when using the following code).
The code is shown below, and the following code snippet contains detailed comments to explain the code:
import cv2 #Synthesize the generated pictures into video fourcc = cv2.VideoWriter_fourcc(*'XVID') #The encoder is set here. The parameters in parentheses mean to set the format of generated video, or other format types can be selected videoWrite = cv2.VideoWriter('E:/temp/video1/generator1.avi',fourcc,24,(1200,1200)) ''' above VideoWriter The parameter meanings of the method are as follows: 1. File storage address/Name 2. Encoder 3. Video frame rate 4 .size Also pay special attention to the above VideoWriter The first parameter in the parameters of the method: the file save address/The name cannot contain Chinese characters, otherwise an error will occur ''' for i in range(230): fileName = 'E:/temp/video1/generator1'+'/'+str(i)+'.png' #The filename here represents the imported image img = cv2.imread(fileName) videoWrite.write(img)# Write method 1 jpg data if (i + 1) % 10 == 0: print('Written'+str(i + 1)+'/230 Picture to video') print('Video generation complete')
(4) Other implementation function codes
The generated picture can be locally cropped and enlarged to the original size, so that the generated video looks like it has the effect of local focusing and amplification:
''' Realize the clipping of the picture, and enlarge the clipped picture to the original size, so as to achieve the magnification effect from far to near ''' from PIL import Image import cv2 left=0 upper=0 right=1200 lower=1200 width=1200 height=1200 for i in range(200): img = Image.open('E:/temp/picture2'+'/'+str(i)+'.png') cropped = img.crop((left, upper, right, lower)) # (left, upper, right, lower) cropped_resize = cropped.resize((width, height), Image.ANTIALIAS) cropped_resize.save('E:/temp/picture3'+'/'+str(i)+'.png') upper+=3 right-=3 if i%10 == 0: print('Cropped zoom in'+str(i)+'Picture') print('All pictures have been processed!') print('Start writing the newly processed picture to the video E:/temp/test3.avi') fourcc = cv2.VideoWriter_fourcc(*'XVID') videoWrite = cv2.VideoWriter('E:/temp/test3.avi',fourcc,24,(1200,1200)) # Write object 1 file name 2. Encoder 3 Frame rate 4 size for i in range(200): fileName = 'E:/temp/picture3'+'/'+str(i)+'.png' img = cv2.imread(fileName) videoWrite.write(img)# Write method 1 jpg data if i%10 == 0: print('Written'+str(i)+'Picture to video') print('All pictures have been written!')
Use the rotate method in the PIL library to rotate and paste the target, which can be used to achieve the effect of rotating the target in the background:
from PIL import Image img = Image.open('E:/file/Aircraft image 1.png') img_rotate=im2.rotate(75) #Here, the IMG is rotated 75 degrees to obtain a new image img_rotate
Use the convert method in the PIL library and input the parameter 'L' to convert the picture into a gray image, so that the video synthesized by the picture looks black and white:
from PIL import Image im1 = Image.open('E:/file/Airport image 1.png') im1_grey=im1.convert('L') im1_grey.show() #For example, convert all images into gray image style for i in range(230): im1=Image.open('E:/temp/video3/generator3/'+str(i)+'.png') transit=im1.convert('L') transit.save('E:/temp/video3/generator4/'+str(i)+'.png') if (i+1) % 10 == 0: print('Already'+str(i+1)+'Convert picture to grayscale') print('Start compositing video')