#Python script to convert PNG files to a MOVIE (MP4) file. Make sure that PNG #files are of nearly equal size. Alternatively, you can specify size of GIF file #[H x W] = Height x Width. Apply argument -s to scale [rescale] the images. To #disable scaling, set n < 0 in case you are code is run from command line. #------------------------------------------------------------------------------- import cv2 import os, sys from PIL import Image import glob imgFolder = "Images" vidName = "Video.mp4" #Specify size of the video if different from image size: 848x480, 1280x720 W = 640 H = 360 #Define number of frames (images) per second. A frame rate refers to the number #of individual frames or images that are displayed/second of film or TV display. #Slow motion effects are created by recording hundreds of frames per second and #then playing them back at slower rate. An example would be a bullet shattering #a light bulb. It may only take a fraction of a second but if the camera records #the light bulb a 500 [frame/second]. When plays back at 25 FPS, this movie on #screen will take [500/25 = ] 20 times as long. If you shoot the bullet travel #for 0.1 [s] at 500 FPS and then play it back at 25 FPS it will take 0.1x500/25 #= 2 [s] to watch the video even though the scene only took 0.1 [s] to record. nfs = 0.5 #Specify toggle variable to scale the images n = 1 #images = [img for img in os.listdir(imagFolder) if img.endswith(".png")] #glob.glob(Pathname) fetches all file with extension PNG in that folder images = glob.glob(os.path.join(imgFolder, "*.png")) images.sort() width, height = Image.open(images[0]).size size = (width, height) codec = cv2.VideoWriter_fourcc(*'mp4v') #DIVX, DIVD if (len(sys.argv) < 2 and n > 0): video = cv2.VideoWriter(vidName, codec, nfs, (W, H)) else: video = cv2.VideoWriter(vidName, codec, nfs, size) #Do not open images in 'Image' as well as 'CV2', else following error occurs # returned NULL without setting an error for img in images: #In case images are of different size, resize them to user specified values if (len(sys.argv) < 2 and n > 0): newImg = cv2.imread(img) newImg = cv2.resize(newImg, (W, H)) video.write(newImg) else: video.write(img) video.release() cv2.destroyAllWindows()