MalikSahib1 commited on
Commit
0b187ab
·
verified ·
1 Parent(s): 3fec58b

Update engine.py

Browse files
Files changed (1) hide show
  1. engine.py +23 -28
engine.py CHANGED
@@ -1,9 +1,9 @@
1
  import numpy as np
2
  from rembg import remove
3
- from PIL import Image, ImageFilter, ImageOps, ImageColor
4
  from moviepy.editor import ImageClip, CompositeVideoClip
5
  import os
6
- import math # Switched from numpy to math for safer scalar operations
7
 
8
  class IconAnimator:
9
  def __init__(self, input_path, output_path):
@@ -11,7 +11,11 @@ class IconAnimator:
11
  self.output_path = output_path
12
 
13
  def hex_to_rgb(self, hex_color):
14
- return ImageColor.getcolor(hex_color, "RGB")
 
 
 
 
15
 
16
  def process_image(self, icon_color_hex="#FFFFFF"):
17
  # 1. Remove Background
@@ -23,22 +27,16 @@ class IconAnimator:
23
  from io import BytesIO
24
  img = Image.open(BytesIO(output_data)).convert("RGBA")
25
 
26
- # 2. Recolor the Icon (Fill non-transparent pixels with chosen color)
27
  r, g, b, a = img.split()
28
-
29
- # Get target RGB
30
  target_r, target_g, target_b = self.hex_to_rgb(icon_color_hex)
31
-
32
- # Create solid color block
33
  solid_color = Image.new("RGB", img.size, (target_r, target_g, target_b))
34
-
35
- # Merge using the original alpha channel as a mask
36
  img_colored = Image.merge("RGBA", (*solid_color.split(), a))
37
 
38
  return img_colored
39
 
40
  def create_glow_layer(self, img, blur_radius, color_hex):
41
- # Create padding
42
  padding = 60
43
  new_size = (img.width + padding*2, img.height + padding*2)
44
  canvas = Image.new("RGBA", new_size, (0, 0, 0, 0))
@@ -60,44 +58,41 @@ class IconAnimator:
60
  # 1. Prepare Base Image
61
  base_pil = self.process_image(icon_color)
62
 
63
- # 2. Create Glow Layers
64
- # Small intense glow (inner)
65
  glow_small, pad = self.create_glow_layer(base_pil, blur_radius=10, color_hex=glow_color)
66
- # Large soft glow (outer)
67
  glow_large, _ = self.create_glow_layer(base_pil, blur_radius=30, color_hex=glow_color)
68
 
69
  w, h = base_pil.size
70
  final_w, final_h = w + (pad*2), h + (pad*2)
71
 
72
- # 3. Create Clips
73
  # Center the base icon
74
  base_clip = ImageClip(np.array(base_pil)).set_duration(speed).set_position("center")
75
-
76
  glow_s_clip = ImageClip(np.array(glow_small)).set_duration(speed)
77
  glow_l_clip = ImageClip(np.array(glow_large)).set_duration(speed)
78
 
79
- # 4. Apply Animation (Pulse)
80
- # We use math.sin instead of np.sin to prevent the "function * float" error
81
- # Intensity scales the opacity (0.0 to 1.0)
82
 
83
  def pulse_small(t):
84
- # Oscillates between 0.6 and 1.0 (modified by intensity)
85
  val = 0.6 + 0.4 * math.sin(2 * math.pi * t / speed)
86
- return min(max(val * intensity, 0), 1)
87
 
88
  def pulse_large(t):
89
- # Oscillates between 0.3 and 0.7 (modified by intensity)
90
  val = 0.4 + 0.3 * math.sin(2 * math.pi * t / speed)
91
- return min(max(val * intensity, 0), 1)
92
 
93
- glow_s_clip = glow_s_clip.set_opacity(pulse_small)
94
- glow_l_clip = glow_l_clip.set_opacity(pulse_large)
 
95
 
96
  # 5. Composite
97
  final = CompositeVideoClip([
98
- glow_l_clip, # Background atmosphere
99
- glow_s_clip, # Rim light
100
- base_clip # Sharp Icon
101
  ], size=(final_w, final_h))
102
 
103
  # 6. Export
 
1
  import numpy as np
2
  from rembg import remove
3
+ from PIL import Image, ImageFilter, ImageColor
4
  from moviepy.editor import ImageClip, CompositeVideoClip
5
  import os
6
+ import math
7
 
8
  class IconAnimator:
9
  def __init__(self, input_path, output_path):
 
11
  self.output_path = output_path
12
 
13
  def hex_to_rgb(self, hex_color):
14
+ # Converts hex string (e.g., "#00ffc8") to RGB tuple
15
+ try:
16
+ return ImageColor.getcolor(hex_color, "RGB")
17
+ except:
18
+ return (255, 255, 255) # Fallback to white
19
 
20
  def process_image(self, icon_color_hex="#FFFFFF"):
21
  # 1. Remove Background
 
27
  from io import BytesIO
28
  img = Image.open(BytesIO(output_data)).convert("RGBA")
29
 
30
+ # 2. Recolor the Icon Body
31
  r, g, b, a = img.split()
 
 
32
  target_r, target_g, target_b = self.hex_to_rgb(icon_color_hex)
 
 
33
  solid_color = Image.new("RGB", img.size, (target_r, target_g, target_b))
 
 
34
  img_colored = Image.merge("RGBA", (*solid_color.split(), a))
35
 
36
  return img_colored
37
 
38
  def create_glow_layer(self, img, blur_radius, color_hex):
39
+ # Create padding so glow isn't cut off
40
  padding = 60
41
  new_size = (img.width + padding*2, img.height + padding*2)
42
  canvas = Image.new("RGBA", new_size, (0, 0, 0, 0))
 
58
  # 1. Prepare Base Image
59
  base_pil = self.process_image(icon_color)
60
 
61
+ # 2. Create Glow Layers (Inner and Outer)
 
62
  glow_small, pad = self.create_glow_layer(base_pil, blur_radius=10, color_hex=glow_color)
 
63
  glow_large, _ = self.create_glow_layer(base_pil, blur_radius=30, color_hex=glow_color)
64
 
65
  w, h = base_pil.size
66
  final_w, final_h = w + (pad*2), h + (pad*2)
67
 
68
+ # 3. Create MoviePy Clips
69
  # Center the base icon
70
  base_clip = ImageClip(np.array(base_pil)).set_duration(speed).set_position("center")
 
71
  glow_s_clip = ImageClip(np.array(glow_small)).set_duration(speed)
72
  glow_l_clip = ImageClip(np.array(glow_large)).set_duration(speed)
73
 
74
+ # 4. Apply Animation (THE FIX)
75
+ # Instead of set_opacity(func), we modify the mask directly using fl()
 
76
 
77
  def pulse_small(t):
78
+ # Oscillates between 0.6 and 1.0
79
  val = 0.6 + 0.4 * math.sin(2 * math.pi * t / speed)
80
+ return max(0.0, min(val * intensity, 1.0))
81
 
82
  def pulse_large(t):
83
+ # Oscillates between 0.4 and 0.7
84
  val = 0.4 + 0.3 * math.sin(2 * math.pi * t / speed)
85
+ return max(0.0, min(val * intensity, 1.0))
86
 
87
+ # We multiply the existing mask frame (gf(t)) by the pulse value
88
+ glow_s_clip.mask = glow_s_clip.mask.fl(lambda gf, t: gf(t) * pulse_small(t))
89
+ glow_l_clip.mask = glow_l_clip.mask.fl(lambda gf, t: gf(t) * pulse_large(t))
90
 
91
  # 5. Composite
92
  final = CompositeVideoClip([
93
+ glow_l_clip, # Outer glow (Background)
94
+ glow_s_clip, # Inner glow (Rim light)
95
+ base_clip # Sharp Icon (Foreground)
96
  ], size=(final_w, final_h))
97
 
98
  # 6. Export