Image background remover api.
Note: it downloads extra large files on the server,if you are using free servers like vercel, seenode or render,it might take a long time to deploy or even fail to deploy. Use your own server or run on local host.
How to run on local host.
create a python file and name it main.py
inside main.py add the following code:
import requests
from PIL import Image
from rembg import remove
from flask import Flask, jsonify, request
from io import BytesIO
import os
import tempfile
def upload_to_telegraph(image_path):
with open(image_path, 'rb') as file:
response = requests.post('https://telegra.ph/upload', files={'file': file})
image_url = response.json()[0]['src']
return image_url
app = Flask(__name__)
@app.route("/")
def homepage():
return " /remove?img={img_url} "
@app.route("/remove")
def remove_bg():
url = request.args.get("img")
req = requests.get(url)
input_image = Image.open(BytesIO(req.content))
output_image = remove(input_image)
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
output_image.save(tmp_file.name, format="PNG")
tmp_file_path = tmp_file.name
try:
image_link = upload_to_telegraph(tmp_file_path)
finally:
os.remove(tmp_file_path)
return jsonify({"status": 200, "image": f"https://telegra.ph{image_link}"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Now create a file named requirements.txt
Inside it add this:
flask
rembg
Pillow
requests
Now open your terminal and navigate to where the files are located.
First install the modules used.
pip3 install -r requirements.txt
Wait until the process is done.
Now run the main file
python3 main.py
Now open any of your browser and type the following.
http://127.0.0.1:5000/remove?image={your image link here}
This will return an image link from telegra.ph
To get image links, use this bot:
https://t.me/OPTIMUSSERVICESBOT
Share with credits
Channel: @TheInfinityApisHub
Owner: @RealOptimusPrimeBot
Note: it downloads extra large files on the server,if you are using free servers like vercel, seenode or render,it might take a long time to deploy or even fail to deploy. Use your own server or run on local host.
How to run on local host.
create a python file and name it main.py
inside main.py add the following code:
import requests
from PIL import Image
from rembg import remove
from flask import Flask, jsonify, request
from io import BytesIO
import os
import tempfile
def upload_to_telegraph(image_path):
with open(image_path, 'rb') as file:
response = requests.post('https://telegra.ph/upload', files={'file': file})
image_url = response.json()[0]['src']
return image_url
app = Flask(__name__)
@app.route("/")
def homepage():
return " /remove?img={img_url} "
@app.route("/remove")
def remove_bg():
url = request.args.get("img")
req = requests.get(url)
input_image = Image.open(BytesIO(req.content))
output_image = remove(input_image)
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
output_image.save(tmp_file.name, format="PNG")
tmp_file_path = tmp_file.name
try:
image_link = upload_to_telegraph(tmp_file_path)
finally:
os.remove(tmp_file_path)
return jsonify({"status": 200, "image": f"https://telegra.ph{image_link}"})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Now create a file named requirements.txt
Inside it add this:
flask
rembg
Pillow
requests
Now open your terminal and navigate to where the files are located.
First install the modules used.
pip3 install -r requirements.txt
Wait until the process is done.
Now run the main file
python3 main.py
Now open any of your browser and type the following.
http://127.0.0.1:5000/remove?image={your image link here}
This will return an image link from telegra.ph
To get image links, use this bot:
https://t.me/OPTIMUSSERVICESBOT
Share with credits
Channel: @TheInfinityApisHub
Owner: @RealOptimusPrimeBot