Spaces:
Running
Running
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334import gradio as grfrom PIL import Imageimport rembgimport iodef remove_background(image):# Convert image to bytesimg_byte_arr = io.BytesIO()image.save(img_byte_arr, format='PNG')processed_bytes = rembg.remove(img_byte_arr.getvalue())# Prepare outputsprocessed_img = Image.open(io.BytesIO(processed_bytes))file_obj = io.BytesIO(processed_bytes)file_obj.name = "no_background.png" # Required for Gradioreturn processed_img, file_obj# 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")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")if __name__ == "__main__":interface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119import gradio as grfrom PIL import Image, ImageOpsimport rembgimport ioimport numpy as npdef 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 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 outlineif outline_size > 0:# Create a mask from the alpha channelalpha = processed_img.split()[3]# Create a slightly larger mask by dilatinglarger_mask = ImageOps.expand(alpha, outline_size, fill=255)# Create an outline imageoutline_img = Image.new('RGBA', larger_mask.size, (0, 0, 0, 0))outline_color_with_alpha = outline_color + (255,) # Add alpha to color# Draw the outlinefor 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 orx >= processed_img.width or y >= processed_img.height orprocessed_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 imagefinal_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# Prepare file for downloaddownload_path = "sticker.png"final_img.save(download_path, format="PNG")return final_img, download_path# 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 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")# Create a larger background image with the outline colorif 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 backgroundbackground.paste(processed_img, (outline_size, outline_size), processed_img)final_img = backgroundelse:final_img = processed_img# Prepare file for downloaddownload_path = "sticker.png"final_img.save(download_path, format="PNG")return final_img, download_path# 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 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)# Use the simpler method for better performancereturn 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