-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc56da8
commit dd7f0e7
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("ʀᴇᴘʟʏ ᴏɴʟʏ ᴛᴏ ᴀ ᴘʜᴏᴛᴏ ᴛᴏ ʀᴇᴍᴏᴠᴇ ɪᴛ'ꜱ ʙᴀᴄᴋɢʀᴏᴜɴᴅ") | ||
|