added app
Browse files- app.py +82 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from sklearn.cluster import KMeans
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# Function to extract dominant colors
|
| 9 |
+
def extract_colors(image, num_colors):
|
| 10 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 11 |
+
pixels = image.reshape(-1, 3) # Reshape to 2D array
|
| 12 |
+
|
| 13 |
+
kmeans = KMeans(n_clusters=num_colors, n_init=10, random_state=42)
|
| 14 |
+
kmeans.fit(pixels)
|
| 15 |
+
|
| 16 |
+
colors = kmeans.cluster_centers_.astype(int)
|
| 17 |
+
return colors
|
| 18 |
+
|
| 19 |
+
# Function to display the color palette
|
| 20 |
+
def display_palette(colors):
|
| 21 |
+
fig, ax = plt.subplots(figsize=(8, 2))
|
| 22 |
+
ax.imshow([colors], aspect='auto')
|
| 23 |
+
ax.set_xticks([])
|
| 24 |
+
ax.set_yticks([])
|
| 25 |
+
st.pyplot(fig)
|
| 26 |
+
|
| 27 |
+
# Streamlit UI Design
|
| 28 |
+
st.set_page_config(page_title="Color Palette Generator", page_icon="🎨", layout="centered")
|
| 29 |
+
|
| 30 |
+
# Custom CSS for aesthetics
|
| 31 |
+
st.markdown(
|
| 32 |
+
"""
|
| 33 |
+
<style>
|
| 34 |
+
.title {
|
| 35 |
+
text-align: center;
|
| 36 |
+
font-size: 36px;
|
| 37 |
+
font-weight: bold;
|
| 38 |
+
color: #4A90E2;
|
| 39 |
+
}
|
| 40 |
+
.subtitle {
|
| 41 |
+
text-align: center;
|
| 42 |
+
font-size: 20px;
|
| 43 |
+
color: #7F8C8D;
|
| 44 |
+
}
|
| 45 |
+
.uploaded-img {
|
| 46 |
+
display: flex;
|
| 47 |
+
justify-content: center;
|
| 48 |
+
}
|
| 49 |
+
</style>
|
| 50 |
+
""",
|
| 51 |
+
unsafe_allow_html=True,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Header Design
|
| 55 |
+
st.markdown("<div class='title'>🎨 K-Means Color Palette Generator</div>", unsafe_allow_html=True)
|
| 56 |
+
st.markdown("<div class='subtitle'>Upload an image and extract its dominant colors!</div>", unsafe_allow_html=True)
|
| 57 |
+
|
| 58 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
|
| 59 |
+
num_colors = st.slider("Select Number of Colors", 2, 10, 5)
|
| 60 |
+
|
| 61 |
+
if uploaded_file is not None:
|
| 62 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 63 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 64 |
+
|
| 65 |
+
st.markdown("<div class='uploaded-img'>", unsafe_allow_html=True)
|
| 66 |
+
st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Uploaded Image", use_column_width=True)
|
| 67 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 68 |
+
|
| 69 |
+
colors = extract_colors(image, num_colors)
|
| 70 |
+
|
| 71 |
+
st.write("## 🎨 Extracted Color Palette")
|
| 72 |
+
display_palette(colors)
|
| 73 |
+
|
| 74 |
+
# Show color RGB values
|
| 75 |
+
st.write("## 🌈 RGB Values of Extracted Colors")
|
| 76 |
+
color_columns = st.columns(num_colors)
|
| 77 |
+
for i, color in enumerate(colors):
|
| 78 |
+
with color_columns[i]:
|
| 79 |
+
st.markdown(f"""<div style='background-color: rgb({color[0]}, {color[1]}, {color[2]});
|
| 80 |
+
height: 50px; border-radius: 10px;'></div>""", unsafe_allow_html=True)
|
| 81 |
+
st.write(f"RGB({color[0]}, {color[1]}, {color[2]})")
|
| 82 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|
| 4 |
+
Pillow
|
| 5 |
+
matplotlib
|