Skip to content

Commit

Permalink
Improved robustness of title screen rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
beveradb committed Dec 18, 2023
1 parent f3ebaf5 commit 6a6154f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions karaoke_prep/karaoke_prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ def calculate_text_size_and_position(self, draw, text, font_path, start_size, re
text_position = ((resolution[0] - text_width) // 2, (resolution[1] - text_height) // 2)
return font, text_position

def calculate_text_position(self, draw, text, font, resolution, vertical_offset):
bbox = draw.textbbox((0, 0), text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
text_x = (resolution[0] - text_width) // 2
text_y = vertical_offset
return (text_x, text_y), text_height

def create_intro_video(self, artist, title, format, output_image_filepath, output_video_filepath):
duration = 5 # Duration in seconds
resolution = (3840, 2160) # 4K resolution
Expand All @@ -270,25 +278,28 @@ def create_intro_video(self, artist, title, format, output_image_filepath, outpu
# Resize background to match resolution
background = background.resize(resolution)

# Create an ImageDraw instance
draw = ImageDraw.Draw(background)

title = title.upper()
artist = artist.upper()

initial_font_size = 500
top_padding = 950
title_padding = 400
artist_padding = 700
second_row_vertical_offset = 450
fixed_gap = 150

draw = ImageDraw.Draw(background)

title_font, title_text_position = self.calculate_text_size_and_position(
# Calculate positions and sizes for title and artist
title_font, _ = self.calculate_text_size_and_position(
draw, title, format["title_font"], initial_font_size, resolution, title_padding
)
artist_font, artist_text_position = self.calculate_text_size_and_position(
artist_font, _ = self.calculate_text_size_and_position(
draw, artist, format["artist_font"], initial_font_size, resolution, artist_padding
)

artist_text_position = (artist_text_position[0], title_text_position[1] + second_row_vertical_offset)
# Calculate vertical positions with consistent gap
title_text_position, title_height = self.calculate_text_position(draw, title, title_font, resolution, top_padding)
artist_text_position, _ = self.calculate_text_position(draw, artist, artist_font, resolution, title_text_position[1] + title_height + fixed_gap)

draw.text(title_text_position, title, fill=format["title_color"], font=title_font)
draw.text(artist_text_position, artist, fill=format["artist_color"], font=artist_font)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-prep"
version = "0.4.7"
version = "0.4.8"
description = "Prepare for karaoke video creation, by downloading audio and lyrics for a specified song or youtube playlist and separatung audio stems."
authors = ["Andrew Beveridge <andrew@beveridge.uk>"]
license = "MIT"
Expand Down

0 comments on commit 6a6154f

Please sign in to comment.