79512599

Date: 2025-03-16 13:22:21
Score: 0.5
Natty:
Report link

import cv2

import numpy as np

# Load images using OpenCV

user_img = cv2.imread(user_image_path)

hamster_img = cv2.imread(hamster_image_path)

# Convert the hamster image to RGBA to handle transparency

hamster_img = cv2.cvtColor(hamster_img, cv2.COLOR_BGR2BGRA)

# Extract the heart and text area from the hamster image

mask = np.all(hamster_img[:, :, :3] > 200, axis=-1) # Assuming the white background is near (255,255,255)

hamster_img[mask] = [0, 0, 0, 0] # Make background transparent

# Resize user image to fit the hamster position

user_resized = cv2.resize(user_img, (hamster_img.shape[1], hamster_img.shape[0]))

# Convert user image to RGBA for transparency handling

user_resized = cv2.cvtColor(user_resized, cv2.COLOR_BGR2BGRA)

# Merge the user image with the hamster image, keeping the heart and text

result = np.where(hamster_img[:, :, 3:] == 0, user_resized, hamster_img)

# Save and display the result

output_path = "/mnt/data/edited_image.png"

cv2.imwrite(output_path, result)

# Return the edited image path

output_path

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Low reputation (1):
Posted by: Eljur Tanega