diff --git a/README.md b/README.md index eb586c4..b6ea308 100644 --- a/README.md +++ b/README.md @@ -150,3 +150,4 @@ LEGO® is a registered trademark of The Lego Group. PERLER BEADS is a trademark of Stitch Acquisition Group. +Aurora test image (`test_image.jpg`) by Jonathan Bean from Unsplash: https://unsplash.com/photos/Ejpx_sdKEKo diff --git a/ih/chart.py b/ih/chart.py index c86c3dc..bcf5311 100644 --- a/ih/chart.py +++ b/ih/chart.py @@ -53,17 +53,17 @@ def preprocess_image( scale=DEFAULT["scale"], guidelines=DEFAULT["guidelines"], ): + # Reduce palette to max 256 colors + reduced_palette = palette.reduce_palette(palette=pal, image=im) + palette_image = palette.get_palette_image(reduced_palette) + im = im.resize((int(im.width / scale), int(im.height / scale))) + # Remove black transparency issues with this one weird trick. alpha = im.convert("RGBA").split()[-1] bg = Image.new("RGBA", im.size, (255, 255, 255, 255)) bg.paste(im, mask=alpha) im = bg - # Reduce palette - reduced_palette = palette.reduce_palette(palette=pal, image=im) # cap palette size at 256 - palette_image = palette.get_palette_image(reduced_palette) - im = im.resize((int(im.width / scale), int(im.height / scale))) - im = ( im.convert("RGB") .convert("P", palette=Image.ADAPTIVE, colors=colors) diff --git a/ih/palette.py b/ih/palette.py index 547b3f6..8a2632e 100644 --- a/ih/palette.py +++ b/ih/palette.py @@ -91,11 +91,12 @@ def reduce_palette(palette, image): best_colours = set() # Get image palette in RGB triplets - my_colours = [x[0:3] for x in image.getdata()] + im = image.convert("P", palette=Image.ADAPTIVE, colors=256) + image_palette = im.getpalette() + my_colours = [] + for i in range (0, len(image_palette), 3): + my_colours.append(image_palette[i:i+3]) - # Larger palettes may take some time, so let 'em know. - if len(my_colours) > 12: - print("Processing...") # Get nearest colour https://stackoverflow.com/a/22478139 tree = sp.KDTree(palette_triplets) for colour in my_colours: diff --git a/test_image.jpg b/test_image.jpg new file mode 100644 index 0000000..acc5cfb Binary files /dev/null and b/test_image.jpg differ diff --git a/tests/test_cli.py b/tests/test_cli.py index 523b899..fda6a21 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -7,9 +7,9 @@ TEST_HTML = TEST_IMAGE.split(".")[0] + ".html" -def runner(args, output=TEST_HTML, print_output=False): +def runner(args, output=TEST_HTML, print_output=False, image=TEST_IMAGE): runner = CliRunner() - result = runner.invoke(main, [TEST_IMAGE] + args) + result = runner.invoke(main, [image] + args) if print_output: print(result.output) assert result.exit_code == 0 @@ -27,6 +27,7 @@ def test_render(): def test_palettes(): for p in palette.PALETTES: runner(["-p", p]) + runner(["-p", p, "-s", "25"], image="test_image.jpg") def test_guidelines(): @@ -51,3 +52,7 @@ def test_term(): def test_term_render(): runner(["-o", "term", "-r"], output="ih version", print_output=True) + runner(["-o", "term", "-r", "-s", "25"], + output="ih version", + print_output=True, + image="test_image.jpg")