|
@@ -1,6 +1,67 @@
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
import os
|
|
|
|
|
|
+def create_name(name,img_url,output_path):
|
|
|
+
|
|
|
+ font : ImageFont
|
|
|
+
|
|
|
+ image1 = Image.open(img_url).convert('RGBA')
|
|
|
+
|
|
|
+ if detect_language(name) == "English":
|
|
|
+ font = ImageFont.truetype(f"{os.path.split(os.path.abspath('main.py'))[0]}/static/MasaFont-Regular.ttf", 120, encoding="utf-8")
|
|
|
+ if len(name) > 50:
|
|
|
+ return "超過字數限制"
|
|
|
+
|
|
|
+ # 設定初始位置
|
|
|
+ x_position = 0
|
|
|
+ y_position = 0
|
|
|
+
|
|
|
+ max_width = int(font.getlength(name[0])*len(name))
|
|
|
+ max_height = int(font.getlength(name[0])*len(name))
|
|
|
+
|
|
|
+ image = Image.new('RGBA', (max_width,max_height), (255, 255, 255, 0))
|
|
|
+ draw = ImageDraw.Draw(image)
|
|
|
+
|
|
|
+ text_width = draw.textlength(name[0], font=font)
|
|
|
+
|
|
|
+ for char in name:
|
|
|
+ draw.text((x_position, y_position), char, font=font, fill=(0, 0, 0))
|
|
|
+ x_position += text_width
|
|
|
+
|
|
|
+ image1.paste(image, (500, 1500),image)
|
|
|
+
|
|
|
+ # 保存疊加後的圖片
|
|
|
+ image1.save(output_path)
|
|
|
+
|
|
|
+ else:
|
|
|
+ # 選擇中文字型和大小
|
|
|
+ font = ImageFont.truetype(f"{os.path.split(os.path.abspath('main.py'))[0]}/static/MasaFont-Regular.ttf", 150, encoding="utf-8")
|
|
|
+ if len(name) > 5 :
|
|
|
+ return "超過字數限制"
|
|
|
+
|
|
|
+ # 設定初始位置
|
|
|
+ x_position = 0
|
|
|
+ y_position = 0
|
|
|
+
|
|
|
+ max_width = int(font.getlength(name[0]))
|
|
|
+ max_height = int(font.getlength(name[0])*len(name))
|
|
|
+
|
|
|
+ image = Image.new('RGBA', (max_width,max_height), (255, 255, 255, 0))
|
|
|
+ draw = ImageDraw.Draw(image)
|
|
|
+ text_width = draw.textlength(name[0], font=font)
|
|
|
+
|
|
|
+ for char in name:
|
|
|
+ draw.text((x_position, y_position), char, font=font, fill=(0, 0, 0))
|
|
|
+ y_position += text_width
|
|
|
+
|
|
|
+ image1.paste(image, (150, 900),image)
|
|
|
+
|
|
|
+ # 保存疊加後的圖片
|
|
|
+ image1.save(output_path)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
def create_image(text, output_path, font_size=300, bg_color=(255, 255, 255), text_color=(0, 0, 0), max_width=200):
|
|
|
|
|
|
|
|
@@ -26,6 +87,10 @@ def create_image(text, output_path, font_size=300, bg_color=(255, 255, 255), tex
|
|
|
# 設定初始位置
|
|
|
x_position = 0
|
|
|
y_position = 0
|
|
|
+
|
|
|
+ lines_tmp = [x.strip() for x in lines if x.strip()!='']
|
|
|
+ lines = lines_tmp
|
|
|
+ print(lines)
|
|
|
|
|
|
line_height = font.getlength(text[0])
|
|
|
print(line_height)
|
|
@@ -34,7 +99,7 @@ def create_image(text, output_path, font_size=300, bg_color=(255, 255, 255), tex
|
|
|
max_height = int(font.getlength(text[0])*find_longest_segment(lines))
|
|
|
|
|
|
print(max_width,max_height)
|
|
|
- print(lines)
|
|
|
+
|
|
|
|
|
|
if check_tag == "en" :
|
|
|
tmp = max_width
|
|
@@ -88,7 +153,7 @@ def create_image(text, output_path, font_size=300, bg_color=(255, 255, 255), tex
|
|
|
|
|
|
return "成功製作文字"
|
|
|
|
|
|
-def overlay_images(background_path, overlay_path, output_path):
|
|
|
+def overlay_images(background_path, overlay_path, output_path,name):
|
|
|
image1 = Image.open(background_path).convert('RGBA')
|
|
|
image2 = Image.open(overlay_path).convert('RGBA')
|
|
|
|
|
@@ -99,12 +164,19 @@ def overlay_images(background_path, overlay_path, output_path):
|
|
|
image1.paste(image2, (x, y),image2)
|
|
|
|
|
|
# 保存疊加後的圖片
|
|
|
- image1.save(output_path)
|
|
|
+ image1.save(f"{output_path}_tmp.png")
|
|
|
+
|
|
|
+
|
|
|
|
|
|
# 顯示疊加後的圖片
|
|
|
# image1.show()
|
|
|
|
|
|
print(f"finished, saving image at {output_path}")
|
|
|
+ try:
|
|
|
+ create_name(name,f"{output_path}_tmp.png",output_path)
|
|
|
+ except Exception as e:
|
|
|
+
|
|
|
+ print( str(e))
|
|
|
|
|
|
im = Image.open(output_path)
|
|
|
name =output_path.lower().split('/')[::-1][0]
|
|
@@ -112,6 +184,7 @@ def overlay_images(background_path, overlay_path, output_path):
|
|
|
im.save(f"{os.path.split(os.path.abspath('main.py'))[0]}/static/tendents/{webp}", 'WebP', quality=40, )
|
|
|
|
|
|
os.remove(output_path)
|
|
|
+ os.remove(f"{output_path}_tmp.png")
|
|
|
|
|
|
def detect_language(text):
|
|
|
for char in text:
|
|
@@ -137,8 +210,10 @@ def split_text_by_length(text, length):
|
|
|
|
|
|
# Add the remaining part as the last paragraph
|
|
|
if current_paragraph:
|
|
|
- paragraphs.append(current_paragraph.strip())
|
|
|
+ if current_paragraph.strip():
|
|
|
+ paragraphs.append(current_paragraph.strip())
|
|
|
|
|
|
+ #print(paragraphs)
|
|
|
return paragraphs
|
|
|
|
|
|
def split_chinese_text(text, max_length=5):
|
|
@@ -170,7 +245,11 @@ def split_chinese_text(text, max_length=5):
|
|
|
|
|
|
# 添加最后一个片段到segments列表中
|
|
|
if current_segment:
|
|
|
- segments.append(current_segment)
|
|
|
+
|
|
|
+ if current_segment.strip() and current_segment != '':
|
|
|
+ segments.append(current_segment)
|
|
|
+
|
|
|
+
|
|
|
|
|
|
return segments
|
|
|
|