#-------------- ------------------ ------------------- --------------------- --- #Numpy and OpenCV to create a PowerPoint like animation of image. This is an #example of BOX-appearance starting from centre of the image. There are also #example of wiping an image from centre to edges and from Top - Left corner to #the Bottom-Right corner. This code is good demonstration of slicing of arrays #in NumPy along with use of numpy.insert and numpy.append operations. Creation #of sub-matrix and cropping of an image while maitaining size same as input #image can also be achieved with this piece of code. #-------------- ------------------ ------------------- --------------------- --- 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 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 imgAnimBoxEntrance(img): #Function to create box-shaped entrance of image from centre to edges frames = [] #Works for coloured images with Alpha channel and grayscale (BW) images img = cv2.imread(img) h_half = img.shape[0]//2 w_half = img.shape[1]//2 even = img.shape[0] % 2 arat = img.shape[1]/img.shape[0] #Check if image is colored with or without alpha layer if len(img.shape) > 2: ht, wd, ch = img.shape imgW = np.ones([ht, wd, ch], dtype=np.uint8) * 255 for i in range(1, h_half, 1): j = int (i * arat) imgW[h_half-i:h_half+i, w_half-j:w_half+j, :] = 0 cropImg = cv2.bitwise_or(img, imgW, mask=None) frames.append(cropImg) #Process image as Black-and-White pixels else: ht, wd = img. shape imgW = np.ones([ht, wd], dtype=np.uint8) * 255 for i in range(1, h_half, 1): j = int(i * arat) imgW[h_half-i:h_half+i, w_half-j:w_half+j] = 0 cropImg = cv2.bitwise_or(img, imgW, mask = None) frames.append(cropImg) return frames #-------------- ------------------ ------------------- --------------------- --- def imgAnimBoxEntranceEdge2Centre(img): #Function to create box-shaped entrance of image from edges to centre frames = [] #Works for coloured images with Alpha channel and grayscale (BW) images img = cv2.imread(img) h_half = img.shape[0]//2 w_half = img.shape[1]//2 arat = img.shape[1]/img.shape[0] #Check if image is colored with or without alpha layer if len(img.shape) > 2: ht, wd, ch = img.shape imgW = np.ones([ht, wd, ch], dtype=np.uint8) * 255 for i in range(1, ht, 1): j = int (i * arat) imgW[0:i, :, :] = 0 imgW[:, 0:j, :] = 0 imgW[ht-i:ht, :, :] = 0 imgW[:, wd-j:wd, :] = 0 cropImg = cv2.bitwise_or(img, imgW, mask=None) frames.append(cropImg) #Process image as Black-and-White pixels else: ht, wd = img. shape imgW = np.ones([ht, wd], dtype=np.uint8) * 255 for i in range(1, h_half, 1): j = int(i * arat) imgW[0:i, :] = 0 imgW[:, 0:j] = 0 imgW[ht-i:ht, :] = 0 imgW[:, wd-j:wd] = 0 cropImg = cv2.bitwise_or(img, imgW, mask = None) frames.append(cropImg) return frames #-------------- ------------------ ------------------- --------------------- --- #Function to (box shape) wipe out an image from centre towards edge def imgAnimBoxWipeOut(img): frames = [] #Works for coloured images with Alpha channel and grayscale (BW) images img = cv2.imread(img) h_half = img.shape[0]//2 w_half = img.shape[1]//2 even = img.shape[0] % 2 arat = img.shape[1]/img.shape[0] #Check if image is colored with or without alpha layer if len(img.shape) > 2: ht, wd, ch = img.shape imgW = np.ones([ht, wd, ch], dtype=np.uint8) * 255 for i in range(1, h_half, 1): j = int (i * arat) imgW[h_half-i:h_half+i, w_half-j:w_half+j, :] = 0 cropImg = cv2.bitwise_or(img, ~imgW, mask=None) frames.append(cropImg) #Process image as Black-and-White pixels else: ht, wd = img. shape imgW = np.ones([ht, wd], dtype=np.uint8) * 255 for i in range(1, h_half, 1): j = int(i * arat) imgW[h_half-i:h_half+i, w_half-j:w_half+j] = 0 cropImg = cv2.bitwise_or(img, ~imgW, mask = None) frames.append(cropImg) return frames imgFrames0 = imgAnimBoxWipeOut("0.jpg") imgFrames1 = imgAnimBoxEntranceEdge2Centre("1.jpg") imgFrames2 = imgAnimBoxEntrance("2.jpg") imgFrames = np.concatenate([imgFrames0, imgFrames1, imgFrames2]) vidFromImgArray(imgFrames, "BoxAnim.mp4", 50, 800, 450)