Skip to content

Commit

Permalink
Merge pull request #15 from ehashman/bugfix
Browse files Browse the repository at this point in the history
Optimize palette selection for non-indexed images
  • Loading branch information
glasnt authored Nov 13, 2020
2 parents 1a29639 + 9c5314e commit 71585a5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions ih/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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 71585a5

Please sign in to comment.