|
|
import gradio as gr |
|
|
import requests |
|
|
from io import BytesIO |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
REMOVE_BG_API_KEY = "e35sbLVzVUkbWALM3PZicdZ7" |
|
|
|
|
|
|
|
|
def remove_background(image): |
|
|
try: |
|
|
|
|
|
buffered = BytesIO() |
|
|
image.save(buffered, format="PNG") |
|
|
image_bytes = buffered.getvalue() |
|
|
|
|
|
|
|
|
response = requests.post( |
|
|
"https://api.remove.bg/v1.0/removebg", |
|
|
files={"image_file": image_bytes}, |
|
|
data={"size": "auto"}, |
|
|
headers={"X-Api-Key": REMOVE_BG_API_KEY}, |
|
|
) |
|
|
|
|
|
if response.status_code == 200: |
|
|
|
|
|
output_image = Image.open(BytesIO(response.content)) |
|
|
return output_image |
|
|
else: |
|
|
|
|
|
return f"Error: {response.status_code} - {response.text}" |
|
|
except Exception as e: |
|
|
return str(e) |
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=remove_background, |
|
|
inputs=gr.Image(type="pil"), |
|
|
outputs=gr.Image(type="pil"), |
|
|
title="إزالة الخلفية من الصور", |
|
|
description="remove bg" |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
interface.launch() |