Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import random
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
# Function to enhance contrast of the image
|
| 8 |
+
def enhance_contrast(img):
|
| 9 |
+
return img # placeholder for actual contrast enhancement logic
|
| 10 |
+
|
| 11 |
+
# Function to detect dominant colors
|
| 12 |
+
def get_dominant_colors(image, n_colors=6):
|
| 13 |
+
image = image.resize((150, 150)) # Resize for faster processing
|
| 14 |
+
result = image.convert('P', palette=Image.ADAPTIVE, colors=n_colors)
|
| 15 |
+
result = result.convert('RGB')
|
| 16 |
+
colors = result.getcolors(150 * 150)
|
| 17 |
+
colors = sorted(colors, reverse=True, key=lambda x: x[0])[:n_colors]
|
| 18 |
+
return [color[1] for color in colors]
|
| 19 |
+
|
| 20 |
+
# Function to convert RGB to HEX
|
| 21 |
+
def rgb_to_hex(color):
|
| 22 |
+
return '#%02x%02x%02x' % color
|
| 23 |
+
|
| 24 |
+
# Function to generate color harmonies
|
| 25 |
+
def generate_harmonies(colors):
|
| 26 |
+
harmonies = {}
|
| 27 |
+
for color in colors:
|
| 28 |
+
r, g, b = color
|
| 29 |
+
comp_color = (255 - r, 255 - g, 255 - b) # complementary color
|
| 30 |
+
analogous1 = ((r + 30) % 255, (g + 30) % 255, (b + 30) % 255)
|
| 31 |
+
analogous2 = ((r - 30) % 255, (g - 30) % 255, (b - 30) % 255)
|
| 32 |
+
harmonies[rgb_to_hex(color)] = {
|
| 33 |
+
'complementary': rgb_to_hex(comp_color),
|
| 34 |
+
'analogous': (rgb_to_hex(analogous1), rgb_to_hex(analogous2))
|
| 35 |
+
}
|
| 36 |
+
return harmonies
|
| 37 |
+
|
| 38 |
+
# Function to create a LinkedIn-friendly color palette description with CSS code
|
| 39 |
+
def create_palette_description(colors):
|
| 40 |
+
descriptions = [
|
| 41 |
+
"A vibrant palette for branding and marketing.",
|
| 42 |
+
"A calming and trustworthy color scheme for professional use.",
|
| 43 |
+
"Bold and energetic colors, perfect for grabbing attention.",
|
| 44 |
+
"Soft and neutral tones, ideal for elegant branding."
|
| 45 |
+
]
|
| 46 |
+
chosen_description = random.choice(descriptions)
|
| 47 |
+
|
| 48 |
+
# Generate the HTML palette
|
| 49 |
+
palette_html = f"<h4>{chosen_description}</h4><div style='display:flex; flex-wrap:wrap;'>"
|
| 50 |
+
css_code = "/* Color Palette CSS */\n"
|
| 51 |
+
for i, color in enumerate(colors):
|
| 52 |
+
hex_color = rgb_to_hex(color)
|
| 53 |
+
palette_html += f"<div style='width: 100px; height: 50px; background-color: {hex_color}; margin: 5px;'></div>"
|
| 54 |
+
palette_html += f"<div style='padding: 15px;'>HEX: {hex_color}</div>"
|
| 55 |
+
# Add the CSS code for each color
|
| 56 |
+
css_code += f".color-{i} {{ background-color: {hex_color}; }}\n"
|
| 57 |
+
|
| 58 |
+
palette_html += "</div>"
|
| 59 |
+
|
| 60 |
+
return palette_html, css_code
|
| 61 |
+
|
| 62 |
+
# Function to generate a downloadable palette image
|
| 63 |
+
def generate_palette_image(colors):
|
| 64 |
+
img_width = 500
|
| 65 |
+
img_height = 100
|
| 66 |
+
palette_img = Image.new('RGB', (img_width, img_height))
|
| 67 |
+
|
| 68 |
+
color_width = img_width // len(colors)
|
| 69 |
+
for i, color in enumerate(colors):
|
| 70 |
+
img = Image.new('RGB', (color_width, img_height), color)
|
| 71 |
+
palette_img.paste(img, (i * color_width, 0))
|
| 72 |
+
|
| 73 |
+
return palette_img # Return PIL image directly
|
| 74 |
+
|
| 75 |
+
# Main function to generate palette and display LinkedIn-friendly results
|
| 76 |
+
def generate_palette(image_path):
|
| 77 |
+
img = Image.open(image_path)
|
| 78 |
+
|
| 79 |
+
# Enhance the contrast
|
| 80 |
+
img = enhance_contrast(img)
|
| 81 |
+
|
| 82 |
+
# Extract dominant colors
|
| 83 |
+
n_colors = 6
|
| 84 |
+
colors = get_dominant_colors(img, n_colors)
|
| 85 |
+
|
| 86 |
+
# Convert colors to HEX and create palette description with CSS
|
| 87 |
+
palette_html, css_code = create_palette_description(colors)
|
| 88 |
+
|
| 89 |
+
# Generate palette image for download
|
| 90 |
+
palette_img = generate_palette_image(colors)
|
| 91 |
+
|
| 92 |
+
return palette_html, palette_img, css_code
|
| 93 |
+
|
| 94 |
+
# Gradio Interface
|
| 95 |
+
def gradio_interface(image_path):
|
| 96 |
+
palette_html, palette_img, css_code = generate_palette(image_path)
|
| 97 |
+
|
| 98 |
+
# Convert PIL image to NumPy array (Gradio expects a NumPy array or filepath)
|
| 99 |
+
palette_img_np = np.array(palette_img)
|
| 100 |
+
|
| 101 |
+
return palette_html, palette_img_np, css_code
|
| 102 |
+
|
| 103 |
+
# Create the Gradio interface
|
| 104 |
+
with gr.Blocks() as interface:
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column():
|
| 107 |
+
image_input = gr.Image(type="filepath", label="Upload Image") # Image Upload
|
| 108 |
+
submit_btn = gr.Button("Submit", elem_id="submit_btn")
|
| 109 |
+
clear_btn = gr.Button("Clear", elem_id="clear_btn")
|
| 110 |
+
|
| 111 |
+
with gr.Column():
|
| 112 |
+
palette_output = gr.HTML(label="Generated Color Palette")
|
| 113 |
+
palette_image_output = gr.Image(label="Downloadable Palette Image")
|
| 114 |
+
css_code_output = gr.Code(label="Generated CSS Code") # Display generated CSS code
|
| 115 |
+
|
| 116 |
+
submit_btn.click(gradio_interface, inputs=[image_input], outputs=[
|
| 117 |
+
palette_output, palette_image_output, css_code_output])
|
| 118 |
+
|
| 119 |
+
# Clear button now resets the image input and clears all outputs
|
| 120 |
+
clear_btn.click(lambda: [None, None, None, None], outputs=[
|
| 121 |
+
image_input, palette_output, palette_image_output, css_code_output])
|
| 122 |
+
|
| 123 |
+
# Launch the interface
|
| 124 |
+
interface.launch(share=True)
|