#-------------- ------------------ ------------------- --------------------- --- #Numpy and OpenCV to create a PowerPoint like animation of image. This is an #example of image appearing in either horizontal and vertical segments #-------------- ------------------ ------------------- --------------------- --- import numpy as np import sys import cv2 #-------------- ------------------ ------------------- --------------------- --- def vidFromImgArray(imgArray, vidName, fps, vidW=0, vidH=0): #Function to create an MP4 file from array of images #Image or video width should be even number else they would be truncated #to the nearest lower even numbers if len(imgArray) <= 1: print("Number of frames <= 1, exiting! \n") sys.exit() if vidW == 0 or vidH == 0: ht, wd, ch = imgArray[0].shape size = (wd, ht) else: size = (vidW, vidH) codec = cv2.VideoWriter_fourcc(*'mp4v') video = cv2.VideoWriter(vidName, codec, fps, size) for img in imgArray: if vidW > 0 and vidH > 0: img=cv2.resize(img, size, interpolation=cv2.INTER_LINEAR) video.write(img) video.release() #-------------- ------------------ ------------------- --------------------- --- def imgAnimEntranceVertSegments(img, n): #Function to create vertical split entrance of image frames = [] #Works for coloured images with Alpha channel and grayscale (BW) images im = cv2.imread(img) dw = im.shape[1] // n ht, wd, ch = im.shape frames.append(im) imgW = np.ones([ht, wd, ch], dtype=np.uint8) * 255 for j in range(1, dw, 1): imgMask = imgW for k in range(0, n, 1): imgMask[:, dw*k:j+dw*k, :] = 0 imgMasked = cv2.bitwise_or(im, imgMask, mask=None) frames.append(imgMasked) #print("Number of frames in return array = ", len(frames)) return frames #-------------- ------------------ ------------------- --------------------- --- def imgAnimEntranceHorizSegments(img, n): #Function to create vertical split entrance of image frames = [] #Works for coloured images with Alpha channel and grayscale (BW) images im = cv2.imread(img) dh = im.shape[0] // n ht, wd, ch = im.shape frames.append(im) imgW = np.ones([ht, wd, ch], dtype=np.uint8) * 255 for j in range(1, dh, 1): imgMask = imgW for k in range(0, n, 1): imgMask[dh*k:j+dh*k, :, :] = 0 imgMasked = cv2.bitwise_or(im, imgMask, mask=None) frames.append(imgMasked) #print("Number of frames in return array = ", len(frames)) return frames #-------------- ------------------ ------------------- --------------------- --- imgFrames = imgAnimEntranceHorizSegments("0.jpg", 5) vidFromImgArray(imgFrames, "BoxAnim.mp4", 500)