Skip to content

Commit

Permalink
Update pixelateTG.py
Browse files Browse the repository at this point in the history
  • Loading branch information
arbadacarbaYK authored Jul 18, 2024
1 parent 51c709d commit 03a6e0c
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions pixelateTG.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Send me a picture or a GIF, and I will pixelate faces in it!')

def detect_heads(image):
detector = MTCNN()
faces = detector.detect_faces(image)
# Sort faces by size (area) in descending order
faces.sort(key=lambda x: x['box'][2] * x['box'][3], reverse=True)
mtcnn = MTCNN()
faces = mtcnn.detect_faces(image)
head_boxes = [(face['box'][0], face['box'][1], int(RESIZE_FACTOR * face['box'][2]), int(RESIZE_FACTOR * face['box'][3])) for face in faces]
# Sort faces based on y-coordinate (top to bottom)
head_boxes.sort(key=lambda box: box[1])
return head_boxes

def overlay(photo_path, user_id, overlay_type, resize_factor, bot):
Expand All @@ -53,16 +49,12 @@ def overlay(photo_path, user_id, overlay_type, resize_factor, bot):
center_y = y + h // 2

# Overlay position adjusted for better centering
overlay_x = center_x - (new_width // 2)
overlay_y = center_y - (new_height // 2)
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)
if overlay_x + new_width > image.shape[1]:
new_width = image.shape[1] - overlay_x
if overlay_y + new_height > image.shape[0]:
new_height = image.shape[0] - overlay_y

# Resize the overlay image
overlay_image_resized = cv2.resize(overlay_image, (new_width, new_height), interpolation=cv2.INTER_AREA)
Expand Down Expand Up @@ -90,6 +82,7 @@ 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 @@ -224,6 +217,7 @@ def process_image(photo_path, user_id, session_id, bot):
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 @@ -236,17 +230,7 @@ def button_callback(update: Update, context: CallbackContext) -> None:
photo_path = data.get('photo_path')
user_or_chat_id = data.get('user_id') or data.get('chat_id')

# Delete files in both 'downloads' and 'processed' directories
if query.data.startswith('cancel'):
# Delete the corresponding processed file
processed_file_path = f"processed/{user_or_chat_id}_{session_id}_pixelated.jpg"
if os.path.exists(processed_file_path):
os.remove(processed_file_path)

# Delete the original photo/gif file from 'downloads' directory
if os.path.exists(photo_path):
os.remove(photo_path)

if session_id in user_data:
del user_data[session_id]
if session_id in chat_data:
Expand All @@ -257,7 +241,7 @@ def button_callback(update: Update, context: CallbackContext) -> None:
processed_path = None

if query.data.startswith('pixelate'):
processed_path = process_image(photo_path, user_or_chat_id, session_id, context.bot)
processed_path = process_image(photo_path, user_or_chat_id, query.id, context.bot)
elif query.data.startswith('liotta'):
processed_path = liotta_overlay(photo_path, user_or_chat_id, context.bot)
elif query.data.startswith('cats_overlay'):
Expand All @@ -274,7 +258,6 @@ 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'))


def main() -> None:
updater = Updater(TOKEN)

Expand Down

0 comments on commit 03a6e0c

Please sign in to comment.