Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue #9: added video support along with playback speed ;-) #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
*.pyc
env
77 changes: 72 additions & 5 deletions asciify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import cv2
import time
import utils

from PIL import Image
from asciimatics.screen import Screen

ASCII_CHARS = ['.',',',':',';','+','*','?','%','S','#','@']
ASCII_CHARS = ASCII_CHARS[::-1]
Expand All @@ -15,6 +20,7 @@ def resize(image, new_width=100):
new_dim = (new_width, new_height)
new_image = image.resize(new_dim)
return new_image

'''
method grayscalify():
- takes an image as a parameter
Expand Down Expand Up @@ -75,6 +81,51 @@ def runner(path):
f.write(image)
f.close()

'''
def play():
- Reads video from VideoCapture object cap
- And prints on the console screen provided by asciimatics
- Used asciimatics to overwrite lines at the same place
'''
def play(stdscr, cap, playback_speed=1):
stdscr.clear()

fps = cap.get(cv2.CAP_PROP_FPS)
delay = 1 / fps
delay = delay / playback_speed

retv, frame = cap.read()
while retv:
img = Image.fromarray(frame)
ascii_img_row_wise = do(img).split('\n')
rows = len(ascii_img_row_wise)

# need to print line by line because print_at does not respect '\n'
for i in range(0, rows):
stdscr.print_at(ascii_img_row_wise[i], 0, i)

stdscr.refresh()
time.sleep(delay)

retv, frame = cap.read()

'''
method play_video_wrapper():
- loads VideoCapture object and checks if video can be read
- accepts video file path and playback_speed (float)
'''
def play_video_wrapper(path, playback_speed):
cap = cv2.VideoCapture(path)

if cap.isOpened():

Screen.wrapper(play, arguments=(
cap, playback_speed))

cap.release()
else:
print("Could not read video file.")

'''
method main():
- reads input from console
Expand All @@ -83,9 +134,25 @@ def runner(path):
if __name__ == '__main__':
import sys
import urllib.request
if sys.argv[1].startswith('http://') or sys.argv[1].startswith('https://'):
urllib.request.urlretrieve(sys.argv[1], "asciify.jpg")
path = "asciify.jpg"

if len(sys.argv) < 3:
print("Incorrect input format.")
print("Syntax 1: python asciify.py -i IMAGE_FILE")
print("Syntax 2: python asciify.py -v VIDEO_FILE")
print("Syntax 3: python asciify.py -v VIDEO_FILE PLAYBACK_SPEED")
sys.exit(-1)

if sys.argv[1] not in ['-i', '-v']:
print("Invalid switch. Use -i for image and -v for video.")
sys.exit(-1)

path = utils.resolve(sys.argv[2])
if sys.argv[1] == '-i':
runner(path)
else:
path = sys.argv[1]
runner(path)
try:
playback_speed = float(sys.argv[3]) if len(sys.argv) > 3 else 1
play_video_wrapper(path, playback_speed)
except ValueError:
print("Invalid playback speed:", sys.argv[3])
sys.exit(-1)
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
asciimatics==1.10.0
future==0.16.0
numpy==1.15.2
opencv-python==3.4.3.18
Pillow==5.3.0
pyfiglet==0.7.5
wcwidth==0.1.7
Binary file added sample-vid.mp4
Binary file not shown.
16 changes: 16 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'''
def resolve():
- utility function that downloads an image/video file in case
it is a url.
- otherwise returns the path as it is.
'''
def resolve(path):
import urllib.request

resolved_path = path
if path.startswith('http://') or path.startswith('https://'):
filename = 'asciify-input-file.{}'.format(path.split('.')[-1])
urllib.request.urlretrieve(path, filename)
resolved_path = filename

return resolved_path