Skip to content

Commit f98954e

Browse files
committed
refactor some stuff in the converter
1 parent bf183d2 commit f98954e

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ EXECUTABLE = bootloader.bin
1515

1616
VIDEO_PATH = video.flv
1717

18+
ASCII_GRADIENT = oxxo
19+
1820
# targets
1921
all: $(BUILD_DIR)/$(EXECUTABLE)
2022

@@ -36,4 +38,5 @@ $(BUILD_DIR)/code.bin: $(SOURCES)
3638
$(BUILD_DIR)/data.bin: $(VIDEO_PATH)
3739
mkdir -p $(BUILD_DIR)
3840

39-
$(PYTHON) $(SRC_DIR)/vid2data/main.py $< -o $@
41+
$(PYTHON) $(SRC_DIR)/converter/main.py $< -o $@ \
42+
--gradient $(ASCII_GRADIENT)
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from toascii import Video, GrayscaleConverter, ConverterOptions
2-
from toascii.gradients import OXXO
2+
from toascii.gradients import BLOCK, HIGH, LOW, OXXO
33
import numpy as np
44

55
from argparse import ArgumentParser
66
from typing import Generator
77
from pathlib import Path
88

9+
GRADIENTS = {
10+
"block": BLOCK,
11+
"high": HIGH,
12+
"low": LOW,
13+
"oxxo": OXXO
14+
}
15+
916
class 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

Comments
 (0)