Spaces:
Running
Running
Update app.py
Browse files1 unmodified line2345567810910111213141615161719181921202122232524252627293031323328293031323334353637383940394041424341424344454649504748495051525553545556575859585961626364606162636465666667686768697070717273747575767778797677818283848578798081828384858687888889899091929393941 unmodified linefrom PIL import Imageimport rembgimport ioimport os
def process_image(image, outline_size, outline_color, add_outline): if image is None: return None
# Convert hex color to RGB r = int(outline_color[1:3], 16) g = int(outline_color[3:5], 16) b = int(outline_color[5:7], 16) rgb_color = (r, g, b)
# Convert image to bytes for rembg img_byte_arr = io.BytesIO() image.save(img_byte_arr, format='PNG') image.save(img_byte_arr, format="PNG") img_byte_arr.seek(0)
# Remove background processed_bytes = rembg.remove(img_byte_arr.getvalue()) processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")
# Add outline if selected if add_outline and outline_size > 0: # Create a larger background image with the outline color background = Image.new('RGBA', (processed_img.width + 2*outline_size, processed_img.height + 2*outline_size), rgb_color + (255,)) background = Image.new( "RGBA", ( processed_img.width + 2 * outline_size, processed_img.height + 2 * outline_size, ), rgb_color + (255,), ) # Paste the processed image onto the background background.paste(processed_img, (outline_size, outline_size), processed_img) final_img = background else: final_img = processed_img # Save as PNG output_file = "sticker.png" final_img.save(output_file, format="PNG")
return final_img
# Gradio Interfacewith gr.Blocks(title="Sticker Maker") as interface: gr.Markdown("# Sticker Maker") gr.Markdown("Upload an image to remove background and add optional outline") gr.Markdown("Upload an image to remove the background and optionally add an outline.")
with gr.Row(): with gr.Column(): image_upload = gr.Image(label="Upload Image", type="pil") add_outline = gr.Checkbox(label="Add Outline", value=True) outline_size = gr.Slider(label="Outline Thickness", minimum=0, maximum=20, value=5, step=1) outline_size = gr.Slider( label="Outline Thickness", minimum=0, maximum=20, value=5, step=1 ) outline_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF") create_btn = gr.Button("Create Sticker", variant="primary") download_btn = gr.Button("Download Sticker", visible=False)
with gr.Column(): # Output image is visible from the start output_image = gr.Image(label="Preview with Transparent Background", type="pil") # Make outline options visible only when add_outline is checked output_image = gr.Image( label="Preview and Download", type="pil", interactive=False ) download_btn = gr.Button("Download Sticker", variant="secondary")
# Show/hide outline options based on checkbox add_outline.change( fn=lambda x: gr.update(visible=x), inputs=add_outline, outputs=[outline_size, outline_color] fn=lambda x: gr.update(visible=x), inputs=add_outline, outputs=[outline_size, outline_color], )
# Process image and show result create_btn.click( fn=process_image, inputs=[image_upload, outline_size, outline_color, add_outline], outputs=output_image ).then( fn=lambda: gr.update(visible=True), inputs=None, outputs=download_btn outputs=output_image, ) # Handle manual download def download_sticker(): return "sticker.png"
# Handle download def download_sticker(image): if image: img_byte_arr = io.BytesIO() image.save(img_byte_arr, format="PNG") img_byte_arr.seek(0) return img_byte_arr
download_btn.click( fn=download_sticker, inputs=None, outputs=gr.File(label="Download") inputs=output_image, outputs=gr.File(label="Sticker.png"), )
if __name__ == "__main__": interface.launch(debug=True) interface.launch(debug=True)app.py+43-42