# -------------- CREATE PAN / ZOOM / FLY-IN / FLY-OUT 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. NOTE: The code works ONLY when the width of images are EVEN numbers ''' #-------------------- USER INPUT STARTS ---------------------------------------- imgFile1 = 'Ubuntu1.jpg' imgFile2 = 'Ubuntu2.jpg' vidName1 = 'imgAnimateL2R.mp4' vidName2 = 'imgAnimateR2L.mp4' fps = 25 clr = (255, 255, 255) # -------------------- USER INPUT ENDS ----------------------------------------- import cv2 import numpy as np img1 = cv2.imread(imgFile1) img2 = cv2.imread(imgFile2) height, width, c = img1.shape def imgPanLeft2Right(img, vidName, duration, fps, vid_w, vid_h, clr): #This is equivalent to animation Wipe or Fly In or Peek 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) for i in range(0, nframes+1, 1): #crop_img = img[y:y+h, x:x+w] crop_width = int(i/nframes * width) rt_crop = img[0:height, 0:crop_width] padR = vid_w - rt_crop.shape[1] #top,bot, lft,rgt brdr = cv2.BORDER_CONSTANT padImg = cv2.copyMakeBorder(rt_crop, 0,0, 0,padR, brdr, value=clr) vid.write(padImg) vid.release() def imgPanRight2Left(img, vidName, duration, fps, vid_w, vid_h, clr): #This is equivalent to animation Wipe or Fly In or Peek 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) for i in range(0, nframes+1, 1): #crop_img = img[y:y+h, x:x+w] crop_width = int(width-i/nframes * width) lt_crop = img[0:height, crop_width:width] padL = vid_w - lt_crop.shape[1] #top,bot, lft,rgt brdr = cv2.BORDER_CONSTANT padImg = cv2.copyMakeBorder(lt_crop, 0,0, padL,0, brdr, value=clr) vid.write(padImg) vid.release() def imgFlyInCentreToEdge(img1, img2, vidName, duration, fps, vid_w, vid_h, clr): #This is equivalent to animation Split in MS PowerPoint # .---------------. # | /___||__\ | # | \ || / | # !...............! # Directions of arrows indicate direction of appearance of image #Height of img1 and img2 = vid_h, no checks performed to ensure this codec = cv2.VideoWriter_fourcc(*'mp4v') vid = cv2.VideoWriter(vidName, codec, fps, (vid_w, vid_h)) height, width, c = img1.shape nframes = int(duration * fps) img0 = cv2.hconcat([img1, img2]) vid.write(img0) for i in range(0, nframes+1, 1): #crop_img = img[y:y+h, x:x+w] crop_width = int(width-i/nframes * width) lt_crop = img1[0:height, crop_width:width] padL = int(vid_w/2) - lt_crop.shape[1] #top,bot, lft,rgt brdr = cv2.BORDER_CONSTANT padImgL = cv2.copyMakeBorder(lt_crop, 0,0, padL,0, brdr, value=clr) rt_crop = img2[0:height, crop_width:width] padR = int(vid_w/2) - rt_crop.shape[1] #top,bot, lft,rgt brdr = cv2.BORDER_CONSTANT padImgR = cv2.copyMakeBorder(rt_crop, 0,0, 0,padR, brdr, value=clr) imgFlyIn = cv2.hconcat([padImgL, padImgR]) vid.write(imgFlyIn) vid.release() def imgFlyInEdgeToCentre(img1, img2, vidName, duration, fps, vid_w, vid_h, clr): #This is equivalent to animation Split (Entrance) in MS PowerPoint # .---------------. # | ||__\ /__|| | # | || / \ || | # !...............! # Directions of arrows indicate direction of APPEARANCE of image(s) #Height of img1 and img2 = vid_h, no checks performed to ensure this codec = cv2.VideoWriter_fourcc(*'mp4v') vid = cv2.VideoWriter(vidName, codec, fps, (vid_w, vid_h)) height, width, c = img1.shape nframes = int(duration * fps) img0 = cv2.hconcat([img1, img2]) vid.write(img0) for i in range(0, nframes+1, 1): #crop_img = img[y:y+h, x:x+w] crop_width = int(i/nframes * width) lt_crop = img1[0:height, 0:crop_width] padR = int(vid_w/2) - lt_crop.shape[1] #top,bot, lft,rgt brdr = cv2.BORDER_CONSTANT padImgL = cv2.copyMakeBorder(lt_crop, 0,0, 0,padR, brdr, value=clr) rt_crop = img2[0:height, 0:crop_width] padL = int(vid_w/2) - rt_crop.shape[1] #top,bot, lft,rgt brdr = cv2.BORDER_CONSTANT padImgR = cv2.copyMakeBorder(rt_crop, 0,0, padL,0, brdr, value=clr) imgFlyIn = cv2.hconcat([padImgL, padImgR]) vid.write(imgFlyIn) vid.release() def imgWipeOutEdgeToCentre(img1, img2, vidName, duration, fps, vid_w, vid_h, clr): #This is equivalent to animation Split (Exit) in MS PowerPoint # .---------------. # | ||__\ /__|| | # | || / \ || | # !...............! # Directions of arrows indicate direction of DIS-APPEARANCE of image(s) #Height of img1 and img2 = vid_h, no checks performed to ensure this codec = cv2.VideoWriter_fourcc(*'mp4v') vid = cv2.VideoWriter(vidName, codec, fps, (vid_w, vid_h)) height, width, c = img1.shape nframes = int(duration * fps) img0 = cv2.hconcat([img1, img2]) vid.write(img0) for i in range(0, nframes+1, 1): #crop_img = img[y:y+h, x:x+w] crop_width = int(width-i/nframes * width) lt_crop = img1[0:height, 0:crop_width] padL = int(vid_w/2) - lt_crop.shape[1] #top,bot, lft,rgt brdr = cv2.BORDER_CONSTANT padImgL = cv2.copyMakeBorder(lt_crop, 0,0, padL,0, brdr, value=clr) rt_crop = img2[0:height, 0:crop_width] padR = int(vid_w/2) - rt_crop.shape[1] #top,bot, lft,rgt brdr = cv2.BORDER_CONSTANT padImgR = cv2.copyMakeBorder(rt_crop, 0,0, 0,padR, brdr, value=clr) imgFlyIn = cv2.hconcat([padImgL, padImgR]) vid.write(imgFlyIn) vid.release() # ------------------------------------------------------------------------------ vid_w = 2*width imgPanLeft2Right(img1, vidName1, 5, fps, vid_w, height, clr) imgPanRight2Left(img1, vidName2, 5, fps, vid_w, height, clr) imgFlyInCentreToEdge(img1, img2, 'Vid-3.mp4', 5, fps, vid_w, height, clr) imgFlyInEdgeToCentre(img1, img2, 'Vid-4.mp4', 5, fps, vid_w, height, clr) imgWipeOutEdgeToCentre(img1, img2, 'Vid-5.mp4', 5, fps, vid_w, height, clr)