-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconvert.py
41 lines (37 loc) · 1.32 KB
/
convert.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
import numpy as np
import sys
import cv2
from PIL import Image
def video_to_frame(videofile):
video_cap = cv2.VideoCapture(videofile)
frame_count = 0
while (video_cap.isOpened()):
print('frame %d' % frame_count)
ret, frame = video_cap.read()
if ret is False:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imwrite("frame%d.png" % frame_count, frame)
frame_count += 1
return frame_count
def resize_frame(img):
return Image.open(img, 'r').convert("L").resize(size)
def frame_to_char(frame_num):
gray_char=['@','#','$','%','&','?','*','o','/','{','[','(',
'|','!','^','~','-','_',':',';',',','.','`',' ']
text = open("text1.txt", "w")
for i in range(frame_num):
frame = resize_frame('frame%d.png' % i)
for y in range(size[1]):
for x in range(size[0]):
gray = frame.getpixel((x,y))
char = gray_char[int(gray/(255/(len(gray_char)-1)))]
text.write(char)
text.write("\n")
print("writing frame %d"%i)
text.close()
print('Done!')
if __name__ == '__main__':
size = (236, 61) #size of output frame (x,y)
nframe = video_to_frame(sys.argv[1]) #video to convert
frame_to_char(nframe)