-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdominantcolor-repo.py
42 lines (30 loc) · 1016 Bytes
/
dominantcolor-repo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
st.title("Dominant Color Extraction ")
st.subheader("Input Image")
img = st.file_uploader("Choose An Image")
if img is not None:
st.subheader("Original Image")
st.image(img)
print(type(img))
img = plt.imread(img)
print(img.shape)
n = img.shape[0]*img.shape[1]
all_pixels= img.reshape((n, 3))
k = st.number_input('Enter the no. of dominant colors',min_value=1)
#create and fit model
model = KMeans(n_clusters = k)
model.fit(all_pixels)
#creating centers
centers = model.cluster_centers_.astype('uint8')
#creating new image
new_img=np.zeros((n,3),dtype='uint8')
for i in range(n):
group_idx = model.labels_[i]
new_img[i] = centers[group_idx]
new_img = new_img.reshape(*img.shape)
st.subheader("New Image")
st.image(new_img)