antonelli commited on
Commit
ccee5f8
·
1 Parent(s): a961d09
Files changed (1) hide show
  1. video_generator.py +14 -25
video_generator.py CHANGED
@@ -9,37 +9,26 @@ def generate_lyric_frame(lyric, image_size):
9
  image_size = (800, 600) # Example default size, you can change this
10
 
11
  # Ensure image_size is a tuple
12
- image_size = tuple(image_size)
 
13
 
14
- # Create an image that is larger by a factor (e.g. 2)
15
- factor = 2
16
- large_image_size = (image_size[0] * factor, image_size[1] * factor)
17
- image = Image.new('RGB', large_image_size, color='black')
18
  draw = ImageDraw.Draw(image)
19
- font = ImageFont.load_default()
20
-
21
- # Wrap text
22
- max_width = large_image_size[0] - 40 * factor
23
- wrapped_text = textwrap.fill(lyric, width=40 * factor)
24
- lines = wrapped_text.split('\n')
25
 
26
- # Calculate text width and height
27
- text_width = max(draw.textlength(line, font=font) for line in lines)
28
- text_height = sum([font.getmask(line).getbbox()[3] for line in lines if font.getmask(line).getbbox()])
29
 
30
- # Calculate starting coordinates to center the text block
31
- text_x = (large_image_size[0] - text_width) / 2
32
- text_y = (large_image_size[1] - text_height) / 2
33
 
34
- # Draw the wrapped text on the image
35
- y = text_y
36
- for line in lines:
37
- draw.text((text_x, y), line, font=font, fill='white')
38
- y += font.getmask(line).getbbox()[3] if font.getmask(line).getbbox() else 0
39
 
40
- # Resize the image back to the original size
41
- resized_image = image.resize(image_size)
42
- return resized_image
43
 
44
 
45
  def generate_video(lyrics_with_timing, image_size=(800, 600), frame_rate=30):
 
9
  image_size = (800, 600) # Example default size, you can change this
10
 
11
  # Ensure image_size is a tuple
12
+ if not isinstance(image_size, tuple):
13
+ image_size = tuple(image_size)
14
 
15
+ image = Image.new('RGB', image_size, color='black')
 
 
 
16
  draw = ImageDraw.Draw(image)
 
 
 
 
 
 
17
 
18
+ # Define font and text alignment
19
+ font = ImageFont.load_default()
20
+ text_width = draw.textlength(lyric, font=font) # Getting text width
21
 
22
+ # Getting text height, with a check for empty text
23
+ text_bbox = font.getmask(lyric).getbbox()
24
+ text_height = text_bbox[3] if text_bbox else 0
25
 
26
+ text_x = (image_size[0] - text_width) / 2
27
+ text_y = (image_size[1] - text_height) / 2
 
 
 
28
 
29
+ # Draw the text on the image
30
+ draw.text((text_x, text_y), lyric, font=font, fill='white')
31
+ return image
32
 
33
 
34
  def generate_video(lyrics_with_timing, image_size=(800, 600), frame_rate=30):