MagicBoard / main.py
mfranzon's picture
first commit, magicboard on hf
d9dec00
from PIL import Image
from utils import pipe_image
import streamlit as st
from streamlit_drawable_canvas import st_canvas
import gettext
import numpy as np
_ = gettext.gettext
language = st.selectbox(_('Choose your language'), ['en', 'it'])
try:
localizator = gettext.translation('base', localedir='locales', languages=[language])
localizator.install()
_ = localizator.gettext
except:
pass
st.markdown("<h1 style='text-align:center;'> Magic Board</h1>", unsafe_allow_html=True)
# Specify canvas parameters in application
drawing_mode = st.sidebar.selectbox(
_("Drawing tool:"), ("freedraw", "line", "rect", "circle", "transform")
)
stroke_width = st.sidebar.slider(_("Stroke width: "), 1, 25, 3)
stroke_color = st.sidebar.color_picker(_("Stroke color hex: "))
bg_color = st.sidebar.color_picker(_("Background color hex: "), "#eee")
bg_image = st.sidebar.file_uploader(_("Background image:"), type=["png", "jpg"])
strength = st.sidebar.slider(_("Strength value: "), 0.0, 1.0, 0.1)
# Create a canvas component
canvas_result = st_canvas(
fill_color="rgba(255, 165, 0, 0.3)", # Fixed fill color with some opacity
stroke_width=stroke_width,
stroke_color=stroke_color,
background_color=bg_color,
background_image=Image.open(bg_image) if bg_image else None,
update_streamlit=True,
height=450,
width=680,
drawing_mode=drawing_mode,
#point_display_radius=point_display_radius if drawing_mode == 'point' else 0,
key="canvas",
)
prompt = st.text_input(_('Prompt to generate your cool image'),
placeholder=_('write you text here to improve the image with your favourite style'))
if st.button('Submit'):
img_size = (512, 512)
if bg_image:
img_back=Image.open(bg_image).convert('RGB').resize(img_size)
comp_img = img_back
if np.any(canvas_result.image_data):
img_draw=Image.fromarray(canvas_result.image_data).convert('RGB').resize(img_size)
comp_img = img_draw
if bg_image and np.any(canvas_result.image_data):
comp_img = Image.new("RGB", img_size)
comp_img = Image.blend(img_draw, img_back, alpha=0.5)
images = pipe_image(prompt=prompt,
init_image=comp_img,
strength=strength)
for image in images:
st.image(image)