Skip to content

Commit

Permalink
Create bgremove.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Badhacker98 authored Oct 17, 2024
1 parent bc56da8 commit dd7f0e7
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions shizuchat/modules/bgremove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
import aiohttp
import aiofiles
from shizuchat import shizuchat
from aiohttp import ContentTypeError
from shizuchat import shizuchat as app
from pyrogram import filters

def check_filename(filroid):
if os.path.exists(filroid):
no = 1
while True:
ult = "{0}_{2}{1}".format(*os.path.splitext(filroid) + (no,))
if os.path.exists(ult):
no += 1
else:
return ult
return filroid

async def RemoveBG(input_file_name):
headers = {"X-API-Key": "P6oDw1YNnMdkoMXxmWJinHQK"}
files = {"image_file": open(input_file_name, "rb").read()}
async with aiohttp.ClientSession() as ses:
async with ses.post(
"https://api.remove.bg/v1.0/removebg", headers=headers, data=files
) as y:
contentType = y.headers.get("content-type")
if "image" not in contentType:
return False, (await y.json())

name = check_filename("alpha.png")
file = await aiofiles.open(name, "wb")
await file.write(await y.read())
await file.close()
return True, name


@shizuchat.on_cmd(["rmbg"])
async def rmbg(bot, message):
rmbg = await message.reply("ᴘʀᴏᴄᴇꜱꜱɪɴɢ...")
replied = message.reply_to_message
if not replied:
return await rmbg.edit("ʀᴇᴘʟʏ ᴛᴏ ᴀ ᴘʜᴏᴛᴏ ᴛᴏ ʀᴇᴍᴏᴠᴇ ɪᴛ'ꜱ ʙᴀᴄᴋɢʀᴏᴜᴅ")

if replied.photo:
photo = await bot.download_media(replied)
x, y = await RemoveBG(photo)
os.remove(photo)
if not x:
bruh = y["errors"][0]
details = bruh.get("detail", "")
return await rmbg.edit(f"ERROR ~ {bruh['title']},\n{details}")
await message.reply_photo(photo=y,caption="Here is your Image without Background")
await message.reply_document(document=y)
await rmbg.delete()
return os.remove(y)
await rmbg.edit("ʀᴇᴘʟʏ ᴏɴʟʏ ᴛᴏ ᴀ ᴘʜᴏᴛᴏ ᴛᴏ ʀᴇᴍᴏᴠᴇ ɪᴛ'ꜱ ʙᴀᴄᴋɢʀᴏᴜɴᴅ")

0 comments on commit dd7f0e7

Please sign in to comment.