Spaces:
Running
Running
Update app.py
Browse files1223456677891011121314151617181912131415202122232425262728293031323334353637383940414243444546174748495051521920212253545556575859606162636465666768697071727374757677787980818283848524252627282930318687888990919293949596979899100101102103104105106107108109110111112113114115116117118119import gradio as grfrom PIL import Imagefrom PIL import Image, ImageOpsimport rembgimport ioimport numpy as np
def remove_background(image): # Convert image to bytesdef remove_background_add_outline(image, outline_size=5, outline_color=(255, 255, 255)): if image is None: return None, None # Convert image to bytes for rembg img_byte_arr = io.BytesIO() 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") # Prepare outputs processed_img = Image.open(io.BytesIO(processed_bytes)) file_obj = io.BytesIO(processed_bytes) file_obj.name = "no_background.png" # Required for Gradio # Add outline if outline_size > 0: # Create a mask from the alpha channel alpha = processed_img.split()[3] # Create a slightly larger mask by dilating larger_mask = ImageOps.expand(alpha, outline_size, fill=255) # Create an outline image outline_img = Image.new('RGBA', larger_mask.size, (0, 0, 0, 0)) outline_color_with_alpha = outline_color + (255,) # Add alpha to color # Draw the outline for x in range(larger_mask.width): for y in range(larger_mask.height): if larger_mask.getpixel((x, y)) == 255 and (x < outline_size or y < outline_size or x >= processed_img.width or y >= processed_img.height or processed_img.getpixel((x-outline_size, y-outline_size))[3] == 0): outline_img.putpixel((x, y), outline_color_with_alpha) # Combine the outline with the original image final_img = Image.new('RGBA', larger_mask.size, (0, 0, 0, 0)) final_img.paste(outline_img, (0, 0), outline_img) final_img.paste(processed_img, (outline_size, outline_size), processed_img) else: final_img = processed_img return processed_img, file_obj # Prepare file for download download_path = "sticker.png" final_img.save(download_path, format="PNG") return final_img, download_path
# Gradio Interfaceimage_upload = gr.Image(label="Upload Image", type="pil")output_preview = gr.Image(label="Preview", type="pil")output_file = gr.File(label="Download PNG")# Faster but simpler method for adding outlinedef add_outline_simple(image, outline_size=5, outline_color=(255, 255, 255)): if image is None: return None, None # Convert image to bytes for rembg img_byte_arr = io.BytesIO() 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") # Create a larger background image with the outline color if outline_size > 0: background = Image.new('RGBA', (processed_img.width + 2*outline_size, processed_img.height + 2*outline_size), outline_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 # Prepare file for download download_path = "sticker.png" final_img.save(download_path, format="PNG") return final_img, download_path
interface = gr.Interface( fn=remove_background, inputs=image_upload, outputs=[output_preview, output_file], title=" PNG Background Remover", description="Remove background and preview/download as transparent PNG", allow_flagging="never")# Gradio Interfacewith gr.Blocks(title="Sticker Maker") as interface: gr.Markdown("# Sticker Maker") gr.Markdown("Remove background, add white outline, and download as transparent PNG") with gr.Row(): with gr.Column(): image_upload = gr.Image(label="Upload Image", type="pil") 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") with gr.Column(): output_preview = gr.Image(label="Preview", type="pil") output_file = gr.File(label="Download PNG") def process_image(image, outline_size, outline_color): # 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) # Use the simpler method for better performance return add_outline_simple(image, int(outline_size), rgb_color) create_btn.click( fn=process_image, inputs=[image_upload, outline_size, outline_color], outputs=[output_preview, output_file] )
if __name__ == "__main__": interface.launch()app.py+105-20