Untitled
unknown
python
a year ago
3.2 kB
20
Indexable
import cv2
import numpy as np
import os
import time
import random
# Canvas boyutları
canvas_width = 1100
canvas_height = 600
# Emoji dosyalarının bulunduğu dizin
emoji_dir = "C:/Users/7480/Desktop/yz/cv2/HOT/Sprint_2/emojis"
# Klasörden tüm emojileri yükleyelim ve bir kereye mahsus saklayalım
emoji_files = [f for f in os.listdir(emoji_dir) if f.endswith('.png')]
emoji_images = []
for emoji_file in emoji_files:
emoji_path = os.path.join(emoji_dir, emoji_file)
img = cv2.imread(emoji_path, cv2.IMREAD_UNCHANGED)
if img is not None:
emoji_images.append(img)
else:
print(f"Failed to load: {emoji_path}")
num_emojis = len(emoji_images)
# Sabit emoji boyutu ve boşluk
emoji_size = 70 # Emojilerin genişliği ve yüksekliği
spacing = 10 # Emojiler arasındaki boşluk
def create_frame(emoji_sequence):
# Boş bir resim oluşturma (arka plan beyaz)
frame = np.ones((canvas_height, canvas_width, 3), dtype=np.uint8) * 255
for i, emoji_img in enumerate(emoji_sequence):
if emoji_img is None:
continue # Eğer emoji resmi yüklenmediyse atla
# Emoji konumlarını hesapla
total_width = len(emoji_sequence) * (emoji_size + spacing) - spacing
x = (canvas_width - total_width) // 2 + i * (emoji_size + spacing)
y = (canvas_height - emoji_size) // 2
h, w = emoji_img.shape[:2]
emoji_img_resized = cv2.resize(emoji_img, (emoji_size, emoji_size), interpolation=cv2.INTER_AREA)
h, w = emoji_img_resized.shape[:2]
# Emojinin sınırları aşmamasını sağla
if x + w > canvas_width or y + h > canvas_height:
continue
if w > 0 and h > 0:
overlay = emoji_img_resized[:h, :w, :3]
alpha_mask = emoji_img_resized[:h, :w, 3] / 255.0
# Emoji resmini yapıştırma
for c in range(0, 3):
frame[y:y+h, x:x+w, c] = (alpha_mask * overlay[:, :, c] +
(1-alpha_mask) * frame[y:y+h, x:x+w, c])
return frame
# Pencereyi oluşturma
cv2.namedWindow("Emoji Canvas", cv2.WINDOW_AUTOSIZE)
def yatay_kayan_nesneler(n, v, t):
n = max(6, min(n, 12))
t = max(60, min(t, 5 * 60))
v = max(1, min(v, 3))
hiz = 750 // (2 ** (v - 1))
start_time = time.time()
try:
while True:
current_sequence = random.sample(emoji_images, n)
for current_count in range(1, n+1):
frame = create_frame(current_sequence[:current_count])
cv2.imshow("Emoji Canvas", frame)
# 100ms bekle ve emojilerin pozisyonunu güncelle
if cv2.waitKey(100) & 0xFF == ord('q'):
break
time.sleep(hiz / 1000.0) # Hız ayarına göre bekleme
# Süre sonunda pencereyi kapat
if time.time() - start_time > t:
break
finally:
cv2.destroyAllWindows()
# Fonksiyonu çağırarak çalıştırma
# Örnek: 12 nesne, 2. seviye hız, 2 dakika
yatay_kayan_nesneler(12, 2, 120)
Editor is loading...
Leave a Comment