From 772a5861aa073707f5673c992aed44a1faff6a1f Mon Sep 17 00:00:00 2001 From: arbadacarba <63317640+arbadacarbaYK@users.noreply.github.com> Date: Thu, 30 May 2024 02:30:44 +0200 Subject: [PATCH] Delete files After moving from github actions to local DM files need to be deleted after processing/delivering --- pixelateTG.py | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/pixelateTG.py b/pixelateTG.py index b75362f..d47aa95 100644 --- a/pixelateTG.py +++ b/pixelateTG.py @@ -1,5 +1,5 @@ import os -from dotenv import load_dotenv # Import the load_dotenv function from python-dotenv +from dotenv import load_dotenv import cv2 import random import imageio @@ -12,10 +12,10 @@ # Load environment variables from .env file load_dotenv() -TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') # Get the Telegram bot token from the environment variable +TOKEN = os.getenv('TELEGRAM_BOT_TOKEN') MAX_THREADS = 15 PIXELATION_FACTOR = 0.04 -RESIZE_FACTOR = 1.5 # Common resize factor +RESIZE_FACTOR = 1.5 executor = ThreadPoolExecutor(max_workers=MAX_THREADS) def start(update: Update, context: CallbackContext) -> None: @@ -39,32 +39,25 @@ def overlay(photo_path, user_id, overlay_type, resize_factor, bot): overlay_image = cv2.imread(random_overlay, cv2.IMREAD_UNCHANGED) original_aspect_ratio = overlay_image.shape[1] / overlay_image.shape[0] - # Calculate new dimensions for the overlay new_width = int(resize_factor * w) new_height = int(new_width / original_aspect_ratio) - # Ensure the overlay is centered on the face center_x = x + w // 2 center_y = y + h // 2 - # Overlay position adjusted for better centering overlay_x = int(center_x - 0.5 * resize_factor * w) - int(0.1 * resize_factor * w) overlay_y = int(center_y - 0.5 * resize_factor * h) - int(0.1 * resize_factor * w) - # Clamp values to ensure they are within the image boundaries overlay_x = max(0, overlay_x) overlay_y = max(0, overlay_y) - # Resize the overlay image overlay_image_resized = cv2.resize(overlay_image, (new_width, new_height), interpolation=cv2.INTER_AREA) - # Calculate the regions of interest (ROI) roi_start_x = overlay_x roi_start_y = overlay_y roi_end_x = min(image.shape[1], overlay_x + new_width) roi_end_y = min(image.shape[0], overlay_y + new_height) - # Blend the overlay onto the image try: overlay_part = overlay_image_resized[:roi_end_y - roi_start_y, :roi_end_x - roi_start_x] alpha_mask = overlay_part[:, :, 3] / 255.0 @@ -81,7 +74,6 @@ def overlay(photo_path, user_id, overlay_type, resize_factor, bot): cv2.imwrite(processed_path, image, [int(cv2.IMWRITE_JPEG_QUALITY), 95]) return processed_path - # Overlay functions def liotta_overlay(photo_path, user_id, bot): return overlay(photo_path, user_id, 'liotta', RESIZE_FACTOR, bot) @@ -124,6 +116,7 @@ def pixelate_faces(update: Update, context: CallbackContext) -> None: if not faces: update.message.reply_text('No faces detected in the image.') + os.remove(photo_path) # Delete the downloaded file return keyboard = [ @@ -132,15 +125,11 @@ def pixelate_faces(update: Update, context: CallbackContext) -> None: InlineKeyboardButton("☠️ Skull", callback_data=f'skull_overlay_{session_id}')], [InlineKeyboardButton("🐈‍⬛ Cats", callback_data=f'cats_overlay_{session_id}'), InlineKeyboardButton("🐸 Pepe", callback_data=f'pepe_overlay_{session_id}'), - InlineKeyboardButton("🏆 Chad", callback_data=f'chad_overlay_{session_id}')] + InlineKeyboardButton("🏆 Chad", callback_data=f'chad_overlay_{session_id}')], + [InlineKeyboardButton("⚔️ Pixel", callback_data=f'pixelate_{session_id}')], + [InlineKeyboardButton("CLOSE ME", callback_data=f'cancel_{session_id}')] ] - # Check if it's a private chat, if yes, include the "⚔️ Pixel" button - if update.message.chat.type == 'private': - keyboard.append([InlineKeyboardButton("⚔️ Pixel", callback_data=f'pixelate_{session_id}')]) - - keyboard.append([InlineKeyboardButton("CLOSE ME", callback_data=f'cancel_{session_id}')]) - reply_markup = InlineKeyboardMarkup(keyboard) user_data[session_id] = {'photo_path': photo_path, 'user_id': update.message.from_user.id} @@ -157,10 +146,12 @@ def pixelate_faces(update: Update, context: CallbackContext) -> None: processed_gif_path = process_gif(gif_path, session_id, str(uuid4()), context.bot) context.bot.send_animation(chat_id=update.message.chat_id, animation=open(processed_gif_path, 'rb')) + os.remove(gif_path) # Delete the downloaded file + os.remove(processed_gif_path) # Delete the processed file + else: update.message.reply_text('Please send either a photo or a GIF.') - def pixelate_command(update: Update, context: CallbackContext) -> None: if update.message.reply_to_message and update.message.reply_to_message.photo: session_id = str(uuid4()) @@ -177,6 +168,7 @@ def pixelate_command(update: Update, context: CallbackContext) -> None: if not faces: update.message.reply_text('No faces detected in the image.') + os.remove(photo_path) # Delete the downloaded file return keyboard = [ @@ -201,22 +193,16 @@ def process_image(photo_path, user_id, session_id, bot): faces = detect_heads(image) for (x, y, w, h) in faces: - # Define the region of interest (ROI) roi = image[y:y+h, x:x+w] - - # Apply pixelation to the ROI - pixelation_size = max(1, int(PIXELATION_FACTOR * min(w, h))) # Ensure pixelation size is at least 1 + pixelation_size = max(1, int(PIXELATION_FACTOR * min(w, h))) pixelated_roi = cv2.resize(roi, (pixelation_size, pixelation_size), interpolation=cv2.INTER_NEAREST) pixelated_roi = cv2.resize(pixelated_roi, (w, h), interpolation=cv2.INTER_NEAREST) - - # Replace the original face region with the pixelated ROI image[y:y+h, x:x+w] = pixelated_roi processed_path = f"processed/{user_id}_{session_id}_pixelated.jpg" cv2.imwrite(processed_path, image, [int(cv2.IMWRITE_JPEG_QUALITY), 95]) return processed_path - def button_callback(update: Update, context: CallbackContext) -> None: query = update.callback_query query.answer() @@ -235,6 +221,7 @@ def button_callback(update: Update, context: CallbackContext) -> None: if session_id in chat_data: del chat_data[session_id] query.message.delete() + os.remove(photo_path) # Delete the downloaded file return processed_path = None @@ -256,10 +243,11 @@ def button_callback(update: Update, context: CallbackContext) -> None: if processed_path: context.bot.send_photo(chat_id=query.message.chat_id, photo=open(processed_path, 'rb')) + os.remove(processed_path) # Delete the processed file + os.remove(photo_path) # Delete the downloaded file def main() -> None: updater = Updater(TOKEN) - dispatcher = updater.dispatcher dispatcher.add_handler(CommandHandler("start", start))