11from toascii import Video , GrayscaleConverter , ConverterOptions
2- from toascii .gradients import OXXO
2+ from toascii .gradients import BLOCK , HIGH , LOW , OXXO
33import numpy as np
44
55from argparse import ArgumentParser
66from typing import Generator
77from pathlib import Path
88
9+ GRADIENTS = {
10+ "block" : BLOCK ,
11+ "high" : HIGH ,
12+ "low" : LOW ,
13+ "oxxo" : OXXO
14+ }
15+
916class CustomConverter (GrayscaleConverter ):
1017 def _asciify_image (self , image : np .ndarray ) -> Generator [bytes , None , None ]:
1118 g_l_m = len (self .options .gradient ) - 1
@@ -14,25 +21,17 @@ def _asciify_image(self, image: np.ndarray) -> Generator[bytes, None, None]:
1421 for b , g , r in row :
1522 yield self .options .gradient [int ((self ._luminosity (r , g , b ) / 255 ) * g_l_m )]
1623
17- def get_filling_amount (size : int ) -> int :
18- amount = 0
19-
20- while amount < size :
21- amount += 512
22-
23- return amount - size + 1
24-
25- def main (input_path : Path , output_path : Path , width : int = 80 , height : int = 25 ):
24+ def main (input_path : Path , output_path : Path , gradient : str = "oxxo" , width : int = 80 , height : int = 25 ):
2625 if not input_path .exists ():
2726 raise FileNotFoundError (f"input path `{ input_path } ` does not exist" )
2827
29- options = ConverterOptions (gradient = OXXO , width = width , height = height )
28+ options = ConverterOptions (gradient = GRADIENTS [ gradient ] , width = width , height = height )
3029 video = Video (str (input_path ), converter = CustomConverter (options ))
3130
3231 size = width * height
33- filling = b" \0 " * get_filling_amount (size )
32+ filling = 512 - (size % 512 ) + 1
3433
35- print (f"Frame size: { size } bytes ({ round (size / 512 )} sectors, with { len ( filling ) } bytes of filling)" )
34+ print (f"Frame size: { size } bytes ({ round (size / 512 )} sectors, with { filling } bytes of filling)" )
3635 print (f"Terminal size: { width } x{ height } (width x height)" )
3736
3837 data = b""
@@ -41,9 +40,10 @@ def main(input_path: Path, output_path: Path, width: int = 80, height: int = 25)
4140 if index == 1 :
4241 length = video .source .frame_count
4342
44- data += text .encode ("utf-8" )[:- 1 ] + filling
43+ data += text .encode ("utf-8" )[:- 1 ] + ( b" \0 " * filling )
4544
46- print (f"Frame { index } /{ length } ({ round (index / length * 100 , 2 )} %)" , end = "\r " )
45+ print (f"Frame { index } /{ length } " , end = " " )
46+ print (f"({ round (index / length * 100 , 2 )} %)" , end = "\r " )
4747
4848 with open (output_path , "wb" ) as file :
4949 file .write (data )
@@ -53,6 +53,9 @@ def main(input_path: Path, output_path: Path, width: int = 80, height: int = 25)
5353
5454 parser .add_argument ("input_path" , type = Path , help = "The input path to the video file." )
5555 parser .add_argument ("--output_path" , "-o" , type = Path , help = "The output path to the data file." , default = Path ("video.bin" ))
56+
57+ parser .add_argument ("--gradient" , type = str , help = "The character pattern to use with the ASCII stream." , choices = GRADIENTS .keys (), default = "oxxo" )
58+
5659 parser .add_argument ("--width" , type = int , help = "The width of the terminal." , default = 80 )
5760 parser .add_argument ("--height" , type = int , help = "The height of the terminal." , default = 25 )
5861
0 commit comments