Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,9 @@ 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 #
|
| 10 |
|
| 11 |
# Function to detect dominant colors
|
| 12 |
def get_dominant_colors(image, n_colors=6):
|
|
@@ -35,7 +35,7 @@ def generate_harmonies(colors):
|
|
| 35 |
}
|
| 36 |
return harmonies
|
| 37 |
|
| 38 |
-
# Function to create a LinkedIn-friendly color palette description
|
| 39 |
def create_palette_description(colors):
|
| 40 |
descriptions = [
|
| 41 |
"A vibrant palette for branding and marketing.",
|
|
@@ -44,20 +44,13 @@ def create_palette_description(colors):
|
|
| 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 |
-
|
| 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):
|
|
@@ -72,23 +65,34 @@ def generate_palette_image(colors):
|
|
| 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
|
| 87 |
-
palette_html
|
| 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
|
|
@@ -111,12 +115,12 @@ with gr.Blocks() as interface:
|
|
| 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.
|
| 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 |
|
|
|
|
| 4 |
import random
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
+
# Function to enhance contrast of the image (placeholder for future enhancement)
|
| 8 |
def enhance_contrast(img):
|
| 9 |
+
return img # Currently, just returns the original image
|
| 10 |
|
| 11 |
# Function to detect dominant colors
|
| 12 |
def get_dominant_colors(image, n_colors=6):
|
|
|
|
| 35 |
}
|
| 36 |
return harmonies
|
| 37 |
|
| 38 |
+
# Function to create a LinkedIn-friendly color palette description
|
| 39 |
def create_palette_description(colors):
|
| 40 |
descriptions = [
|
| 41 |
"A vibrant palette for branding and marketing.",
|
|
|
|
| 44 |
"Soft and neutral tones, ideal for elegant branding."
|
| 45 |
]
|
| 46 |
chosen_description = random.choice(descriptions)
|
|
|
|
|
|
|
| 47 |
palette_html = f"<h4>{chosen_description}</h4><div style='display:flex; flex-wrap:wrap;'>"
|
| 48 |
+
for color in colors:
|
|
|
|
| 49 |
hex_color = rgb_to_hex(color)
|
| 50 |
palette_html += f"<div style='width: 100px; height: 50px; background-color: {hex_color}; margin: 5px;'></div>"
|
| 51 |
palette_html += f"<div style='padding: 15px;'>HEX: {hex_color}</div>"
|
|
|
|
|
|
|
|
|
|
| 52 |
palette_html += "</div>"
|
| 53 |
+
return palette_html
|
|
|
|
| 54 |
|
| 55 |
# Function to generate a downloadable palette image
|
| 56 |
def generate_palette_image(colors):
|
|
|
|
| 65 |
|
| 66 |
return palette_img # Return PIL image directly
|
| 67 |
|
| 68 |
+
# Function to generate the CSS code for the color palette
|
| 69 |
+
def generate_css_code(colors):
|
| 70 |
+
css_code = "/* Color Palette CSS */\n"
|
| 71 |
+
for i, color in enumerate(colors):
|
| 72 |
+
hex_color = rgb_to_hex(color)
|
| 73 |
+
css_code += f".color-{i} {{ background-color: {hex_color}; }}\n"
|
| 74 |
+
return css_code
|
| 75 |
+
|
| 76 |
# Main function to generate palette and display LinkedIn-friendly results
|
| 77 |
def generate_palette(image_path):
|
| 78 |
img = Image.open(image_path)
|
| 79 |
|
| 80 |
+
# Enhance the contrast (optional placeholder)
|
| 81 |
img = enhance_contrast(img)
|
| 82 |
|
| 83 |
# Extract dominant colors
|
| 84 |
n_colors = 6
|
| 85 |
colors = get_dominant_colors(img, n_colors)
|
| 86 |
|
| 87 |
+
# Convert colors to HEX and create palette description
|
| 88 |
+
palette_html = create_palette_description(colors)
|
| 89 |
|
| 90 |
# Generate palette image for download
|
| 91 |
palette_img = generate_palette_image(colors)
|
| 92 |
|
| 93 |
+
# Generate CSS code for the palette
|
| 94 |
+
css_code = generate_css_code(colors)
|
| 95 |
+
|
| 96 |
return palette_html, palette_img, css_code
|
| 97 |
|
| 98 |
# Gradio Interface
|
|
|
|
| 115 |
with gr.Column():
|
| 116 |
palette_output = gr.HTML(label="Generated Color Palette")
|
| 117 |
palette_image_output = gr.Image(label="Downloadable Palette Image")
|
| 118 |
+
css_code_output = gr.Textbox(label="Generated CSS Code", lines=6) # Use Textbox to display CSS code
|
| 119 |
|
| 120 |
submit_btn.click(gradio_interface, inputs=[image_input], outputs=[
|
| 121 |
palette_output, palette_image_output, css_code_output])
|
| 122 |
|
| 123 |
+
# The Clear button now resets the image input and clears all outputs
|
| 124 |
clear_btn.click(lambda: [None, None, None, None], outputs=[
|
| 125 |
image_input, palette_output, palette_image_output, css_code_output])
|
| 126 |
|