ming 3 years ago
parent
commit
e610a1db31
1 changed files with 20 additions and 5 deletions
  1. 20 5
      main.py

+ 20 - 5
main.py

@@ -226,7 +226,7 @@ def file_prepare(name, name_hash,text_content,image_urls):
     output.save(dir_sound+name_hash+".mp3")
     print("mp3 file made")
     #make title as image
-    txt2image(name, dir_title+name_hash+".png",'font.ttf',40)
+    txt2image(name, dir_title+name_hash+".png")
 
 def get_url_type(url):
     req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'})
@@ -296,11 +296,26 @@ def file_prepare_v2(name, name_hash,text_content,image_urls):
         txt_idx+=1
     print("mp3 file made")
     #make title as image
-    txt2image(name, dir_title+name_hash+".png",'font.ttf',40)
+    txt2image_title(name, dir_title+name_hash+".png")
 
-def txt2image(content, save_target,font,size):
+def txt2image(content, save_target):
     unicode_text = content
-    font = ImageFont.truetype(font, size,encoding='big5')
+    font = ImageFont.truetype("caption_font.ttf", 20,encoding='utf-8')
+    text_width, text_height = font.getsize(unicode_text)
+    canvas = Image.new('RGBA', (500, 500), (255, 0, 0, 0) )
+    draw = ImageDraw.Draw(canvas)
+    text=''
+    for c in unicode_text:
+        if len(re.findall(r'[\u4e00-\u9fff]+', c))>0:
+            text+=myunichchar(c)
+        else:
+            text+=c
+    draw.text((5,5), text, (255, 255, 255), font)
+    canvas.save(save_target, "PNG")
+
+def txt2image_title(content, save_target):
+    unicode_text = content
+    font = ImageFont.truetype("font.ttf", 40,encoding='big5')
     text_width, text_height = font.getsize(unicode_text)
     canvas = Image.new('RGBA', (500, 500), (255, 0, 0, 0) )
     draw = ImageDraw.Draw(canvas)
@@ -408,7 +423,7 @@ def generate_subtitle_image(name_hash,text_content):
         for step in range(math.ceil(len(text_content[idx])/sub_len)):
             sv_path = dir_subtitle + name_hash +'/'+str(idx)+ str(step) +'.png'
             sub = text_content[idx][step*sub_len:step*sub_len+sub_len]
-            txt2image(sub, sv_path,'caption_font.ttf',20)
+            txt2image(sub, sv_path)
             img_list[idx] += [{"count":len(sub),"path":sv_path}]
     return img_list