From c5aae4a01cd1a7eebaae867e0777c4fa230ce835 Mon Sep 17 00:00:00 2001 From: arbadacarba <63317640+arbadacarbaYK@users.noreply.github.com> Date: Wed, 29 May 2024 20:43:35 +0200 Subject: [PATCH] Update pixelateTG.py --- pixelateTG.py | 50 +++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/pixelateTG.py b/pixelateTG.py index 9205286..ea260f1 100644 --- a/pixelateTG.py +++ b/pixelateTG.py @@ -34,29 +34,32 @@ def overlay(photo_path, user_id, overlay_type, resize_factor, bot): random_overlay = random.choice(overlay_files) overlay_image = cv2.imread(random_overlay, cv2.IMREAD_UNCHANGED) original_aspect_ratio = overlay_image.shape[1] / overlay_image.shape[0] - center_x = x + w // 2 - center_y = y + h // 2 + + # Calculate the new dimensions new_width = int(resize_factor * w) new_height = int(new_width / original_aspect_ratio) - overlay_image_resized = cv2.resize(overlay_image, (new_width, new_height), interpolation=cv2.INTER_AREA) - # Ensure ROI dimensions match - roi_start_x = max(0, center_x - new_width // 2) - roi_start_y = max(0, center_y - new_height // 2) - roi_end_x = min(image.shape[1], roi_start_x + new_width) - roi_end_y = min(image.shape[0], roi_start_y + new_height) + # Ensure the overlay is centered on the face + center_x = x + w // 2 + center_y = y + h // 2 + start_x = max(center_x - new_width // 2, 0) + start_y = max(center_y - new_height // 2, 0) + end_x = min(start_x + new_width, image.shape[1]) + end_y = min(start_y + new_height, image.shape[0]) + + # Adjust the new dimensions to fit within the image boundaries + overlay_width = end_x - start_x + overlay_height = end_y - start_y - overlay_resized_width = roi_end_x - roi_start_x - overlay_resized_height = roi_end_y - roi_start_y - - overlay_image_resized = cv2.resize(overlay_image_resized, (overlay_resized_width, overlay_resized_height), interpolation=cv2.INTER_AREA) - - # Blending + # Resize the overlay image to match the adjusted dimensions + overlay_image_resized = cv2.resize(overlay_image, (overlay_width, overlay_height), interpolation=cv2.INTER_AREA) + + # Blend the overlay onto the image try: alpha_mask = overlay_image_resized[:, :, 3] / 255.0 for c in range(3): - image[roi_start_y:roi_end_y, roi_start_x:roi_end_x, c] = (alpha_mask * overlay_image_resized[:, :, c] + - (1 - alpha_mask) * image[roi_start_y:roi_end_y, roi_start_x:roi_end_x, c]) + image[start_y:end_y, start_x:end_x, c] = (alpha_mask * overlay_image_resized[:, :, c] + + (1 - alpha_mask) * image[start_y:end_y, start_x:end_x, c]) except ValueError as e: print(f"Error blending overlay: {e}") continue @@ -84,21 +87,6 @@ def cats_overlay(photo_path, user_id, bot): def clowns_overlay(photo_path, user_id, bot): return overlay(photo_path, user_id, 'clown', RESIZE_FACTOR, bot) -def process_image(photo_path, user_id, file_id, bot): - image = cv2.imread(photo_path) - faces = detect_heads(image) - - def process_face(x, y, w, h): - face = image[y:y+h, x:x+w] - pixelated_face = cv2.resize(face, (0, 0), fx=PIXELATION_FACTOR, fy=PIXELATION_FACTOR, interpolation=cv2.INTER_NEAREST) - image[y:y+h, x:x+w] = cv2.resize(pixelated_face, (w, h), interpolation=cv2.INTER_NEAREST) - - futures = [executor.submit(process_face, x, y, w, h) for (x, y, w, h) in faces] - wait(futures) - - processed_path = f"processed/{user_id}_{file_id}.jpg" - cv2.imwrite(processed_path, image, [int(cv2.IMWRITE_JPEG_QUALITY), 95]) - return processed_path def process_gif(gif_path, session_id, user_id, bot): frames = imageio.mimread(gif_path)