Spaces:
Sleeping
Sleeping
new version
Browse filesFiles changed (5)
- .streamlit/config.tomlDeleted-6
- app.py+44-69
- assets/demo.jpgDeleted
- assets/test_character.pngAdded
- requirements.txt+2-2
123456[server]maxUploadSize = 10
[theme]base="light"primaryColor="#0074ff"1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import streamlit as stimport osfrom datetime import datetimefrom PIL import Imagefrom io import BytesIOfrom src.utils import change_background, mattefrom src.st_style import apply_prod_style# apply_prod_style(st) # NOTE: Uncomment this for production!def image_download_button(pil_image, filename: str, fmt: str, label="Download"):if fmt not in ["jpg", "png"]:raise Exception(f"Unknown image format (Available: {fmt} - case sensitive)")pil_format = "JPEG" if fmt == "jpg" else "PNG"file_format = "jpg" if fmt == "jpg" else "png"mime = "image/jpeg" if fmt == "jpg" else "image/png"buf = BytesIO()pil_image.save(buf, format=pil_format)return st.download_button(label=label,data=buf.getvalue(),file_name=f'{filename}.{file_format}',mime=mime,)st.title("AI Photo Background Removal")st.image(Image.open("assets/demo.jpg"))st.write("""You want to remove your photo background, but don't have the time and effort to learn photo editing skills?**This app will change or remove your photo background, in seconds.**""")uploaded_file = st.file_uploader(label="Upload your photo here",accept_multiple_files=False, type=["png", "jpg", "jpeg"],)if uploaded_file is not None:with st.expander("Original photo", expanded=True):if uploaded_file is not None:st.image(uploaded_file)else:st.warning("You haven't uploaded any photo yet")in_mode = st.selectbox("Choose background color", ["Transparent (PNG)", "White", "Black", "Green", "Red", "Blue"])in_submit = st.button("Submit")if uploaded_file is not None and in_submit:img_input = Image.open(uploaded_file)with st.spinner("AI is doing magic to your photo. Please wait..."):hexmap = {"Transparent (PNG)": "#000000","Black": "#000000","White": "#FFFFFF","Green": "#22EE22","Red": "#EE2222","Blue": "#2222EE",}alpha = 0.0 if in_mode == "Transparent (PNG)" else 1.0img_matte = matte(img_input)img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[in_mode])with st.expander("Success!", expanded=True):st.image(img_output)uploaded_name = os.path.splitext(uploaded_file.name)[0]image_download_button(pil_image=img_output,filename=uploaded_name,fmt="png")12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import gradio as grfrom PIL import Imagefrom io import BytesIOfrom src.utils import change_background, mattedef process_image(uploaded_file, background_color):# Add debugging statementprint(f"background_color received: {background_color}")# Default value checkif background_color is None:return "Please select a background color."hexmap = {"Transparent (PNG)": "#000000","Black": "#000000","White": "#FFFFFF","Green": "#22EE22","Red": "#EE2222","Blue": "#2222EE",}alpha = 0.0 if background_color == "Transparent (PNG)" else 1.0img_input = uploaded_file # No need to open, it's already a PIL imageimg_matte = matte(img_input)img_output = change_background(img_input, img_matte, background_alpha=alpha, background_hex=hexmap[background_color])return img_outputwith gr.Blocks() as demo:gr.Markdown("""# AI Photo Background RemovalYou want to remove your photo background, but don't have the time and effort to learn photo editing skills?**This app will change or remove your photo background, in seconds.**""")with gr.Row():image_input = gr.Image(type="pil", label="Upload your photo here")bg_color = gr.Dropdown(choices=["Transparent (PNG)", "White", "Black", "Green", "Red", "Blue"], label="Choose background color")output_image = gr.Image(type="pil", label="Processed Image")btn = gr.Button("Submit")btn.click(fn=process_image, inputs=[image_input, bg_color], outputs=output_image)gr.Examples(examples=[["assets/demo.jpg", "Transparent (PNG)"]],inputs=[image_input, bg_color])if __name__ == "__main__":demo.launch()
2 unmodified lines345672 unmodified linesnumpyopencv-python-headlessmatplotlibstreamlitaltair<52 unmodified lines345672 unmodified linesnumpyopencv-python-headlessmatplotlibaltair<5gradio
.streamlit/config.tomlDeleted-6
app.py+44-69
assets/demo.jpgDeleted
assets/test_character.pngAdded

requirements.txt+2-2