i need help averaging the colors of emojis and outputting them in a list where each new line displays the emoji information in this format
"colours[‘emoji icon’]=[colors in vector3 format]
EXAMPLE FORMAT: colours[‘’]=[189,181,176]
the thing is when I output it it shows this
Attached is the emoji list and the python file. this uses packages numpy, tkinter, and emoji
WHEN IT PROMPTS YOU WITH A TTF FILE, LISTEN TO THE WARNING THAT POPS UP
Any help would be appreciated.
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image, ImageDraw, ImageFont
import emoji
def select_files():
input_file = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
if not input_file:
return
messagebox.showwarning("Font Selection", "Please ensure you have the Noto Color Emoji font installed.\n\n"
"You can download it from: "
"https://fonts.google.com/noto/specimen/Noto+Color+Emoji")
font_path = filedialog.askopenfilename(filetypes=[("Font Files", "*.ttf")])
if not font_path:
return
output_file = filedialog.asksaveasfilename(filetypes=[("Text Files", "*.txt")], defaultextension=".txt")
if not output_file:
return
process_text_file(input_file, output_file, font_path)
messagebox.showinfo("Process Complete", "Emoji colors have been processed and saved to the output file.")
def process_text_file(file_path, output_file, font_path):
image_size = (200, 200) # Adjust the image size as needed for better accuracy
with open(file_path, 'r', encoding='utf-8') as file:
colors = {}
for line in file:
emojis = emoji.emoji_list(line)
for e in emojis:
emoji_char = e['emoji']
emoji_name = '_'.join(hex(ord(c))[2:] for c in emoji_char)
if emoji_name not in colors:
img = Image.new('RGB', image_size, color=(255, 255, 255))
d = ImageDraw.Draw(img)
font = ImageFont.truetype(font_path, 150) # Adjust the font size as needed
text_width, text_height = d.textsize(emoji_char, font=font)
x = (image_size[0] - text_width) // 2
y = (image_size[1] - text_height) // 2
d.text((x, y), emoji_char, fill=(0, 0, 0), font=font)
colors[emoji_name] = get_average_color(img)
with open(output_file, 'w') as file:
file.write("colours = {\n")
for emoji_name, color in colors.items():
r, g, b = color
file.write(f" '{emoji_name}': [{r}, {g}, {b}],\n")
file.write("}")
def get_average_color(image):
width, height = image.size
pixels = image.load()
r_total, g_total, b_total, count = 0, 0, 0, 0
for y in range(height):
for x in range(width):
r, g, b, _ = pixels[x, y]
if r != 255 or g != 255 or b != 255: # Exclude white background pixels
r_total += r
g_total += g
b_total += b
count += 1
if count > 0:
r_avg = r_total // count
g_avg = g_total // count
b_avg = b_total // count
return r_avg, g_avg, b_avg
else:
return 255, 255, 255 # Return white if no non-white pixels found
root = tk.Tk()
root.title("Emoji Color Extractor")
button = tk.Button(root, text="Select File", command=select_files)
button.pack(pady=20)
root.mainloop()
EMOJI LIST THAT YOU WILL USE ON THE FIRST FILE PROMPT
https://file.io/KNCMErOBFGhZ