Surn commited on
Commit
89b516d
1 Parent(s): 4d4fe17

Fix font issue

Browse files
Files changed (2) hide show
  1. assets/Arial.TTF +0 -0
  2. audiocraft/utils/extend.py +14 -2
assets/Arial.TTF ADDED
Binary file (276 kB). View file
 
audiocraft/utils/extend.py CHANGED
@@ -7,6 +7,7 @@ import string
7
  import tempfile
8
  import os
9
  import textwrap
 
10
 
11
  def separate_audio_segments(audio, segment_duration=30, overlap=1):
12
  sr, audio_data = audio[0], audio[1]
@@ -122,6 +123,16 @@ def hex_to_rgba(hex_color):
122
  rgba = (255,255,0,255)
123
  return rgba
124
 
 
 
 
 
 
 
 
 
 
 
125
  def add_settings_to_image(title: str = "title", description: str = "", width: int = 768, height: int = 512, background_path: str = "", font: str = "arial.ttf", font_color: str = "#ffffff"):
126
  # Create a new RGBA image with the specified dimensions
127
  image = Image.new("RGBA", (width, height), (255, 255, 255, 0))
@@ -138,7 +149,8 @@ def add_settings_to_image(title: str = "title", description: str = "", width: in
138
  text_x = width // 2
139
  text_y = height // 2
140
  # Draw the title text at the center top
141
- title_font = ImageFont.truetype(font, 26) # Replace with your desired font and size
 
142
  title_text = '\n'.join(textwrap.wrap(title, width // 12))
143
  title_x, title_y, title_text_width, title_text_height = title_font.getbbox(title_text)
144
  title_x = max(text_x - (title_text_width // 2), title_x, 0)
@@ -146,7 +158,7 @@ def add_settings_to_image(title: str = "title", description: str = "", width: in
146
  title_draw = ImageDraw.Draw(image)
147
  title_draw.multiline_text((title_x, title_y), title, fill=font_color, font=title_font, align="center")
148
  # Draw the description text two lines below the title
149
- description_font = ImageFont.truetype(font, 16) # Replace with your desired font and size
150
  description_text = '\n'.join(textwrap.wrap(description, width // 12))
151
  description_x, description_y, description_text_width, description_text_height = description_font.getbbox(description_text)
152
  description_x = max(text_x - (description_text_width // 2), description_x, 0)
 
7
  import tempfile
8
  import os
9
  import textwrap
10
+ from huggingface_hub import hf_hub_download
11
 
12
  def separate_audio_segments(audio, segment_duration=30, overlap=1):
13
  sr, audio_data = audio[0], audio[1]
 
123
  rgba = (255,255,0,255)
124
  return rgba
125
 
126
+ def getFont(font_name, size:int=16):
127
+ try:
128
+ font = ImageFont.truetype(font_name, size)
129
+ except:
130
+ try:
131
+ font = ImageFont.truetype(hf_hub_download("assets", font_name), encoding="UTF-8")
132
+ except:
133
+ font = ImageFont.load_default()
134
+ return font
135
+
136
  def add_settings_to_image(title: str = "title", description: str = "", width: int = 768, height: int = 512, background_path: str = "", font: str = "arial.ttf", font_color: str = "#ffffff"):
137
  # Create a new RGBA image with the specified dimensions
138
  image = Image.new("RGBA", (width, height), (255, 255, 255, 0))
 
149
  text_x = width // 2
150
  text_y = height // 2
151
  # Draw the title text at the center top
152
+ title_font = getFont(font, 26) # Replace with your desired font and size
153
+
154
  title_text = '\n'.join(textwrap.wrap(title, width // 12))
155
  title_x, title_y, title_text_width, title_text_height = title_font.getbbox(title_text)
156
  title_x = max(text_x - (title_text_width // 2), title_x, 0)
 
158
  title_draw = ImageDraw.Draw(image)
159
  title_draw.multiline_text((title_x, title_y), title, fill=font_color, font=title_font, align="center")
160
  # Draw the description text two lines below the title
161
+ description_font = getFont(font, 16) # Replace with your desired font and size
162
  description_text = '\n'.join(textwrap.wrap(description, width // 12))
163
  description_x, description_y, description_text_width, description_text_height = description_font.getbbox(description_text)
164
  description_x = max(text_x - (description_text_width // 2), description_x, 0)