-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.py
79 lines (70 loc) · 1.6 KB
/
resize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import cv2
import sys
import glob
import os
import shutil
if (len(sys.argv)!=3):
print("\nInsufficient Arguments")
sys.exit()
parent=os.getcwd()
new="Frames"
dirpath=os.path.join(parent,new)
os.mkdir(dirpath)
newconv="ConvFrames"
dirpathconv=os.path.join(parent,newconv)
os.mkdir(dirpathconv)
print(dirpathconv)
v_name=sys.argv[1]
vid = cv2.VideoCapture(f'{v_name}')
fps=vid.get(cv2.CAP_PROP_FPS)
print(fps)
check,image = vid.read()
count = 0
while check:
cv2.imwrite(f"{dirpath}/frame{count}.jpg", image)
check,image = vid.read()
print(f'Frame number {count}: {check}')
count += 1
print("\nPlease Wait!!")
t=0
try:
prc=100-int(sys.argv[2])
except:
print("\n Insufficient Arguments : Resize Value not provided")
sys.exit()
while(t<count):
img=cv2.imread(f"{dirpath}/frame{t}.jpg",cv2.IMREAD_UNCHANGED)
hnew=int(img.shape[0]*(prc/100))
wnew=int(img.shape[1]*(prc/100))
snew=(wnew,hnew)
nimg=cv2.resize(img,snew)
cv2.imwrite(f"{dirpathconv}/Conv{t}.jpg",nimg)
t=t+1
vimages=[]
img=glob.glob(f"{dirpathconv}/*.jpg")
def srtcrt(a):
x=len(dirpathconv)
return(int(a[(x+5):-4]))
img.sort(key=srtcrt)
for i in img:
print(i)
for i in img:
img1=cv2.imread(i)
h,w,layers=img1.shape
size=(w,h)
vimages.append(img1)
v_name='vid_output'
try:
vid=cv2.VideoWriter(f'{parent}/{v_name}.mp4',cv2.VideoWriter_fourcc(*'mp4v'),fps,size)
except:
print("File not found")
os.rmdir(dirpath)
os.rmdir(dirpathconv)
sys.exit()
for i in range(len(vimages)):
vid.write(vimages[i])
vid.release()
shutil.rmtree(dirpath)
shutil.rmtree(dirpathconv)
print("DONE")
print(f"OUTPUT : {v_name}")