Spaces:
Running
Running
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142import gradio as grfrom PIL import Imageimport rembgimport ioimport zipfiledef remove_background(image):"""Remove background and return transparent PNG"""img_byte_arr = io.BytesIO()image.save(img_byte_arr, format='PNG')result_bytes = rembg.remove(img_byte_arr.getvalue())return Image.open(io.BytesIO(result_bytes)).convert("RGBA")def process_batch(image_files):"""Process batch of images and return ZIP of transparent PNGs"""zip_buffer = io.BytesIO()with zipfile.ZipFile(zip_buffer, "w") as zip_file:for i, img_file in enumerate(image_files):# Open and process imageimg = Image.open(img_file).convert("RGBA")processed_img = remove_background(img)# Save to in-memory bufferimg_byte_arr = io.BytesIO()processed_img.save(img_byte_arr, format='PNG')# Add to ZIPzip_file.writestr(f"processed_{i+1}.png", img_byte_arr.getvalue())zip_buffer.seek(0)return zip_buffer# Gradio Interfaceimage_upload = gr.Files(label="Upload Images (Batch)", file_types=["image"])output_zip = gr.File(label="Download Processed Images (ZIP)")interface = gr.Interface(fn=process_batch,inputs=image_upload,outputs=output_zip,title="Transparent Background Remover (Batch)",description="Upload multiple images, remove backgrounds, and download as transparent PNGs (batch).",)if __name__ == "__main__":123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import gradio as grfrom PIL import Image, ImageChops, ImageFilterimport rembgimport ioimport zipfiledef remove_background(image):"""Remove background using rembg"""img_byte_arr = io.BytesIO()image.save(img_byte_arr, format='PNG')result = rembg.remove(img_byte_arr.getvalue())return Image.open(io.BytesIO(result)).convert("RGBA")def add_outline(image, outline_width=2, outline_color=(255, 255, 255)):"""Add white outline to transparent image"""alpha = image.split()[-1]dilated_alpha = alpha.filter(ImageFilter.MaxFilter(outline_width * 2 + 1))outline_mask = ImageChops.difference(dilated_alpha, alpha)# Create outline layeroutline = Image.new('RGBA', image.size, (*outline_color, 255))outline.putalpha(outline_mask)# Combine layersfinal = Image.alpha_composite(outline, image)return finaldef process_images(image_files):processed = []for img_file in image_files:img = Image.open(img_file).convert("RGBA")no_bg = remove_background(img)sticker = add_outline(no_bg)processed.append(sticker)# Return single PNG or ZIPif len(processed) == 1:buf = io.BytesIO()processed[0].save(buf, format='PNG')return buf.getvalue()else:zip_buf = io.BytesIO()with zipfile.ZipFile(zip_buf, 'w') as zip_file:for i, sticker in enumerate(processed):buf = io.BytesIO()sticker.save(buf, format='PNG')zip_file.writestr(f'sticker_{i+1}.png', buf.getvalue())zip_buf.seek(0)return zip_buf# Gradio Interfaceimage_upload = gr.Files(label="Upload Images (Single or Batch)", file_types=["image"])output = gr.File(label="Download Sticker(s)")interface = gr.Interface(fn=process_images,inputs=image_upload,outputs=output,title="✨ Sticker Maker",description="Remove background, add white outline, and download as PNG(s)",)if __name__ == "__main__":
app.py+46-25