Spaces:
Running
Running
Update app.py
Browse files1 unmodified line234565678912131011151617181213141516171820192021226 unmodified lines293031323334351 unmodified linefrom PIL import Imageimport rembgimport ioimport tempfileimport os
def remove_background(image): # Process image img_byte_arr = io.BytesIO() image.save(img_byte_arr, format='PNG') result_bytes = rembg.remove(img_byte_arr.getvalue()) processed_img = Image.open(io.BytesIO(result_bytes)).convert("RGBA") processed_bytes = rembg.remove(img_byte_arr.getvalue()) # Save to temporary file for download temp_dir = tempfile.mkdtemp() file_path = os.path.join(temp_dir, "no_background.png") processed_img.save(file_path) # Create PIL image for preview processed_img = Image.open(io.BytesIO(processed_bytes)) # Prepare download file download_bytes = io.BytesIO(processed_bytes) download_bytes.seek(0) # Reset buffer position return processed_img, file_path return processed_img, download_bytes
# Gradio Interfaceimage_upload = gr.Image(label="Upload Image", type="pil")6 unmodified lines outputs=[output_preview, output_file], title=" PNG Background Remover", description="Remove background and preview/download as transparent PNG", allow_flagging="never")
if __name__ == "__main__":app.py+9-9