Skip to content

Instantly share code, notes, and snippets.

@andfanilo
Last active September 6, 2020 16:06
Show Gist options
  • Save andfanilo/263b6487ea2e5f5411dfeb2c923d724a to your computer and use it in GitHub Desktop.
Save andfanilo/263b6487ea2e5f5411dfeb2c923d724a to your computer and use it in GitHub Desktop.
Streamlit color namer
import numpy as np
from PIL import Image
from colornamer import get_color_from_rgb
import streamlit as st
from streamlit_cropper import st_cropper
st.set_option('deprecation.showfileUploaderEncoding', False)
st.title("Color namer demo")
img_file = st.sidebar.file_uploader(label='Upload a file', type=['png', 'jpg'])
box_color = st.sidebar.beta_color_picker(label="Box Color", value='#ffff00')
if not img_file:
st.info("Upload file in sidebar")
if img_file:
st.markdown("Select the zone of the image you want to get the name color of")
img = Image.open(img_file)
cropped_img = st_cropper(img, realtime_update=True, box_color=box_color, aspect_ratio=None)
np_img = np.asarray((cropped_img))
avg_color_per_row = np.average(np_img, axis=0)
avg_color = np.average(avg_color_per_row, axis=0)
color_names = get_color_from_rgb(avg_color)
st.markdown(f"The Common Color in selected zone is: {color_names['common_color']}")
st.sidebar.markdown("_Credits to [Stitchfix colornamer](https://github.com/stitchfix/colornamer/) and [streamlit-cropper](https://github.com/turner-anderson/streamlit-cropper)_")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment