Skip to content

Commit

Permalink
Update pixelateTG.py
Browse files Browse the repository at this point in the history
  • Loading branch information
arbadacarbaYK authored May 29, 2024
1 parent 3ba28a3 commit 2f7fc25
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions pixelateTG.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,39 @@ 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 the new dimensions
# 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
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

# Resize the overlay image to match the adjusted dimensions
overlay_image_resized = cv2.resize(overlay_image, (overlay_width, overlay_height), interpolation=cv2.INTER_AREA)

# Calculate the regions of interest
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 + overlay_width)
roi_end_y = min(image.shape[0], roi_start_y + overlay_height)

# Adjust resized overlay if it goes out of bounds
resized_overlay_start_x = 0 if start_x >= 0 else abs(start_x)
resized_overlay_start_y = 0 if start_y >= 0 else abs(start_y)
resized_overlay_width = overlay_width if end_x <= image.shape[1] else overlay_image_resized.shape[1] - resized_overlay_start_x
resized_overlay_height = overlay_height if end_y <= image.shape[0] else overlay_image_resized.shape[0] - resized_overlay_start_y

# 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:
alpha_mask = overlay_image_resized[resized_overlay_start_y:resized_overlay_start_y + resized_overlay_height, resized_overlay_start_x:resized_overlay_start_x + resized_overlay_width, 3] / 255.0
overlay_part = overlay_image_resized[:roi_end_y - roi_start_y, :roi_end_x - roi_start_x]
alpha_mask = overlay_part[:, :, 3] / 255.0
for c in range(3):
image[roi_start_y:roi_start_y + resized_overlay_height, roi_start_x:roi_start_x + resized_overlay_width, c] = (
alpha_mask * overlay_image_resized[resized_overlay_start_y:resized_overlay_start_y + resized_overlay_height, resized_overlay_start_x:resized_overlay_start_x + resized_overlay_width, c] +
(1 - alpha_mask) * image[roi_start_y:roi_start_y + resized_overlay_height, roi_start_x:roi_start_x + resized_overlay_width, c]
image[roi_start_y:roi_end_y, roi_start_x:roi_end_x, c] = (
alpha_mask * overlay_part[:, :, c] +
(1 - alpha_mask) * image[roi_start_y:roi_end_y, roi_start_x:roi_end_x, c]
)
except ValueError as e:
print(f"Error blending overlay: {e}")
Expand All @@ -83,6 +78,7 @@ def overlay(photo_path, user_id, overlay_type, resize_factor, bot):
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

0 comments on commit 2f7fc25

Please sign in to comment.