Skip to content

Commit

Permalink
Implemented closest-colour algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryPeach committed Jun 19, 2019
1 parent 3e94705 commit 09d8538
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
Binary file added composite.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 20 additions & 7 deletions emojimage/__main__.py
Expand Up @@ -2,20 +2,21 @@
import argparse
import os
from PIL import Image
from .metafile import generate_metafile, does_metafile_exist, populate_emoji_dictionary
from .metafile import (generate_metafile, does_metafile_exist, populate_emoji_dictionary, emoji_dictionary,
get_closest_colour)
from .log_manager import setup_custom_logger
from .img_utils import resize_image, get_emoji_image
from .img_utils import resize_image, get_emoji_image, get_average_color


def create_collage(path, image_scale=1, emoji_size=64):
def create_collage(path, image_scale=1, emoji_size=16):
"""Creates a collage of emojis from image data
Arguments:
path {string} -- The path to the original image
Keyword Arguments:
image_scale {int} -- The scale of the output image (default: {1})
emoji_size {int} -- The size for each emoji to be (default: {64})
emoji_size {int} -- The size for each emoji to be (default: {16})
"""
image = Image.open(path)

Expand All @@ -34,9 +35,20 @@ def create_collage(path, image_scale=1, emoji_size=64):
composite_image = Image.new("RGB", (width, height))
# For every x emoji
for x in range(0, emojis_x):
print(f"X coord: {(width/emojis_x) * x}")
for y in range(0, emojis_y):
# Temporary image to crop and get average color
temp_image = image.copy().crop((x * emoji_size,
y * emoji_size,
(x + 1) * emoji_size,
(y + 1) * emoji_size))
temp_avg = get_average_color(temp_image)

print(get_emoji_image("adult"))
offset = ((width//emojis_x) * x, (height//emojis_y) * y)
emoji_image = get_emoji_image("face-with-tears-of-joy")
composite_image.paste(emoji_image.resize((emoji_size, emoji_size)), offset)

# Save the final image
composite_image.save("composite.png")

if __name__ == "__main__":
# Setup logger
Expand All @@ -61,5 +73,6 @@ def create_collage(path, image_scale=1, emoji_size=64):
logger.debug("Emoji metafile already existed, and so was not created")

# TODO implement arguments for width, height, and emoji size
create_collage(args.image_input)
# create_collage(args.image_input)
populate_emoji_dictionary()
get_closest_colour((123, 123, 253))
2 changes: 1 addition & 1 deletion emojimage/img_utils.py
Expand Up @@ -15,7 +15,7 @@ def get_emoji_image(name):
"""
with resources.path("emojimage", "emoji") as p:
for emoji_path in p.glob(name + ".*"):
return emoji_path
return Image.open(emoji_path)
return None


Expand Down
21 changes: 20 additions & 1 deletion emojimage/metafile.py
Expand Up @@ -2,6 +2,7 @@
import os
import csv
import logging
from math import sqrt
from PIL import Image
from importlib import resources
from .img_utils import get_average_color
Expand All @@ -22,7 +23,6 @@ def populate_emoji_dictionary():
rgbval = tuple(row[-3:])
emoji_dictionary.setdefault(rgbval, [])
emoji_dictionary[rgbval].append(row[0])
print(emoji_dictionary)


def get_metafile_path():
Expand All @@ -43,6 +43,25 @@ def does_metafile_exist():
return os.path.isfile(get_metafile_path())


def get_closest_colour(rgb):
"""Takes in a colour and returns the closest emojis to that colour
Adapted from https://stackoverflow.com/a/54242348
Arguments:
rgb {tuple} -- The red, green, and blue values of the input
Returns:
list -- The emoji/emojis that are closest to the given colour
"""
r, g, b = rgb
colour_diffs = []
for colour in emoji_dictionary.keys():
cr, cg, cb = colour
colour_diff = sqrt(abs(int(r) - int(cr))**2 + abs(int(g) - int(cg))**2 + abs(int(b) - int(cb))**2)
colour_diffs.append((colour_diff, colour))
return emoji_dictionary.get(min(colour_diffs)[1])


def generate_metafile():
"""Generates a metafile of information about each emoji for easy use in the program
"""
Expand Down

0 comments on commit 09d8538

Please sign in to comment.