#-------------- ------------------ ------------------- --------------------- --- #Numpy and OpenCV to create a PowerPoint like animation of image. This is an #example of appearance of an image in small rectangular boxes. #-------------- ------------------ ------------------- --------------------- --- import numpy as np import sys import cv2 #-------------- ------------------ ------------------- --------------------- --- def vidFromImgArray(imgArray, vidName, fps=50, 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 imgWipeDiagonalBox(img, wBox): #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) ht, wd, ch = img.shape #Number of segments in the width direction nw = wd // wBox #Dimension of each box in the height direction nh = ht // wBox for k in range(1, wBox, 1): imgW = np.ones([ht, wd, ch], dtype=np.uint8) * 255 for i in range(0, nw, 1): w1 = i * wBox + k w2 = (i+1) * wBox for j in range(0, nh, 1): h1 = j * wBox + k h2 = (j+1) * wBox imgW[h1:h2, w1:w2, :] = 0 cropImg = cv2.bitwise_or(img, imgW, mask=None) frames.append(cropImg) return frames #-------------- ------------------ ------------------- ------------------------ def imgWipeUniformBox(img, wBox): #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) ht, wd, ch = img.shape #Number of segments in the width direction nw = wd // wBox #Dimension of each box in the height direction nh = ht // wBox for k in range(1, wBox, 1): imgW = np.ones([ht, wd, ch], dtype=np.uint8) * 255 for i in range(0, nw, 1): w1 = i * wBox + k w2 = (i+1) * wBox - k - 1 for j in range(0, nh, 1): h1 = j * wBox + k h2 = (j+1) * wBox - k - 1 imgW[h1:h2, w1:w2, :] = 0 cropImg = cv2.bitwise_or(img, imgW, mask=None) frames.append(cropImg) return frames #-------------- ------------------ ------------------- ------------------------ def imgAppearUniformBox(img, wBox): #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) ht, wd, ch = img.shape #Number of segments in the width direction nw = wd // wBox #Dimension of each box in the height direction nh = ht // wBox for k in range(1, wBox, 1): imgW = np.ones([ht, wd, ch], dtype=np.uint8) * 255 for i in range(0, nw, 1): w1 = i * wBox w2 = w1 + k w4 = (i+1) * wBox w3 = w4 - k for j in range(0, nh, 1): h1 = j * wBox h2 = h1 + k h4 = (j+1) * wBox h3 = h4 - k imgW[h1:h2, w1:w2, :] = 0 imgW[h3:h4, w1:w2, :] = 0 imgW[h1:h2, w3:w4, :] = 0 imgW[h3:h4, w3:w4, :] = 0 cropImg = cv2.bitwise_or(img, imgW, mask=None) frames.append(cropImg) return frames #-------------- ------------------ ------------------- ------------------------ img = "Lena-with-Roses.jpg" imgArray1 = imgWipeDiagonalBox(img, 50) imgArray2 = imgWipeUniformBox(img, 50) imgArray3 = imgAppearUniformBox(img, 50) imgFrames = np.concatenate([imgArray1, imgArray2, imgArray3]) vidFromImgArray(imgFrames, "imgBoxSegments.mp4", 20)