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

fill color customization #6

Merged
merged 2 commits into from
Apr 11, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.8'

- name: Install Requirements
run: pip install .[dev]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.8'

- name: Install Requirements
run: pip install .[dev]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 40 additions & 29 deletions signwriting/visualizer/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,27 @@


class VisualizeCase(unittest.TestCase):
def test_image_fsw(self):
fsw = "AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S20544510x500S10019476x475"
image = signwriting_to_image(fsw)

def assert_image_equal_with_reference(self, fsw, image):
assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)

self.assertEqual(reference_image.size, image.size)
assert_array_equal(np.array(image), np.array(reference_image))

def test_image_fsw(self):
fsw = "AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S20544510x500S10019476x475"
image = signwriting_to_image(fsw)
self.assert_image_equal_with_reference(fsw, image)

def test_image_without_antialiasing(self):
fsw = "M528x526S1ce40506x474S1ce48472x474S22a04507x511S22a14480x510"
image = signwriting_to_image(fsw, antialiasing=False)

assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)

self.assertEqual(reference_image.size, image.size)
assert_array_equal(np.array(image), np.array(reference_image))
self.assert_image_equal_with_reference(fsw, image)

def test_image_invalid_fsw(self):
fsw = "S20555"
image = signwriting_to_image(fsw)

assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)

assert_array_equal(np.array(image), np.array(reference_image))
self.assert_image_equal_with_reference(fsw, image)

def test_image_small_box(self):
fsw = "M500x500S2ff00407x501S1ce20436x535S2e300413x552S22b04418x565S36520420x523"
Expand All @@ -46,24 +39,42 @@ def test_image_small_box(self):

def test_image_small_box_is_corrected(self):
fsw = "M500x500S2ff00407x501S1ce20436x535S2e300413x552S22b04418x565S36520420x523"

image = signwriting_to_image(fsw, trust_box=False)

assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)

assert_array_equal(np.array(image), np.array(reference_image))
self.assert_image_equal_with_reference(fsw, image)

def test_image_with_line_color(self):
fsw = "M518x517S10040482x485S26500493x474"
image = signwriting_to_image(fsw, line_color=(123, 234, 0, 255))
self.assert_image_equal_with_reference(fsw, image)

def test_image_with_fill_color(self):
fsw = "M536x517S30a00482x482S33e00482x482S17e11516x464S17e19460x481S22a07523x481S22a11463x481"
image = signwriting_to_image(fsw, fill_color=(123, 234, 0, 255))
self.assert_image_equal_with_reference(fsw, image)

def test_image_with_embedded_color(self):
fsw = "M518x517S10043487x482S20500487x507"
image = signwriting_to_image(fsw, embedded_color=True)
self.assert_image_equal_with_reference(fsw, image)

def test_image_with_line_and_fill_color(self):
fsw = "M530x518S19a30500x482S19a38465x481S22f04509x506S22f14467x504"
image = signwriting_to_image(fsw,
line_color=(10, 23, 122, 255),
fill_color=(255, 255, 255, 0))
self.assert_image_equal_with_reference(fsw, image)

def test_image_with_line_and_embedded_color(self):
fsw = "M518x518S30a00482x483S33e00482x483"

image = signwriting_to_image("M518x518S30a00482x483S33e00482x483",
line_color=(144, 70, 180, 255), embedded_color=True)

assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)

assert_array_equal(np.array(image), np.array(reference_image))
image = signwriting_to_image(fsw, embedded_color=True,
line_color=(144, 70, 180, 255))
self.assert_image_equal_with_reference(fsw, image)

def test_image_with_fill_and_embedded_color(self):
fsw = "M518x533S20348482x515S10040493x485S20b00497x468S26600502x468"
image = signwriting_to_image(fsw, embedded_color=True,
fill_color=(123,234,0,255))
self.assert_image_equal_with_reference(fsw, image)


if __name__ == '__main__':
Expand Down
17 changes: 12 additions & 5 deletions signwriting/visualizer/visualize.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from functools import lru_cache
from pathlib import Path
from typing import Tuple

from PIL import Image, ImageDraw, ImageFont

from signwriting.formats.fsw_to_sign import fsw_to_sign
from signwriting.formats.fsw_to_swu import key2id, symbol_line, symbol_fill

# Type alias representing a tuple of four integers: Red, Green, Blue, Alpha
RGBA = Tuple[int, int, int, int]


@lru_cache(maxsize=None)
def get_font(font_name: str) -> ImageFont.FreeTypeFont:
Expand All @@ -20,9 +24,10 @@ def get_symbol_size(symbol: str):
return right - left, bottom - top


# pylint: disable=too-many-locals
def signwriting_to_image(fsw: str, antialiasing=True, trust_box=True,
line_color: tuple[int, int , int, int]=(0, 0, 0, 255), embedded_color=False) -> Image:
# pylint: disable=too-many-locals, too-many-arguments
def signwriting_to_image(fsw: str, antialiasing=True, trust_box=True, embedded_color=False,
line_color: RGBA = (0, 0, 0, 255),
fill_color: RGBA = (255, 255, 255, 255)) -> Image:
sign = fsw_to_sign(fsw)
if len(sign['symbols']) == 0:
return Image.new('RGBA', (1, 1), (0, 0, 0, 0))
Expand Down Expand Up @@ -53,7 +58,9 @@ def signwriting_to_image(fsw: str, antialiasing=True, trust_box=True,
x, y = symbol["position"]
x, y = x - min_x, y - min_y
symbol_id = key2id(symbol["symbol"])
draw.text((x, y), symbol_fill(symbol_id), (255, 255, 255, 255), fill_font)
draw.text((x, y), symbol_line(symbol_id), line_color, line_font, embedded_color=embedded_color)
draw.text((x, y), symbol_fill(symbol_id), fill=fill_color,
font=fill_font, embedded_color=embedded_color)
draw.text((x, y), symbol_line(symbol_id), fill=line_color,
font=line_font, embedded_color=embedded_color)

return img
Loading