#----------------------IMAGE to VIDEO by ROTATION and PADDING------------------- #This Python and OpenCV code contains function to rotate an image, pad rotated #image to resize it to width and height of the desired video. The colour of un #defined region in rotation and padding can be defined separately. There is a #check if the diagonal length of input image exceeed the height or width of the #video size and images are not rotated or padded- hence no video is created. # import numpy as np import cv2 import math, os, sys # # -------------------------User Input Starts------------------------------------ imgName = 'Sample.jpg' vidName = 'imgRot.mp4' vidH = 450 vidW = 800 nfs = 11 #Color for image rotation (warpAffine) and padding (copyMakeBorder) clrRot = (255, 255, 255) clrPad = (255, 255, 255) #Rotation angle increment and total rotation angle (starts with q = 0) delta_q = 2 total_q = 180 # ------------------------User Input Ends--------------------------------------- def rotateImage(img, angleInDeg, vid_w, vid_h, clr): h = img.shape[0] w = img.shape[1] #This is a basic check assuming rotation angle > 45 degrees diagLen = int(math.sqrt(h*h + w*w)) if (int(diagLen) > vid_w or int(diagLen) > vid_h): print("Size of rotated image shall exceed height or width of Video") print( "No image rotation performed. \n") sys.exit() img_c = (w/2, h/2) rot_mtx = cv2.getRotationMatrix2D(img_c, angleInDeg, 1) rad = math.radians(angleInDeg) sin = math.sin(rad) cos = math.cos(rad) b_w = int((h * abs(sin)) + (w * abs(cos))) b_h = int((h * abs(cos)) + (w * abs(sin))) rs = (b_w, b_h) rot_mtx [0, 2] = rot_mtx [0, 2] + ((b_w/2) - img_c[0]) rot_mtx [1, 2] = rot_mtx [1, 2] + ((b_h/2) - img_c[1]) bm = cv2.BORDER_CONSTANT fg = cv2.INTER_LINEAR rImg=cv2.warpAffine(img,rot_mtx,rs, borderMode=bm,borderValue=clr,flags=fg) return rImg #------------------------------------------------------------------------------- def paddedImage(img, vid_w, vid_h, clr): delta_w = vid_w - img.shape[1] delta_h = vid_h - img.shape[0] if delta_w < 0 or delta_h < 0: print("Specified video size< input image, no video created. \n") sys.exit() pad_w = delta_w // 2 pad_h = delta_h // 2 top, bot = pad_h, delta_h - pad_h lft, rgt = pad_w, delta_w - pad_w #Copy img image into center of result image brdr = cv2.BORDER_CONSTANT padimg = cv2.copyMakeBorder(img, top,bot, lft,rgt, brdr, value=clr) return padimg #------------------------------------------------------------------------------- def rotPadImg2Vid(imgName, del_q,q_tot, vidName,fps, vid_w,vid_h, c_rot,c_pad): if not os.path.exists(imgName): print("Specified Image File does not Exist. \n") sys.exit() img = cv2.imread(imgName) codec = cv2.VideoWriter_fourcc(*'mp4v') vid = cv2.VideoWriter(vidName, codec, nfs, (vid_w, vid_h)) n = int(q_tot / del_q + 1) for i in range(0, n): angle = i * delta_q rotImg = rotateImage(img, angle, vid_w, vid_h, c_rot) padImg = paddedImage(rotImg, vid_w, vid_h, c_pad) vid.write(padImg) vid.release() #------------------------------------------------------------------------------- #Create video using Function rotPadImg2Vid(imgName, delta_q,total_q, vidName,nfs, vidW,vidH, clrRot,clrPad) # ''' #Create video using inline command codec= cv2.VideoWriter_fourcc(*'mp4v') img = cv2.imread(imgName) vid = cv2.VideoWriter(vidName, codec, nfs, (vidW, vidH)) n = int(total_q / delta_q + 1) for i in range(0, n): angle= i * delta_q rotImg = rotateImage(img, angle, clrRot, vidW, vidH) padImg = paddedImage(rotImg, vidW, vidH, clrPad) vid.write(padImg) vid.release() cv2.destroyA11Windows() '''