20
loading...
This website collects cookies to deliver better user experience
from PIL import Image
try:
with Image.open(<your_filename>) as img:
img = img.convert("RGB")
row, col = img.size
pixels = list(img.getdata())
except OSError:
print("Could not open file!")
ASCII_CHARS = "`^\",:;Il!i~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
MAX_PIXEL_VALUE = 255
symbol_index = int(intensity / MAX_PIXEL_VALUE * len(ASCII_CHARS)) - 1
intensity
above there is what we would compute the brightness to be from the RGB values (For the time being, let intensity = 10
). We subtract 1 to prevent overflow of indices above the length of the character string, and we use a simple modulo operation for the mapping.-1
turns up problem when the intensity
is zero. Because then it becomes -1, which means we map our most intense character to a zero intensity 😱symbol_index = symbol_index + 1 if symbol_index < 0 else symbol_index
img.resize(<length>, <width>)
. However, using a few libraries and a clever line of code means that the image could be resized based upon the terminal size.import sys
import os
size = os.get_terminal_size()
# new_rows, new_cols defined using image's aspect ratio and the terminal size
img - img.resize((new_cols, new_rows))
intensity = (pixel_red + pixel_blue + pixel_green) / 3
works, the human eye is most sensitive to green light, and least to blue. So a better intensity formula would beintensity = (0.299 * pixel_red_square + 0.587 * pixel_green_square + 0.114 * pixel_blue_square) ** 0.5
import cv2
vidcap = cv2.VideoCapture(<filename>)
i = 0 # a frame counter
frame_skip = 0 # to control the choppiness/frame rate
os.system("clear")
while vidcap.isOpened():
success, image = vidcap.read()
if not success:
break
if i > frame_skip - 1:
cv2.imwrite("frame.jpg", image)
i = 0
# a call the old function to ASCII render image 'frame.jpg'
continue
i += 1
vidcap.release()
image = cv2.convertScaleAbs(image, alpha=1.5, beta=50)
which increase contrast to 1.25 and brightness to 50% more."ESC[ 38;2;<r>;<g>;<b> m"
"ESC"
is actually "\033"
when using in code. (Since ASCII code of ESC is 033 in octal)print("\033[38;2;255;0;0m Hello \033[0m")
ESC[0m
ESC[ <line_number>;<column_number> H
ESC[1;1H
1
00:00:00,000 --> 00:00:01,000
Hey, want to party?
2
00:00:01,001 --> 00:01:00,123
(Tense music plays)
Hey, want to party?
should display, and then from 1.001 second (1 second, 001 millisecond) to 1 minute 123 millisecond, we should show the subtitle (Tense music plays)
Hours:Minutes:Second,Milliseconds
import cv2
vidcap = cv2.VideoCapture(<filename>)
sub_file = pysrt.open(<subtitle_srt_file>)
i = 0 # a frame counter
frame_skip = 0 # to control the choppiness/frame rate
os.system("clear")
while vidcap.isOpened():
success, image = vidcap.read()
if not success:
break
if i > frame_skip - 1:
cv2.imwrite("frame.jpg", image)
i = 0
timestamp = int(vidcap.get(cv2.CAP_PROP_POS_MSEC))
# a call the old function to ASCII render image 'frame.jpg'
# send timestamp & sub_file to a subtitle printing function
continue
i += 1
vidcap.release()
cv2.CAP_PROP_POS_MSEC
is an attribute that returns the current frame's time after start in milliseconds, we can directly use this in our subtitle printing function, as pysrt allows for a splice feature that selects subtitles for the specific timestamp.subs = sub_file.slice(starts_before={'milliseconds': timestamp}, ends_after={'milliseconds': timestamp})
for line in subs:
# print line (a string)