Spaces:
Sleeping
Sleeping
| import sys | |
| import os | |
| import pytest | |
| from PIL import Image | |
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src'))) | |
| import graphics_tools | |
| class DummyImageTemplate: | |
| def __init__(self, image, colors): | |
| self.image = image | |
| self.colors = colors | |
| def test_find_closest_color_idx(): | |
| color = (100, 100, 100) | |
| template_colors = [(0, 0, 0), (255, 255, 255), (110, 110, 110)] | |
| idx = graphics_tools.find_closest_color_idx(color, template_colors) | |
| assert idx == 2 | |
| def test_compute_difference(): | |
| color1 = (10, 20, 30) | |
| color2 = (20, 10, 40) | |
| diff = graphics_tools.compute_difference(color1, color2) | |
| assert diff == 30 | |
| def test_replace_colors_by_template(tmp_path): | |
| # Create a dummy image | |
| img = Image.new('RGB', (2, 2), (100, 100, 100)) | |
| img_path = tmp_path / 'dummy.png' | |
| img.save(img_path) | |
| colors = [(100, 100, 100), (200, 200, 200)] | |
| template = DummyImageTemplate(str(img_path), colors) | |
| target_colors = [(1, 2, 3), (4, 5, 6)] | |
| out_img = graphics_tools.replace_colors_by_template(template, target_colors) | |
| assert isinstance(out_img, Image.Image) | |