#-------------- ------------------ ------------------- --------------------- --- #Numpy and OpenCV to create a PowerPoint like animation of an 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 the edges and from Top-Left corner to #the Bottom-Right corner. This code is a 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 imgAnimBoxEntrance(img, fps): #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 cv2.imshow("Masked", cv2.bitwise_or(img, imgW, mask=None)) t = int(1000/fps) cv2.waitKey(t) #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 cv2.imshow("Masked", cv2.bitwise_or(img, imgW, mask = None)) t = int(1000/fps) cv2.waitKey(t) cv2.destroyAllWindows() #-------------- ------------------ ------------------- --------------------- --- def imgPptAnimWipeTL2BR(img, fps): #This function works only for coloured images with no Alpha channel img = cv2.imread(img) if len(img.shape) < 3 or len(img.shape) > 3: print("Image is either grayscale or coloured with Alpha channel") print("Image not process, exited normally!") sys.exit() h_half = img.shape[0]//2 w_half = img.shape[1]//2 even = img.shape[0] % 2 arat = img.shape[1]/img.shape[0] for i in range(1, h_half, 1): j = int(i * arat) #Box Wipe - TL Corner towards BR corner img[0:i*2+2, 0:j*2+2, :] = 255 cv2.imshow("Image", img) t = int(1000/fps) cv2.waitKey(t) cv2.destroyAllWindows() #-------------- ------------------ ------------------- --------------------- --- def imgPptAnimWipeCen2Edge(img, fps): #This function works only for coloured images with no Alpha channel img = cv2.imread(img) if len(img.shape) < 3 or len(img.shape) > 3: print("Image is either grayscale or coloured with Alpha channel") print("Image not process, exited normally!") sys.exit() h_half = img.shape[0]//2 w_half = img.shape[1]//2 even = img.shape[0] % 2 arat = img.shape[1]/img.shape[0] for i in range(1, h_half, 1): j = int(i * arat) #Box Wipe - Centre towards outwards img[h_half-i:h_half+i, w_half-j:w_half+j, :] = 255 cv2.imshow("Image", img) t = int(1000/fps) cv2.waitKey(t) cv2.destroyAllWindows() #-------------- ------------------ ------------------- --------------------- --- imgPptAnimWipeTL2BR("3.jpg", 20) imgPptAnimWipeCen2Edge("3.jpg", 20) imgAnimBoxEntrance("3.jpg", 20)