Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import gradio as grfrom PIL import Image, ImageDraw, ImageFontimport numpy as npimport io# Define your palette as before (numbers mapped to RGB colors)PALETTE = {"M": (255, 0, 255), # Magenta"7": (255, 0, 0), # Red"A": (255, 204, 0), # Yellow Orange"B": (255, 255, 0), # Yellow"9": (255, 128, 0), # Orange"8": (255, 69, 0), # Red Orange"H": (0, 0, 255), # Blue"0": (0, 0, 0), # Black"I": (0, 0, 139), # Dark Blue"F": (0, 255, 255), # Aqua Green"D": (0, 128, 0), # Green"L": (75, 0, 130), # Dark Violet}BLOCK_SIZE = 20def find_nearest_color(rgb, palette):r, g, b = rgbbest_key, best_dist = None, float("inf")for key, (pr, pg, pb) in palette.items():dist = (r - pr)**2 + (g - pg)**2 + (b - pb)**2if dist < best_dist:best_dist, best_key = dist, keyreturn best_keydef process_image(img):img = img.convert("RGB")w, h = img.sizenumbered_img = Image.new("RGB", (w * BLOCK_SIZE, h * BLOCK_SIZE), (255, 255, 255))draw_nums = ImageDraw.Draw(numbered_img)# Use a default PIL font for compatibility and size suitable for blockfont = ImageFont.load_default()for y in range(h):for x in range(w):pixel = img.getpixel((x, y))key = find_nearest_color(pixel, PALETTE)color = PALETTE[key]x0, y0 = x * BLOCK_SIZE, y * BLOCK_SIZEx1, y1 = x0 + BLOCK_SIZE, y0 + BLOCK_SIZE# Draw colored blockdraw_nums.rectangle([x0, y0, x1, y1], fill=color, outline=(0, 0, 0))# Draw number centeredw_text, h_text = draw_nums.textsize(str(key), font=font)text_x = x0 + (BLOCK_SIZE - w_text) / 2text_y = y0 + (BLOCK_SIZE - h_text) / 2draw_nums.text((text_x, text_y), str(key), fill="black", font=font)return numbered_img# Build Gradio UItitle = "Color By Number Generator"iface = gr.Interface(fn=process_image,inputs=gr.Image(type="pil"),outputs=gr.Image(type="pil"),title=title,description="Upload an image and generate a color-by-number pixel art version.")if __name__ == "__main__":123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import gradio as grfrom PIL import Image, ImageDraw, ImageFont# Define palette mapping keys to RGB colors (from your sample image)PALETTE = {"M": (255, 0, 255), # Magenta"7": (255, 0, 0), # Red"A": (255, 204, 0), # Yellow Orange"B": (255, 255, 0), # Yellow"9": (255, 128, 0), # Orange"8": (255, 69, 0), # Red Orange"H": (0, 0, 255), # Blue"0": (0, 0, 0), # Black"I": (0, 0, 139), # Dark Blue"F": (0, 255, 255), # Aqua Green"D": (0, 128, 0), # Green"L": (75, 0, 130), # Dark Violet}BLOCK_SIZE = 20def find_nearest_color(rgb):r, g, b = rgbbest_key = Nonebest_dist = float('inf')for key, (pr, pg, pb) in PALETTE.items():dist = (r - pr)**2 + (g - pg)**2 + (b - pb)**2if dist < best_dist:best_dist = distbest_key = keyreturn best_keydef process_image(img):# Ensure image RGBimg = img.convert("RGB")w, h = img.size# Resize large images to max 50 pixels wide/tall for performanceMAX_DIM = 50if max(w, h) > MAX_DIM:scale = MAX_DIM / max(w, h)img = img.resize((int(w*scale), int(h*scale)))w, h = img.size# Create new image for outputoutput_img = Image.new("RGB", (w * BLOCK_SIZE, h * BLOCK_SIZE), (255, 255, 255))draw = ImageDraw.Draw(output_img)font = ImageFont.load_default()for y in range(h):for x in range(w):pixel = img.getpixel((x, y))key = find_nearest_color(pixel)color = PALETTE[key]x0 = x * BLOCK_SIZEy0 = y * BLOCK_SIZEx1 = x0 + BLOCK_SIZEy1 = y0 + BLOCK_SIZE# Draw blockdraw.rectangle([x0, y0, x1, y1], fill=color, outline=(0, 0, 0))# Draw number centered on blockw_text, h_text = draw.textsize(key, font=font)draw.text((x0 + (BLOCK_SIZE - w_text)/2, y0 + (BLOCK_SIZE - h_text)/2), key, fill=(0, 0, 0), font=font)return output_imgiface = gr.Interface(fn=process_image,inputs=gr.Image(type="pil"),outputs=gr.Image(type="pil"),title="Color By Number Pixel Art",description="Upload an image and get a color-by-number pixel art version with numbers.")if __name__ == "__main__":
app.py+42-38