Created
April 22, 2025 08:10
-
-
Save me-suzy/175e225331d18e40dbb83548633d97c4 to your computer and use it in GitHub Desktop.
Efect Flaire 3.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import numpy as np | |
from moviepy.editor import ImageClip, VideoClip | |
from PIL import Image | |
# Calea imaginii | |
IMAGE_PATH = r"d:\family-hugging.jpg" | |
OUTPUT_PATH = r"d:\family-hugging-animation.mp4" | |
# Durata animației | |
DURATION = 10 # 10 secunde | |
def apply_ken_burns_effect(image_array, duration, zoom_range=(1.0, 1.2), pan_range=0.1): | |
"""Aplică efectul Ken Burns direct pe array-ul de imagine""" | |
print("Aplic efect Ken Burns...") | |
# Creăm un VideoClip cu o funcție care modifică imaginea în timp | |
def make_frame(t): | |
# Calculăm progresul animației (0.0 -> 1.0) | |
progress = t / duration | |
# Aplicăm zoom progresiv | |
zoom_min, zoom_max = zoom_range | |
zoom = zoom_min + (zoom_max - zoom_min) * progress | |
# Calculăm deplasarea pentru panoramare | |
pan_x = pan_range * np.sin(2 * np.pi * t / duration) | |
# Obținem dimensiunile imaginii | |
h, w = image_array.shape[:2] | |
# Aplicăm zoom (redimensionăm imaginea) | |
zoomed_h, zoomed_w = int(h * zoom), int(w * zoom) | |
# Asigurăm-ne că dimensiunile sunt suficient de mari | |
if zoomed_h <= h or zoomed_w <= w: | |
zoomed_h, zoomed_w = h + 10, w + 10 # Adăugăm un buffer minim | |
# Creăm o imagine temporară redimensionată | |
import cv2 | |
zoomed = cv2.resize(image_array, (zoomed_w, zoomed_h), interpolation=cv2.INTER_CUBIC) | |
# Calculăm decalajul pentru a păstra imaginea centrată cu efect de panoramare | |
offset_x = int((zoomed_w - w) * (0.5 + pan_x / 2)) | |
offset_y = int((zoomed_h - h) * 0.5) | |
# Verificăm limitele pentru a evita erorile | |
if offset_x + w > zoomed_w: | |
offset_x = zoomed_w - w | |
if offset_y + h > zoomed_h: | |
offset_y = zoomed_h - h | |
# Decupăm zona de interes | |
frame = zoomed[offset_y:offset_y + h, offset_x:offset_x + w] | |
# Adăugăm un efect de luminozitate pulsatorie subtilă | |
brightness = 1.0 + 0.1 * np.sin(t * 2) | |
frame = np.clip(frame * brightness, 0, 255).astype('uint8') | |
return frame | |
# Creăm și returnăm un VideoClip cu funcția definită mai sus | |
return VideoClip(make_frame, duration=duration) | |
def main(): | |
print(f"Încarc imaginea: {IMAGE_PATH}") | |
try: | |
# Deschidem imaginea folosind PIL și o convertim la numpy array | |
pil_img = Image.open(IMAGE_PATH) | |
img_array = np.array(pil_img) | |
print(f"Dimensiune imagine: {pil_img.width}x{pil_img.height}") | |
# Aplicăm efectul Ken Burns pe imaginea încărcată | |
video = apply_ken_burns_effect(img_array, DURATION) | |
# Scriem videoclipul în fișier | |
print(f"Salvez animația: {OUTPUT_PATH}") | |
video.write_videofile( | |
OUTPUT_PATH, | |
fps=24, | |
codec='libx264', | |
audio=False, | |
preset='medium', | |
bitrate="5000k" | |
) | |
print("\n✅ Animația a fost creată cu succes!") | |
except Exception as e: | |
print(f"❌ Eroare: {str(e)}") | |
import traceback | |
traceback.print_exc() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment