Skip to content

Commit de45b38

Browse files
committed
Add SVG generation
1 parent 03a4282 commit de45b38

14 files changed

+7653
-39
lines changed

parse_qwantz/cli.py

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Inner:
2222
debug: bool
2323
show_boxes: bool
2424
unambiguous_words: bool
25+
generate_svg: bool
2526

2627
def __call__(self, image_path: Path):
2728
try:
@@ -31,6 +32,7 @@ def __call__(self, image_path: Path):
3132
debug=self.debug,
3233
show_boxes=self.show_boxes,
3334
unambiguous_words=self.unambiguous_words,
35+
svg=self.generate_svg,
3436
)
3537
except ImageError:
3638
pass
@@ -44,6 +46,7 @@ def cli(
4446
debug: bool = typer.Option(False, help="Enable debug features."),
4547
show_boxes: bool = typer.Option(False, help="Show character boxes (for debug)"),
4648
unambiguous_words: bool = typer.Option(False, help="Print only unambiguous words"),
49+
generate_svg: bool = typer.Option(False, help="Generate SVG file"),
4750
):
4851
"""Generate transcripts for Ryan North's Dinosaur Comics from https://qwantz.com"""
4952
set_logging_formatter()
@@ -60,6 +63,7 @@ def cli(
6063
debug=debug,
6164
show_boxes=show_boxes,
6265
unambiguous_words=unambiguous_words,
66+
generate_svg=generate_svg,
6367
)
6468

6569
with Pool(4) as pool:

parse_qwantz/colors.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1-
from enum import Enum
2-
from typing import Optional
1+
from typing import Optional, NamedTuple
32

43
COLOR_THRESHOLD = 250
54

65

7-
class Color(Enum):
8-
WHITE = (255, 255, 255)
9-
BLACK = (0, 0, 0)
10-
RED = (128, 0, 0)
6+
class Color(NamedTuple):
7+
red: int
8+
green: int
9+
blue: int
1110

12-
@classmethod
13-
def get_with_threshold(cls, rgb: tuple[int, int, int]) -> Optional["Color"]:
14-
for color in cls:
15-
if square_distance(color.value, rgb) < COLOR_THRESHOLD:
16-
return color
11+
12+
WHITE = Color(255, 255, 255)
13+
BLACK = Color(0, 0, 0)
14+
RED = Color(128, 0, 0)
15+
16+
17+
def get_color_with_threshold(rgb: tuple[int, int, int]) -> Optional["Color"]:
18+
for color in [WHITE, BLACK, RED]:
19+
if square_distance(color, rgb) < COLOR_THRESHOLD:
20+
return color
1721

1822

1923
def square_distance(rgb1: tuple[int, int, int], rgb2: tuple[int, int, int]):

parse_qwantz/data/panel_overrides.json

+4
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,10 @@
978978
"panels": {
979979
"1": [
980980
"Heading: SUBJECT: hee hee hee hee MOOD: ☹ Groggy MUSIC: Cibo Matto - Birthday Cake"
981+
],
982+
"4": [
983+
"Heading: 4 COMMENTS | 〚blue〛 POST A COMMENT",
984+
"Utahraptor: *hugses*"
981985
]
982986
}
983987
},

parse_qwantz/elements.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616

1717
def get_elements(
1818
image: SimpleImage
19-
) -> tuple[list[Line], list[Box], list[TextLine], list[Character], list[list[Pixel]]]:
19+
) -> tuple[list[Line], list[int], list[Box], list[TextLine], list[Character], list[list[Pixel]]]:
2020
text_lines: list[TextLine] = []
2121
lines: list[Line] = []
22+
line_widths: list[int] = []
2223
thoughts: list[Box] = []
2324
unmatched: list[list[Pixel]] = []
2425
sorted_pixels = sorted(image.pixels)
@@ -50,8 +51,9 @@ def get_elements(
5051
else:
5152
result = get_line(pixel, tmp_image)
5253
if result:
53-
line, line_pixels = result
54+
line, line_pixels, width = result
5455
lines.append(line)
56+
line_widths.append(width)
5557
sorted_pixels = remove_subsequence(sorted_pixels, line_pixels)
5658
elif result := get_batman(pixel, tmp_image):
5759
batman_box, batman_pixels = result
@@ -69,7 +71,7 @@ def get_elements(
6971
if len(unmatched) == 5:
7072
logger.warning("At least five unmatched objects detected, aborting")
7173
break
72-
return lines, thoughts, cleanup_text_lines(text_lines), extra_characters, unmatched
74+
return lines, line_widths, thoughts, cleanup_text_lines(text_lines), extra_characters, unmatched
7375

7476

7577
def get_batman(pixel: Pixel, image: SimpleImage) -> tuple[Box, list[Pixel]] | None:
+92
Loading

0 commit comments

Comments
 (0)