Files
userbot/modules/removebg.py
T
forust 7fb0a0e179 chore: batch lint fixes across userbot and edu_master
- S113: Add timeout=10 to all requests calls (74 fixes)
- E722: Replace bare except: with except Exception:
- B904: Replace redundant re-raise with bare raise
- E402: Add noqa for intentional late imports after import_library()
- S102/S307/S310/S311/S603/S605/S606/S607/S108: Add noqa for intentional usage
- F601: Fix duplicate dict key in unsplash.py
- N802: Rename ReplyCheck -> reply_check with backward compat alias
- N813: Rename bs -> BS in icons.py
- B007/B020: Rename loop var _j in animations.py
- SIM102: Collapse nested if in autofwd.py
- SIM113: Use enumerate() in calculator.py
- A002: Add noqa for builtin shadowing in admlist.py
- F811: Add noqa for cohere redefinition
- edu_master: Fix ARG001, S108, S110, SIM117, apply --unsafe-fixes
- Add modules_list.txt with full module inventory
2026-06-19 15:36:25 +02:00

184 lines
6.7 KiB
Python

# Copyright (C) 2020-2021 by DevsExpo@Github, < https://github.com/DevsExpo >.
#
# This file is part of < https://github.com/DevsExpo/FridayUserBot > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/DevsExpo/blob/master/LICENSE >
#
# All rights reserved.
# Modifed by @moonuserbot
import io
import os
from datetime import datetime
from functools import wraps
from io import BytesIO
import requests
from PIL import Image
from pyrogram import Client, enums, filters
from pyrogram.types import Message
from utils.config import rmbg_key
from utils.misc import modules_help, prefix
from utils.scripts import edit_or_reply, format_exc
async def convert_to_image(message, client) -> None | str:
"""Convert Most Media Formats To Raw Image"""
if not message:
return None
if not message.reply_to_message:
return None
final_path = None
if not (
message.reply_to_message.video
or message.reply_to_message.photo
or message.reply_to_message.sticker
or message.reply_to_message.media
or message.reply_to_message.animation
or message.reply_to_message.audio
or message.reply_to_message.document
):
return None
if message.reply_to_message.photo:
final_path = await message.reply_to_message.download()
elif message.reply_to_message.sticker:
if message.reply_to_message.sticker.mime_type == 'image/webp':
final_path = 'webp_to_png_s_proton.png'
path_s = await message.reply_to_message.download()
im = Image.open(path_s)
im.save(final_path, 'PNG')
else:
path_s = await client.download_media(message.reply_to_message)
final_path = 'lottie_proton.png'
cmd = f'lottie_convert.py --frame 0 -if lottie -of png {path_s} {final_path}'
await exec(cmd) # skipcq # noqa: S102
elif message.reply_to_message.audio:
thumb = message.reply_to_message.audio.thumbs[0].file_id
final_path = await client.download_media(thumb)
elif message.reply_to_message.video or message.reply_to_message.animation:
final_path = 'fetched_thumb.png'
vid_path = await client.download_media(message.reply_to_message)
await exec(f'ffmpeg -i {vid_path} -filter:v scale=500:500 -an {final_path}') # skipcq # noqa: S102
elif message.reply_to_message.document:
if (
message.reply_to_message.document.mime_type == 'image/jpeg'
or message.reply_to_message.document.mime_type == 'image/png'
):
final_path = await message.reply_to_message.download()
elif message.reply_to_message.document.mime_type == 'image/webp':
final_path = 'webp_to_png_s_proton.png'
path_s = await message.reply_to_message.download()
im = Image.open(path_s)
im.save(final_path, 'PNG')
else:
return None
return final_path
def remove_background(photo_data):
with open(photo_data, 'rb') as image_file:
image_data = image_file.read()
response = requests.post(
'https://api.remove.bg/v1.0/removebg',
files={'image_file': image_data},
data={'size': 'auto'},
headers={'X-Api-Key': rmbg_key},
timeout=10,
)
if response.status_code == 200:
return BytesIO(response.content)
print('Error:', response.status_code, response.text)
return None
def _check_rmbg(func):
@wraps(func)
async def check_rmbg(client: Client, message: Message):
if not rmbg_key:
await edit_or_reply(
message,
"<code>Is Your RMBG Api 'rmbg_key' Valid Or You Didn't Add It??</code>",
)
else:
await func(client, message)
return check_rmbg
@Client.on_message(filters.command('rmbg', prefix) & filters.me)
@_check_rmbg
async def rmbg(client: Client, message: Message):
pablo = await edit_or_reply(message, '<code>Processing...</code>')
if not message.reply_to_message:
await pablo.edit('<code>Reply To A Image Please!</code>')
return
cool = await convert_to_image(message, client)
if not cool:
await pablo.edit('<code>Reply to a valid media first.</code>')
return
start = datetime.now()
await pablo.edit('sending to ReMove.BG')
input_file_name = cool
files = {
'image_file': (input_file_name, open(input_file_name, 'rb')),
}
r = requests.post(
'https://api.remove.bg/v1.0/removebg',
headers={'X-Api-Key': rmbg_key},
files=files,
allow_redirects=True,
stream=True,
timeout=10,
)
if os.path.exists(cool):
os.remove(cool)
output_file_name = r
contentType = output_file_name.headers.get('content-type')
if 'image' in contentType:
with io.BytesIO(output_file_name.content) as remove_bg_image:
remove_bg_image.name = 'BG_rem.png'
await client.send_document(message.chat.id, remove_bg_image, reply_to_message_id=message.id)
end = datetime.now()
ms = (end - start).seconds
await pablo.edit(f"<code>Removed image's Background in {ms} seconds.")
if os.path.exists('BG_rem.png'):
os.remove('BG_rem.png')
else:
await pablo.edit('ReMove.BG API returned Errors.' + f'\n`{output_file_name.content.decode("UTF-8")}')
@Client.on_message(filters.command('rebg', prefix) & filters.me)
async def rembg(client: Client, message: Message):
await message.edit('<code>Processing...</code>')
chat_id = message.chat.id
try:
try:
photo_data = await message.download()
except ValueError:
try:
photo_data = await message.reply_to_message.download()
except ValueError:
await message.edit('<b>File not found</b>')
return
background_removed_data = remove_background(photo_data)
if background_removed_data:
await message.delete()
await client.send_photo(chat_id, photo=background_removed_data, caption='Background removed!')
else:
await message.edit_text(
"`Is Your RMBG Api 'rmbg_key' Valid Or You Didn't Add It??`\n **Check logs for details**",
parse_mode=enums.ParseMode.MARKDOWN,
)
except Exception as e:
await message.reply_text(f'An error occurred: {format_exc(e)}')
finally:
if os.path.exists(photo_data):
os.remove(photo_data)
modules_help['removebg'] = {
'rebg [reply to image]*': 'remove background from image without transparency',
'rmbg [reply to image]*': 'remove background from image with transparency',
}