Spaces:
Running
Running
Update app.py
Browse files122345678891011121112131415161718192021222324252627281415161718192021222324252627282930313233343536373839404142434445464748495051313252535455355657373839585960616263import gradio as grfrom PIL import Imagefrom PIL import Image, ImageChops, ImageFilterimport rembgimport ioimport zipfile
def remove_background(image): """Remove background and return transparent PNG""" """Remove background using rembg""" 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") result = rembg.remove(img_byte_arr.getvalue()) return Image.open(io.BytesIO(result)).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 image img = Image.open(img_file).convert("RGBA") processed_img = remove_background(img) # Save to in-memory buffer img_byte_arr = io.BytesIO() processed_img.save(img_byte_arr, format='PNG') # Add to ZIP zip_file.writestr(f"processed_{i+1}.png", img_byte_arr.getvalue()) zip_buffer.seek(0) return zip_bufferdef 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 layer outline = Image.new('RGBA', image.size, (*outline_color, 255)) outline.putalpha(outline_mask) # Combine layers final = Image.alpha_composite(outline, image) return final
def 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 ZIP if 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 (Batch)", file_types=["image"])output_zip = gr.File(label="Download Processed Images (ZIP)")image_upload = gr.Files(label="Upload Images (Single or Batch)", file_types=["image"])output = gr.File(label="Download Sticker(s)")
interface = gr.Interface( fn=process_batch, fn=process_images, inputs=image_upload, outputs=output_zip, title="Transparent Background Remover (Batch)", description="Upload multiple images, remove backgrounds, and download as transparent PNGs (batch).", outputs=output, title="✨ Sticker Maker", description="Remove background, add white outline, and download as PNG(s)",)
if __name__ == "__main__":app.py+46-25