# -------------- CREATE DIAGONAL ENTRY AND EXIT ANIMATIONS---------------------- ''' This Python and OpenCV code is intended to create functions to generate the animations available in Microsoft PowerPoint. The first category of aniations are [Wipe, Split, Fly In, Float In, Rise Up, Fly Out, Float Down, Peek In, Peek Out]. All of these look similar and they differ in speed and direction of entrance. The other set is [ Shape, Wheel, Cicle, Box, Diamond ] where the image needs to be in non-orthogonal directions. The third set of animation is [Stretch, Compress, Zoom, Glow and Turn, Pin Wheel] - all of these operations are performed on entire image. The animations in PowerPoint are categories by Entrance, Emphasis and Exit. VideoWriter assumes color by default. VideoWriter has a 5th boolean parameter which one can set False to specify that the video is to be black and white. The the dimensions (width and height) that cv2 expects are the opposite of numpy. ''' #-------------------- USER INPUT STARTS ---------------------------------------- imgFile1 = 'Ubuntu1.jpg' imgFile2 = 'Ubuntu2.jpg' vidName1 = 'Vid-1.mp4' vidName2 = 'Vid-2.mp4' fps = 25 clr = (255, 255, 255) # -------------------- USER INPUT ENDS ----------------------------------------- import cv2, sys import numpy as np img1 = cv2.imread(imgFile1) img2 = cv2.imread(imgFile2) height, width, c = img1.shape def imgDiagonalUp(img, vidName, duration, fps, vid_w, vid_h, clr): #This is equivalent to animation Rise Up / Float Down in MS PowerPoint # .----------------. # | | # | | # | | # | \ _ . | # | \ /| | # | \ | # !................! ^^^^^^^^^^ # Direction of arrow indicates direction of ""APPEARANCE"" of image codec = cv2.VideoWriter_fourcc(*'mp4v') vid = cv2.VideoWriter(vidName, codec, fps, (vid_w, vid_h)) height, width, c = img.shape nframes = int(duration * fps) vid.write(img) #Split the 3D image in 3 different 2D arrays blu_chan = img[:, :, 0] grn_chan = img[:, :, 1] red_chan = img[:, :, 2] for i in range(height, 1, -1): #Setupper portion of matrix set to ZERO: black pixel blu_chan_diag = np.tril(blu_chan, -i) grn_chan_diag = np.tril(grn_chan, -i) red_chan_diag = np.tril(red_chan, -i) #Create mask to invert black pixels [0] to white [255] height, width = blu_chan.shape mask = np.ones([height, width]) * 255 mask = np.tril(mask, -i) inv_mask = 255 - mask blu_chan_diag = blu_chan_diag + inv_mask grn_chan_diag = grn_chan_diag + inv_mask red_chan_diag = red_chan_diag + inv_mask imgDiag = cv2.merge([blu_chan_diag, grn_chan_diag, red_chan_diag]) imgDiag = np.uint8(imgDiag) vid.write(imgDiag) for i in range(1, width, 1): #Setupper portion of matrix set to ZERO: black pixel blu_chan_diag = np.tril(blu_chan, i) grn_chan_diag = np.tril(grn_chan, i) red_chan_diag = np.tril(red_chan, i) #Create mask to invert black pixels [0] to white [255] height, width = blu_chan.shape mask = np.ones([height, width]) * 255 mask = np.tril(mask, i) inv_mask = 255 - mask blu_chan_diag = blu_chan_diag + inv_mask grn_chan_diag = grn_chan_diag + inv_mask red_chan_diag = red_chan_diag + inv_mask imgDiag = cv2.merge([blu_chan_diag, grn_chan_diag, red_chan_diag]) imgDiag = np.uint8(imgDiag) vid.write(imgDiag) vid.release() def imgDiagonalDn(img, vidName, duration, fps, vid_w, vid_h, clr): #This is equivalent to animation Rise Up / Float Down in MS PowerPoint # .----------------. # | \ | # | \ | # | |/ \ | # | '*' \ | # | | # | | # !................! ^^^^^^^^^^^^^ # Direction of arrow indicates direction of ""DISAPPEARANCE"" of image codec = cv2.VideoWriter_fourcc(*'mp4v') vid = cv2.VideoWriter(vidName, codec, fps, (vid_w, vid_h)) height, width, c = img.shape nframes = int(duration * fps) vid.write(img) #Split the 3D image in 3 different 2D arrays blu_chan = img[:, :, 0] grn_chan = img[:, :, 1] red_chan = img[:, :, 2] for i in range(width, 1, -1): #Setupper portion of matrix set to ZERO: black pixel blu_chan_diag = np.tril(blu_chan, i) grn_chan_diag = np.tril(grn_chan, i) red_chan_diag = np.tril(red_chan, i) #Create mask to invert black pixels [0] to white [255] height, width = blu_chan.shape mask = np.ones([height, width]) * 255 mask = np.tril(mask, i) inv_mask = 255 - mask blu_chan_diag = blu_chan_diag + inv_mask grn_chan_diag = grn_chan_diag + inv_mask red_chan_diag = red_chan_diag + inv_mask imgDiag = cv2.merge([blu_chan_diag, grn_chan_diag, red_chan_diag]) imgDiag = np.uint8(imgDiag) vid.write(imgDiag) for i in range(1, height, 1): #Setupper portion of matrix set to ZERO: black pixel blu_chan_diag = np.tril(blu_chan, -i) grn_chan_diag = np.tril(grn_chan, -i) red_chan_diag = np.tril(red_chan, -i) #Create mask to invert black pixels [0] to white [255] height, width = blu_chan.shape mask = np.ones([height, width]) * 255 mask = np.tril(mask, -i) inv_mask = 255 - mask blu_chan_diag = blu_chan_diag + inv_mask grn_chan_diag = grn_chan_diag + inv_mask red_chan_diag = red_chan_diag + inv_mask imgDiag = cv2.merge([blu_chan_diag, grn_chan_diag, red_chan_diag]) imgDiag = np.uint8(imgDiag) vid.write(imgDiag) vid.release() # ------------------------------------------------------------------------------ vid_h = height vid_w = width imgDiagonalUp(img1, vidName1, 5, fps, vid_w, vid_h, clr) imgDiagonalDn(img1, vidName2, 5, fps, vid_w, vid_h, clr)