Last active
April 12, 2022 17:40
-
-
Save Moondarker/e7a81e510da1bf553fcb36a3a6c2e52c to your computer and use it in GitHub Desktop.
rplace palette checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageColor | |
from math import sqrt | |
import sys | |
PALETTE = ["#6D001A", "#BE0039", "#FF4500", "#FFA800", "#FFD635", "#FFF8B8", "#00A368", "#00CC78", "#7EED56", "#00756F", "#009EAA", "#00CCC0", "#2450A4", "#3690EA", "#51E9F4", "#493AC1", "#6A5CFF", "#94B3FF", "#811E9F", "#B44AC0", "#E4ABFF", "#DE107F", "#FF3881", "#FF99AA", "#6D482F", "#9C6926", "#FFB470", "#000000", "#515252", "#898D90", "#D4D7D9", "#FFFFFF"] | |
rgb_palette = [] | |
def hex_to_rgb(hex: str): | |
return ImageColor.getcolor(hex, "RGB") | |
def load_image(): | |
try: | |
im = Image.open(sys.argv[1]) | |
except FileNotFoundError: | |
print('File not found') | |
exit() | |
if im.mode != "RGBA": | |
im = im.convert("RGBA") | |
return im, im.load(), im.width, im.height | |
def closest_color(rgb): | |
r, g, b = rgb | |
color_diffs = [] | |
for color in rgb_palette: | |
cr, cg, cb = color | |
color_diff = sqrt((r - cr)**2 + (g - cg)**2 + (b - cb)**2) | |
color_diffs.append((color_diff, color)) | |
return min(color_diffs)[1] | |
for color in PALETTE: | |
rgb_palette.append(hex_to_rgb(color)) | |
known_pixels = [] | |
image, pixels, width, height = load_image() | |
bad_flag = False | |
for y in range(height): | |
for x in range(width): | |
color = pixels[x, y][:3] | |
if color not in rgb_palette: | |
bad_flag = True | |
good_color = closest_color(color) | |
image.putpixel((x, y), good_color) | |
print("Color {} => {} at X{} Y{}".format(color, good_color, x, y)) | |
if not bad_flag: | |
print('All good') | |
else: | |
image.save('{}_modified.png'.format(sys.argv[1][:-4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment