In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to convert pictures and videos by Python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to convert pictures and videos with Python.
Use background
Sometimes we need to synthesize a lot of pictures into video, or write a script ourselves to speed up or slow down the video.
Sometimes it is necessary to cut the video into pictures for follow-up operations.
Two methods are provided here, one is video to image, the other is image to video.
I. converting video to image
Sometimes we need to capture pictures of the videos in the folder at a certain frame rate, such as three pictures per second. In order to achieve this requirement, I specially wrote a code to implement it, and attached a very detailed description. In order to facilitate you to change the code to meet your needs (mainly to take care of those who have just learned python or only need to use this need), the features of this blog are as follows:
The function can be realized by directly changing the location of the input folder and the output folder.
The output image can be named according to a certain format, here it is named after 202108230001
Step through the video in the folder one by one, and the sequence numbers can be continuous (or discontinuous, and the code needs to be changed)
By using the bilinear interpolation method, the resolution can be guaranteed even if the picture becomes larger (as for bilinear, it is only implemented by calling the opencv method. See my next blog post for the specific implementation code and principle).
The code is explained in great detail and can be understood at a glance
Very detailed code implementation
First, explain the parameters that need to be modified by yourself. The code is as follows:
Filepath = 'save_filename='C:/Users/ZFG/Desktop/2222/' hand UsersAccording to the directory where the video folder is located, timeF # if the stored files are in 20210823,00001 format, the address of the folder where the pictures are stored for the first half of UsersAccording to the number of frames per second, for example, if my video is 24 frames per second, take 6 and take three kernal= (700700) # a second to set the output size. Set up according to your own needs
Then set up a method to store the intercepted image:
Def saveImage (image,SaveAddress,num): # image is the image to be read, SaveAddress is the address to be saved, and num is the sequence number address= SaveAddress+data+'_'+str (num) .zfill (4) + '.jpg' # which is recorded when the picture is intercepted. Here the output format is cv2.imwrite (address,image) # here is to save the picture.
Then read the folder:
PathDir = os.listdir (filepath) i=0j=0for allDir in pathDir: # iterate through each video in the folder videopath = filepath+'/'+ allDir videoCapture=cv2.VideoCapture (videopath) # enter the absolute path untill,picture=videoCapture.read () # read the video. When the video is read, the returned untill is False Indicates that the video has been read while untill: iComple1 if (i%timeF==0): # look at the frequency of the screenshot according to the frame rate of the original video, jobless 1 picture=cv2.resize (picture,kernal,cv2.INTER_LINEAR) # the bilinear interpolation method in opencv is called here, if the picture increases quickly Ensure the image precision saveImage (picture,save_filename,j) # call the method pass untill we described earlier, picture = videoCapture.read () # again to see whether the video ends, and if it ends, until is False.
After combining the appeal code, the final procedure is as follows:
Import cv2import osi = 0j = 0pathDir = os.listdir (filepath) filepath = 'C:/Users/ZFG/Desktop/1111'data='20210823'save_filename='C:/Users/ZFG/Desktop/2222/'timeF = 6kernal = (700700) def saveImage (image,SaveAddress,num): address= SaveAddress+data+'_'+str (num) .zfill (4) +' .jpg 'cv2.imwrite (address,image) for allDir in pathDir: videopath = filepath+'/'+ allDir videoCapture=cv2.VideoCapture (videopath) untill Picture=videoCapture.read () while untill: iTunes 1 if (i%timeF==0): jacks 1 picture=cv2.resize (picture,kernal,cv2.INTER_LINEAR) saveImage (picture,save_filename,j) pass untill, picture=videoCapture.read ()
Finally, take a look at the effect:
The first picture is the video in the folder with a frame rate of 24 frames per second, and the second picture is the picture stored in the folder after taking three pictures in one second.
Second, converting image to video
Sometimes we need to synthesize a lot of pictures into a video, or write a script ourselves to speed up or slow down the video. I wrote this because I have a model that can only deal with pictures, but I want to see the processing effect of the video, so I first turn the video into a picture, and then I turn the picture into a video, so the demand is solved.
Very detailed code implementation
First, explain the parameters that need to be modified by yourself. The code is as follows:
If _ _ name__ = ='_ main__': im_dir ='# Image frame storage path, here write a folder dir_list=os.listdir (im_dir) fps = 20 # set a frame rate, the more frames per second, the faster the video will be stored after dir_video=''# the synthesized video video_dir = dir_video + '.avi' frame2video (im_dir, video_dir, fps)
Then set up a method to store the intercepted image:
Def frame2video (im_dir, video_dir, fps): im_list = os.listdir (im_dir) im_list.sort (key=lambda x: int (x.replace ("frame", "). Split ('.') [0]) img = Image.open (os.path.join (im_dir, im_ list [0]) img_size = img.size # to obtain the image resolution, the image resolution under the folder needs to be consistent In case of inconsistency, you can write an if function resize ~ fourcc = cv2.VideoWriter_fourcc (* 'XVID') # opencv version is 3 videoWriter = cv2.VideoWriter (video_dir, fourcc, fps, img_size) for i in im_list: im_name = os.path.join (im_dir + I) frame = cv2.imdecode (np.fromfile (im_name, dtype=np.uint8) -1) videoWriter.write (frame) videoWriter.release () print ('Done')
The great task has been completed
Import cv2import osimport numpy as npfrom PIL import Imagedef frame2video (im_dir, video_dir, fps): im_list = os.listdir (im_dir) im_list.sort (key=lambda x: int (x.replace ("frame", "). Split ('.') [0]) img = Image.open (os.path.join (im_dir) Im_ list [0]) img_size = img.size fourcc = cv2.VideoWriter_fourcc (* 'XVID') # opencv version is 3 videoWriter = cv2.VideoWriter (video_dir, fourcc, fps, img_size) for i in im_list: im_name = os.path.join (im_dir + I) frame = cv2.imdecode (np.fromfile (im_name, dtype=np.uint8) -1) videoWriter.write (frame) videoWriter.release () print ('Done') if _ _ name__ = =' _ main__': im_dir =''dir_list=os.listdir (im_dir) fps = 20 dir_video='' video_dir = dir_video + '.avi' frame2video (im_dir, video_dir, fps) so far I believe you have a deeper understanding of "how to achieve the conversion between pictures and videos in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.