Untitled

 avatar
unknown
python
a year ago
3.2 kB
8
Indexable
import cv2
import numpy as np
import os
import random
import time

# Parameters set by user
speed_level = 1  # 1 for slow, 2 for medium, 3 for fast
object_count = 6  # Number of objects, can be between 6 and 12
display_duration = 60  # Duration in seconds, can be between 60 and 300

# Derived parameters
canvas_w, canvas_h = 1100, 600
object_size = 100  # Size of the objects (emojis)
object_spacing = 10  # Spacing between objects
emoji_folder = 'emojiler/'  # Folder path for emojis
background_color = (255, 255, 255)  # White background
frame_color = (211, 211, 211)  # Grey frame color

# Speed settings in milliseconds
speed_settings = {
    1: 750,  # Slow
    2: 500,  # Medium
    3: 250   # Fast
}

# Speed based on user selection
speed = speed_settings[speed_level]

def load_emojis(emoji_folder):
    emojis = []
    for file_name in os.listdir(emoji_folder):
        if file_name.endswith('.png'):
            emoji_path = os.path.join(emoji_folder, file_name)
            img = cv2.imread(emoji_path, cv2.IMREAD_UNCHANGED)
            if img is not None and img.shape[2] == 4:
                emojis.append(img)
            else:
                print(f"Failed to load: {file_name}")
    return emojis

def overlay_image(background, img, position):
    x, y = position
    h, w = img.shape[:2]

    if y + h > background.shape[0] or x + w > background.shape[1]:
        return

    alpha_img = img[:, :, 3] / 255.0
    alpha_bg = 1.0 - alpha_img

    for c in range(0, 3):
        background[y:y+h, x:x+w, c] = (alpha_img * img[:, :, c] +
                                       alpha_bg * background[y:y+h, x:x+w, c])

def emoji_display():
    emojis = load_emojis(emoji_folder)
    if not emojis:
        print("Failed to load emojis!")
        return

    # Calculate total frames to display based on duration and speed
    total_frames = display_duration * 1000 // speed

    for _ in range(total_frames):
        frame = np.full((canvas_h, canvas_w, 3), background_color, dtype=np.uint8)
        
        # Randomly select the number of objects
        selected_emojis = random.sample(emojis, object_count)

        # Frame dimensions and coordinates
        frame_height = object_size + 2 * object_spacing
        frame_width = (object_size + object_spacing) * object_count - object_spacing
        start_x = (canvas_w - frame_width) // 2
        start_y = (canvas_h - frame_height) // 2

        # Draw the frame
        cv2.rectangle(frame, (start_x, start_y), (start_x + frame_width, start_y + frame_height), frame_color, -1)

        # Display each emoji one by one
        for i, emoji in enumerate(selected_emojis):
            resized_emoji = cv2.resize(emoji, (object_size, object_size), interpolation=cv2.INTER_AREA)
            x = start_x + i * (object_size + object_spacing)
            y = start_y + object_spacing
            overlay_image(frame, resized_emoji, (x, y))
            cv2.imshow("Emoji Display", frame)
            if cv2.waitKey(speed) & 0xFF == ord('q'):
                break

    cv2.destroyAllWindows()

# Call the display function
emoji_display()
Editor is loading...
Leave a Comment