Spaces:
Running
Running
Update app.py
Browse files1 unmodified line234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192931 unmodified linefrom PIL import Imageimport rembgimport ioimport osdef process_image(image, outline_size, outline_color, add_outline):if image is None:return None# Convert hex color to RGBr = 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 rembgimg_byte_arr = io.BytesIO()image.save(img_byte_arr, format='PNG')img_byte_arr.seek(0)# Remove backgroundprocessed_bytes = rembg.remove(img_byte_arr.getvalue())processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")# Add outline if selectedif add_outline and outline_size > 0:# Create a larger background image with the outline colorbackground = Image.new('RGBA',(processed_img.width + 2*outline_size,processed_img.height + 2*outline_size),rgb_color + (255,))# Paste the processed image onto the backgroundbackground.paste(processed_img, (outline_size, outline_size), processed_img)final_img = backgroundelse:final_img = processed_img# Save as PNGoutput_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")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_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 startoutput_image = gr.Image(label="Preview with Transparent Background", type="pil")# Make outline options visible only when add_outline is checkedadd_outline.change(fn=lambda x: gr.update(visible=x),inputs=add_outline,outputs=[outline_size, outline_color])# Process image and show resultcreate_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)# Handle manual downloaddef download_sticker():return "sticker.png"download_btn.click(fn=download_sticker,inputs=None,outputs=gr.File(label="Download"))if __name__ == "__main__":interface.launch(debug=True)1 unmodified line23456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293941 unmodified linefrom PIL import Imageimport rembgimport iodef process_image(image, outline_size, outline_color, add_outline):if image is None:return None# Convert hex color to RGBr = 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 rembgimg_byte_arr = io.BytesIO()image.save(img_byte_arr, format="PNG")img_byte_arr.seek(0)# Remove backgroundprocessed_bytes = rembg.remove(img_byte_arr.getvalue())processed_img = Image.open(io.BytesIO(processed_bytes)).convert("RGBA")# Add outline if selectedif add_outline and outline_size > 0:# Create a larger background image with the outline colorbackground = Image.new("RGBA",(processed_img.width + 2 * outline_size,processed_img.height + 2 * outline_size,),rgb_color + (255,),)# Paste the processed image onto the backgroundbackground.paste(processed_img, (outline_size, outline_size), processed_img)final_img = backgroundelse:final_img = processed_imgreturn final_img# Gradio Interfacewith gr.Blocks(title="Sticker Maker") as interface:gr.Markdown("# Sticker Maker")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_color = gr.ColorPicker(label="Outline Color", value="#FFFFFF")create_btn = gr.Button("Create Sticker", variant="primary")with gr.Column():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 checkboxadd_outline.change(fn=lambda x: gr.update(visible=x),inputs=add_outline,outputs=[outline_size, outline_color],)# Process image and show resultcreate_btn.click(fn=process_image,inputs=[image_upload, outline_size, outline_color, add_outline],outputs=output_image,)# Handle downloaddef 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_arrdownload_btn.click(fn=download_sticker,inputs=output_image,outputs=gr.File(label="Sticker.png"),)if __name__ == "__main__":interface.launch(debug=True)
app.py+43-42