Spaces:
Running
Running
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243import gradio as grfrom PIL import Imageimport rembgimport ioimport zipfile
def 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 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_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__": interface.launch()app.pyAdded+43