Skip to content

Commit

Permalink
Optimize palette selection for non-indexed images
Browse files Browse the repository at this point in the history
  • Loading branch information
ehashman committed Oct 27, 2020
1 parent 22d1b1b commit edc406d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,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
9 changes: 5 additions & 4 deletions ih/palette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Binary file added test_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand All @@ -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")

0 comments on commit edc406d

Please sign in to comment.