def snap_to_grid(src, ref): src = colour.palette(src) ref = colour.palette(ref) utils = colour.utils(ref) results = {} for hex, details in src.colours.items(): closest = utils.closest_colour(hex) results[hex] = closest return results
def draw(palette, image): palette = colour.palette(palette) utils = colour.utils(palette) results = {} im = Image.open(image) for y in range(im.size[1]): for x in range(im.size[0]): old = im.getpixel((x, y)) hex = webcolors.rgb_to_hex(old) closest = utils.closest_colour(hex) new = webcolors.hex_to_rgb(closest[0]) im.putpixel((x, y), new) return im
x = 0 y = 0 img = Image.new("RGBA", (width, height)) canvas = ImageDraw.Draw(img) for hex in (hex1, hex2): rgb = webcolors.hex_to_rgb(hex) canvas.rectangle((x, y, (x + dx), (y + dy)), fill=rgb) canvas.text((x + 10, y + 10), hex, fill=(255,255,255)) x += dx return img if __name__ == '__main__': palette = sys.argv[1] hex = sys.argv[2] p = colour.palette(palette) u = colour.utils(p) c_hex, c_name = u.closest_colour(hex) im = side_by_side(hex, c_hex) im.show()