#-------------- CREATE DIAGONAL ENTRY AND EXIT ANIMATIONS----------------------- #This code creates animation from Top-Left to Bottom-Right corner and vice-versa ''' 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 = 100 # -------------------- USER INPUT ENDS ----------------------------------------- import cv2, sys import numpy as np img1 = cv2.imread(imgFile1) img2 = cv2.imread(imgFile2) height, width, c = img1.shape #np.tril(Arr): Returns a copy of Arr with elements above k-th diagonal zeroed #np.triu(Arr): Returns a copy of Arr with elements below k-th diagonal zeroed def imgDiagonalWipeTL2BR(img, vidName, duration, fps, vid_w, vid_h): #This is partly equivalent to animation Diamond 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)) 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] #print(blu_chan[1,:]) height, width = blu_chan.shape for i in range(width, 1, -1): #Create mask to invert black pixels [0] to white [255] mask_img = np.ones([height, width])*255 mask_img = np.tril(mask_img, i) #i > 0, i-th diagonal above main mask_flip = np.fliplr(mask_img) inv_mask = 255 - mask_flip blu_chan_flip = np.fliplr(blu_chan) blu_chan_diag = np.tril(blu_chan_flip, i) grn_chan_flip = np.fliplr(grn_chan) grn_chan_diag = np.tril(grn_chan_flip, i) red_chan_flip = np.fliplr(red_chan) red_chan_diag = np.tril(red_chan_flip, i) blu_chan_diag = np.fliplr(blu_chan_diag) + inv_mask grn_chan_diag = np.fliplr(grn_chan_diag) + inv_mask red_chan_diag = np.fliplr(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): #Create mask to invert black pixels [0] to white [255] mask_img = np.ones([height, width]) * 255 mask_img = np.tril(mask_img, -i) #i < 0, i-th diagonal below main mask_flip = np.fliplr(mask_img) inv_mask = 255 - mask_flip #Set upper portion of matrix set to ZERO: black pixel blu_chan_flip = np.fliplr(blu_chan) blu_chan_diag = np.tril(blu_chan_flip, -i) grn_chan_flip = np.fliplr(grn_chan) grn_chan_diag = np.tril(grn_chan_flip, -i) red_chan_flip = np.fliplr(red_chan) red_chan_diag = np.tril(red_chan_flip, -i) blu_chan_diag = np.fliplr(blu_chan_diag) + inv_mask grn_chan_diag = np.fliplr(grn_chan_diag) + inv_mask red_chan_diag = np.fliplr(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 imgDiagonalFlyinTL2BR(img, vidName, duration, fps, vid_w, vid_h): #This is partly equivalent to animation Diamond 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)) 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] #print(blu_chan[1,:]) height, width = blu_chan.shape for i in range(width, 1, -1): #Create mask to invert black pixels [0] to white [255] mask_img = np.ones([height, width]) * 255 mask_img = np.triu(mask_img, i) mask_flip = np.fliplr(mask_img) inv_mask = 255 - mask_flip #Set upper portion of matrix set to ZERO: black pixel blu_chan_flip = np.fliplr(blu_chan) blu_chan_diag = np.triu(blu_chan_flip, i) grn_chan_flip = np.fliplr(grn_chan) grn_chan_diag = np.triu(grn_chan_flip, i) red_chan_flip = np.fliplr(red_chan) red_chan_diag = np.triu(red_chan_flip, i) blu_chan_diag = np.fliplr(blu_chan_diag) + inv_mask grn_chan_diag = np.fliplr(grn_chan_diag) + inv_mask red_chan_diag = np.fliplr(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): #Create mask to invert black pixels [0] to white [255] mask_img = np.ones([height, width])*255 mask_img = np.triu(mask_img, -i) mask_flip = np.fliplr(mask_img) inv_mask = 255 - mask_flip blu_chan_flip = np.fliplr(blu_chan) blu_chan_diag = np.triu(blu_chan_flip, -i) grn_chan_flip = np.fliplr(grn_chan) grn_chan_diag = np.triu(grn_chan_flip, -i) red_chan_flip = np.fliplr(red_chan) red_chan_diag = np.triu(red_chan_flip, -i) blu_chan_diag = np.fliplr(blu_chan_diag) + inv_mask grn_chan_diag = np.fliplr(grn_chan_diag) + inv_mask red_chan_diag = np.fliplr(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 imgDiagonalFlyinBR2TL(img, vidName, duration, fps, vid_w, vid_h): #This is partly equivalent to animation Diamond 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)) 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] #print(blu_chan[1,:]) height, width = blu_chan.shape for i in range(height, 1, -1): #Create mask to invert black pixels [0] to white [255] mask_img = np.ones([height, width]) * 255 mask_img = np.tril(mask_img, -i) mask_flip = np.fliplr(mask_img) inv_mask = 255 - mask_flip #Set upper portion of matrix set to ZERO: black pixel blu_chan_flip = np.fliplr(blu_chan) blu_chan_diag = np.tril(blu_chan_flip, -i) grn_chan_flip = np.fliplr(grn_chan) grn_chan_diag = np.tril(grn_chan_flip, -i) red_chan_flip = np.fliplr(red_chan) red_chan_diag = np.tril(red_chan_flip, -i) blu_chan_diag = np.fliplr(blu_chan_diag) + inv_mask grn_chan_diag = np.fliplr(grn_chan_diag) + inv_mask red_chan_diag = np.fliplr(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): #Create mask to invert black pixels [0] to white [255] mask_img = np.ones([height, width])*255 mask_img = np.tril(mask_img, i) mask_flip = np.fliplr(mask_img) inv_mask = 255 - mask_flip blu_chan_flip = np.fliplr(blu_chan) blu_chan_diag = np.tril(blu_chan_flip, i) grn_chan_flip = np.fliplr(grn_chan) grn_chan_diag = np.tril(grn_chan_flip, i) red_chan_flip = np.fliplr(red_chan) red_chan_diag = np.tril(red_chan_flip, i) blu_chan_diag = np.fliplr(blu_chan_diag) + inv_mask grn_chan_diag = np.fliplr(grn_chan_diag) + inv_mask red_chan_diag = np.fliplr(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 imgDiagonalWipeBR2TL(img, vidName, duration, fps, vid_w, vid_h): #This is partly equivalent to animation Diamond 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)) 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] #print(blu_chan[1,:]) height, width = blu_chan.shape #np.tril(Arr): elements above i-th diagonal zeroed #np.triu(Arr): elements below i-th diagonal zeroed for i in range(height, 1, -1): #Create mask to invert black pixels [0] to white [255] mask_img = np.ones([height, width]) * 255 mask_img = np.triu(mask_img, -i) #i < 0, i-th diagonal below main mask_flip = np.fliplr(mask_img) inv_mask = 255 - mask_flip #Set upper portion of matrix set to ZERO: black pixel blu_chan_flip = np.fliplr(blu_chan) blu_chan_diag = np.triu(blu_chan_flip, -i) grn_chan_flip = np.fliplr(grn_chan) grn_chan_diag = np.triu(grn_chan_flip, -i) red_chan_flip = np.fliplr(red_chan) red_chan_diag = np.triu(red_chan_flip, -i) blu_chan_diag = np.fliplr(blu_chan_diag) + inv_mask grn_chan_diag = np.fliplr(grn_chan_diag) + inv_mask red_chan_diag = np.fliplr(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): #Create mask to invert black pixels [0] to white [255] mask_img = np.ones([height, width])*255 mask_img = np.triu(mask_img, i) mask_flip = np.fliplr(mask_img) inv_mask = 255 - mask_flip blu_chan_flip = np.fliplr(blu_chan) blu_chan_diag = np.triu(blu_chan_flip, i) grn_chan_flip = np.fliplr(grn_chan) grn_chan_diag = np.triu(grn_chan_flip, i) red_chan_flip = np.fliplr(red_chan) red_chan_diag = np.triu(red_chan_flip, i) blu_chan_diag = np.fliplr(blu_chan_diag) + inv_mask grn_chan_diag = np.fliplr(grn_chan_diag) + inv_mask red_chan_diag = np.fliplr(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 #imgDiagonalWipeTL2BR(img1, vidName1, 5, fps, vid_w, vid_h) #imgDiagonalFlyinTL2BR(img1, vidName1, 5, fps, vid_w, vid_h) #imgDiagonalFlyinBR2TL(img1, vidName2, 5, fps, vid_w, vid_h) imgDiagonalWipeBR2TL(img1, vidName2, 5, fps, vid_w, vid_h)