浏览代码

add arial

ming 3 年之前
父节点
当前提交
554a509d7f
共有 2 个文件被更改,包括 17 次插入10 次删除
  1. 16 8
      OpenshotService/openshot_video_generator.py
  2. 1 2
      api/main.py

+ 16 - 8
OpenshotService/openshot_video_generator.py

@@ -108,7 +108,7 @@ def make_dir(name_hash):
     except FileExistsError:
         print("~~~~~~Warning~~~~~~~~~Directory " , dir_subtitle+name_hash ,  " already exists")
 
-def file_prepare(name, name_hash,text_content,image_urls,lang):
+def file_prepare(name, name_hash,text_content,image_urls,lang='zh'):
     make_dir(name_hash)
     img_num = 1
     for imgu in image_urls:
@@ -146,11 +146,15 @@ def file_prepare(name, name_hash,text_content,image_urls,lang):
         txt_idx+=1
     print("mp3 file made")
     #make title as image
-    txt2image_title(name, dir_title+name_hash+".png")
+    txt2image_title(name, dir_title+name_hash+".png",lang)
 
-def txt2image(content, save_target):
+def txt2image(content, save_target,lang='zh'):
     unicode_text = trim_punctuation(content)
-    font = ImageFont.truetype(font="font/DFT_B7.ttc", size=38)
+    font = ''
+    if lang=='zh':
+        font = ImageFont.truetype(font="font/DFT_B7.ttc", size=38)
+    else :
+        font = ImageFont.load("arial.pil")
     text_width, text_height = font.getsize(unicode_text)
     canvas = Image.new('RGBA', (700, 500), (255, 0, 0, 0) )
     draw = ImageDraw.Draw(canvas)
@@ -158,9 +162,13 @@ def txt2image(content, save_target):
     draw.text((5,5), text, (255, 255, 0), font)
     canvas.save(save_target, "PNG")
 
-def txt2image_title(content, save_target):
+def txt2image_title(content, save_target, lang='zh'):
     unicode_text = trim_punctuation(content)
-    font = ImageFont.truetype(font="font/DFT_B7.ttc", size=28)
+    font = ''
+    if lang=='zh':
+        font = ImageFont.truetype(font="font/DFT_B7.ttc", size=38)
+    else :
+        font = ImageFont.load("arial.pil")
     text_width, text_height = font.getsize(unicode_text)
     canvas = Image.new('RGBA', (510, 500), (255, 0, 0, 0) )
     draw = ImageDraw.Draw(canvas)
@@ -240,7 +248,7 @@ def generate_subtitle_image_ENG(name_hash,text_content):
     for idx in range(len(text_content)):
         sv_path = dir_subtitle + name_hash +'/'+str(idx)+'.png'
         sub = text_content[idx]
-        txt2image(sub, sv_path)
+        txt2image(sub, sv_path,lang='eng')
         img_list[idx] = sv_path
     return img_list
 
@@ -389,7 +397,7 @@ def anchor_video_v2(name_hash,name,text_content, image_urls,avatar):
 
 
 def anchor_video_eng(name_hash,name,text_content, image_urls,sub_titles,avatar):
-    file_prepare(name, name_hash, text_content,image_urls,1)
+    file_prepare(name, name_hash, text_content,image_urls,'eng')
     sub_list=generate_subtitle_image_ENG(name_hash,sub_titles)
     
     for fname in range(len(text_content)):

+ 1 - 2
api/main.py

@@ -144,7 +144,6 @@ async def make_anchor_video_v2(req:request):
                 im= im.convert("RGB")
         except:
             return {'msg':"無法辨別圖片網址"+imgu}
-
     save_history(req,name_hash)
     x = threading.Thread(target=gen_video_queue, args=(name_hash,req.name, req.text_content, req.image_urls,int(req.avatar)))
     x.start()
@@ -202,6 +201,7 @@ def get_url_type(url):
     r = urllib.request.urlopen(req)
     contentType = r.getheader('Content-Type')
     return contentType
+
 def notify_group(msg):
     glist=['WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD']
     for gid in glist:
@@ -261,7 +261,6 @@ def gen_video_queue(name_hash,name,text_content, image_urls,avatar):
         db['video_queue'].delete(id=top1['id'])
         db.query('UPDATE video_queue_status SET status = 0')
 
-
 def gen_video_queue_eng(name_hash,name,text_content, image_urls,sub_titles,avatar):
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
     time_stamp = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S")