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 969e34f commit 010cb14
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions pixelateTG.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,34 @@ 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
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)
new_width = int(resize_factor * w)
new_height = int(new_width / original_aspect_ratio)

# Ensure new dimensions are within the image bounds
new_width = min(new_width, image.shape[1] - x)
new_height = min(new_height, image.shape[0] - y)

overlay_image_resized = cv2.resize(overlay_image, (new_width, new_height), interpolation=cv2.INTER_AREA)

# Compute overlay position
center_x = x + w // 2
center_y = y + h // 2
overlay_x = max(0, int(center_x - new_width / 2))
overlay_y = max(0, int(center_y - new_height / 2))

# Determine the region of interest (ROI)
roi_start_x = overlay_x
roi_start_y = overlay_y
overlay_x = max(0, overlay_x)
overlay_y = max(0, overlay_y)
roi_start_x = max(0, overlay_x)
roi_start_y = max(0, overlay_y)
roi_end_x = min(image.shape[1], overlay_x + new_width)
roi_end_y = min(image.shape[0], overlay_y + new_height)

# Adjust the overlay region to fit within the image
overlay_image_resized = overlay_image_resized[:roi_end_y - roi_start_y, :roi_end_x - roi_start_x]

# Handle the alpha channel for transparency
alpha_mask = overlay_image_resized[:, :, 3] / 255.0
for c in range(0, 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[roi_start_y:roi_end_y, roi_start_x:roi_end_x, :3] = (
overlay_image_resized[
roi_start_y - overlay_y : roi_end_y - overlay_y,
roi_start_x - overlay_x : roi_end_x - overlay_x,
:3
] * (overlay_image_resized[:, :, 3:] / 255.0) +
image[roi_start_y:roi_end_y, roi_start_x:roi_end_x, :3] *
(1.0 - overlay_image_resized[:, :, 3:] / 255.0)
)

processed_path = f"processed/{user_id}_{overlay_type}.jpg"
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

0 comments on commit 010cb14

Please sign in to comment.