Skip to content

Commit

Permalink
Delete files
Browse files Browse the repository at this point in the history
After moving from github actions to local DM files need to be deleted after processing/delivering
  • Loading branch information
arbadacarbaYK authored May 30, 2024
1 parent 35bae96 commit 772a586
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions pixelateTG.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 = [
Expand All @@ -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}

Expand All @@ -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())
Expand All @@ -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 = [
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 772a586

Please sign in to comment.