Spaces:
Sleeping
Sleeping
Update src/pyscripts/img_process.py
Browse files- src/pyscripts/img_process.py +377 -92
src/pyscripts/img_process.py
CHANGED
|
@@ -1,92 +1,377 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
from dataclasses import dataclass
|
| 6 |
+
import requests
|
| 7 |
+
from pypinyin import pinyin, Style
|
| 8 |
+
import rembg
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
font_path_fangzhenghei = "src/assets/FangZhengHeiTiJianTi-1.ttf"
|
| 12 |
+
font_path_zihun59 = "src/assets/字魂59号-创粗黑.ttf"
|
| 13 |
+
font_path_notosans = "src/assets/NotoSansHans-Regular.otf"
|
| 14 |
+
font_path_feihuasong = "src/assets/FeiHuaSongTi-2.ttf"
|
| 15 |
+
font_path_wendingPLjianbaosong = "src/assets/gbsn00lp-2.ttf"
|
| 16 |
+
font_path_SourceHanSans = "src/assets/SourceHanSansCN-VF-2.otf"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@dataclass
|
| 22 |
+
class text_info:
|
| 23 |
+
text: str
|
| 24 |
+
font_size: int
|
| 25 |
+
position: list
|
| 26 |
+
color: tuple= (0,0,0)
|
| 27 |
+
max_height: int=1000
|
| 28 |
+
max_width: int=0
|
| 29 |
+
line_spacing: int = 10
|
| 30 |
+
font_path: str = None
|
| 31 |
+
em_font_path: str = None
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def __post_init__(self):
|
| 35 |
+
self.wrap_text()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def wrap_text(self):
|
| 39 |
+
"""
|
| 40 |
+
resize to fix max_height and max_width
|
| 41 |
+
"""
|
| 42 |
+
font = ImageFont.truetype(self.font_path, self.font_size)
|
| 43 |
+
words = re.findall(r'[a-zA-Z\d]+|[\u4e00-\u9fa5]|\n|.', self.text)
|
| 44 |
+
lines = []
|
| 45 |
+
current_line = ''
|
| 46 |
+
for word in words:
|
| 47 |
+
if word == '\n':
|
| 48 |
+
lines.append(current_line.strip())
|
| 49 |
+
current_line = ''
|
| 50 |
+
continue
|
| 51 |
+
test_line = current_line + word
|
| 52 |
+
test_bbox = font.getbbox(test_line, anchor="la")
|
| 53 |
+
test_width = test_bbox[2] - test_bbox[0] # 计算宽度
|
| 54 |
+
if test_width <= self.max_width and '\n' not in test_line:
|
| 55 |
+
current_line = test_line
|
| 56 |
+
else:
|
| 57 |
+
lines.append(current_line.strip())
|
| 58 |
+
current_line = word
|
| 59 |
+
if current_line:
|
| 60 |
+
lines.append(current_line.strip())
|
| 61 |
+
self.lines = lines
|
| 62 |
+
self.height = len(lines) * self.font_size + (len(lines) - 1) * self.line_spacing
|
| 63 |
+
key_lines = [line for line in lines if line.startswith("【】")]
|
| 64 |
+
if len(key_lines) > 0:
|
| 65 |
+
key_line = max(key_lines, key=len)
|
| 66 |
+
else:
|
| 67 |
+
key_line = False
|
| 68 |
+
if self.height > self.max_height or (key_line and not re.search("^"+re.escape(key_line)+"$", self.text, re.M)):
|
| 69 |
+
self.font_size -= 2
|
| 70 |
+
self.wrap_text()
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def write_text_on_image(img, text_info):
|
| 74 |
+
# 创建一个可以在给定图像上绘图的对象
|
| 75 |
+
draw = ImageDraw.Draw(img)
|
| 76 |
+
# 加载字体,如果没有提供字体路径,则使用系统默认字体
|
| 77 |
+
font = ImageFont.load_default().font_variant(size=text_info.font_size)
|
| 78 |
+
em_font = ImageFont.load_default().font_variant(size=text_info.font_size)
|
| 79 |
+
if text_info.font_path:
|
| 80 |
+
font = ImageFont.truetype(text_info.font_path, text_info.font_size)
|
| 81 |
+
if text_info.em_font_path:
|
| 82 |
+
em_font = ImageFont.truetype(text_info.em_font_path, text_info.font_size)
|
| 83 |
+
# 在图片上写入文字
|
| 84 |
+
# draw.text(text_info.position, text_info.text, fill=text_info.color, font=font)
|
| 85 |
+
y_text = text_info.position[1]
|
| 86 |
+
wrapped_lines = text_info.lines
|
| 87 |
+
text_bbox_chinese = font.getbbox("中文行高")
|
| 88 |
+
for line in wrapped_lines:
|
| 89 |
+
text_bbox = font.getbbox(line)
|
| 90 |
+
line_width = text_bbox[2] - text_bbox[0] # 获取宽度
|
| 91 |
+
line_height = max(text_bbox[3] - text_bbox[1], text_bbox_chinese[3] - text_bbox_chinese[1])
|
| 92 |
+
# print(text_bbox[3] - text_bbox[1], text_bbox_chinese[3] - text_bbox_chinese[1], text_info.font_size)
|
| 93 |
+
if re.search(r"^【.*】.*?"+re.escape(line), text_info.text, re.M) or re.match(r"^【.*】.*?$", line):
|
| 94 |
+
draw.text((text_info.position[0], y_text), line.replace("【】", ""), fill=text_info.color, font=em_font)
|
| 95 |
+
else:
|
| 96 |
+
draw.text((text_info.position[0], y_text), line, fill=text_info.color, font=font)
|
| 97 |
+
# 更新y坐标以添加行间距并为下一行做准备
|
| 98 |
+
y_text += line_height + text_info.line_spacing
|
| 99 |
+
return img, y_text
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def embed_image(background, foreground, vertical_offset=0, standard_fg_size=None):
|
| 103 |
+
"""
|
| 104 |
+
粘贴图片
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
background (_type_): 背景图片
|
| 108 |
+
foreground (_type_): 前景图片
|
| 109 |
+
vertical_offset (int, optional): 纵向偏移. Defaults to 1500.
|
| 110 |
+
standard_fg_size (tuple, optional): 前景缩放大小. Defaults to (1600, 450).
|
| 111 |
+
|
| 112 |
+
Returns:
|
| 113 |
+
_type_: _description_
|
| 114 |
+
"""
|
| 115 |
+
# 获取背景图片的尺寸
|
| 116 |
+
if standard_fg_size is None:
|
| 117 |
+
standard_fg_size = background.size
|
| 118 |
+
standard_fg_width, standard_fg_height = standard_fg_size
|
| 119 |
+
bg_width, bg_height = background.size
|
| 120 |
+
fg_width, fg_height = foreground.size
|
| 121 |
+
fg_resize_percent = max(fg_width / standard_fg_width, fg_height / standard_fg_height)
|
| 122 |
+
# print((fg_width / standard_fg_width, fg_height / standard_fg_height))
|
| 123 |
+
foreground = foreground.resize((int(fg_width // fg_resize_percent), int(fg_height // fg_resize_percent)))
|
| 124 |
+
fg_width, fg_height = foreground.size
|
| 125 |
+
offset_x = (bg_width - fg_width) // 2 # 横轴偏移使小图居中
|
| 126 |
+
offset_y = vertical_offset # 纵轴固定偏移300px
|
| 127 |
+
|
| 128 |
+
background.paste(foreground, (offset_x, offset_y), foreground)
|
| 129 |
+
return background
|
| 130 |
+
|
| 131 |
+
def make_exhibitor_poster(logo_link, slogan, content):
|
| 132 |
+
background = Image.open("src/assets/展商海报白板.jpg")
|
| 133 |
+
H_PADDING_RATION = 0.08
|
| 134 |
+
V_PADDING = 25
|
| 135 |
+
V_START = 950
|
| 136 |
+
MAX_HEIGHT = 520
|
| 137 |
+
res = requests.get(logo_link)
|
| 138 |
+
foreground = Image.open(io.BytesIO(res.content))
|
| 139 |
+
# foreground = Image.open(logo_path_list[FOREGROUND_ID])
|
| 140 |
+
background = embed_image(background, foreground, vertical_offset=750, standard_fg_size=(1600//2, 450//2))
|
| 141 |
+
slogan = text_info(text=slogan, font_size=110//2,
|
| 142 |
+
position=[background.size[0] * H_PADDING_RATION, V_START],
|
| 143 |
+
max_width=background.size[0]*(1-2*H_PADDING_RATION),
|
| 144 |
+
max_height=110//2,
|
| 145 |
+
font_path=font_path_fangzhenghei)
|
| 146 |
+
slogan.position[0] = (background.size[0] - len(slogan.text) * slogan.font_size) // 2
|
| 147 |
+
background, next_height = write_text_on_image(background, slogan)
|
| 148 |
+
|
| 149 |
+
content = text_info(text=content, font_size=100,
|
| 150 |
+
position=[background.size[0] * H_PADDING_RATION, next_height+V_PADDING],
|
| 151 |
+
max_width=background.size[0]*(1-2*H_PADDING_RATION),
|
| 152 |
+
max_height=MAX_HEIGHT,
|
| 153 |
+
font_path=font_path_SourceHanSans, em_font_path=font_path_fangzhenghei)
|
| 154 |
+
print(content.height)
|
| 155 |
+
background, next_height = write_text_on_image(background, content)
|
| 156 |
+
img_stream = io.BytesIO()
|
| 157 |
+
background.save(img_stream, format="JPEG")
|
| 158 |
+
img_stream.seek(0)
|
| 159 |
+
background.save("output/展商海报.jpg")
|
| 160 |
+
return img_stream
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def crop_image(image):
|
| 164 |
+
# 获取图片的宽度和高度
|
| 165 |
+
width, height = image.size
|
| 166 |
+
# 初始化边界框
|
| 167 |
+
left = width
|
| 168 |
+
top = height
|
| 169 |
+
right = 0
|
| 170 |
+
bottom = 0
|
| 171 |
+
# 遍历图片的每个像素,找到非透明的区域
|
| 172 |
+
for x in range(width):
|
| 173 |
+
for y in range(height):
|
| 174 |
+
pixel = image.getpixel((x, y))
|
| 175 |
+
if pixel[3] != 0: # 检查透明度通道
|
| 176 |
+
if x < left:
|
| 177 |
+
left = x
|
| 178 |
+
if x > right:
|
| 179 |
+
right = x
|
| 180 |
+
if y < top:
|
| 181 |
+
top = y
|
| 182 |
+
if y > bottom:
|
| 183 |
+
bottom = y
|
| 184 |
+
cropped_image = image.crop((left, top, right + 1, bottom + 1))
|
| 185 |
+
return cropped_image
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def rm_img_bg(image):
|
| 189 |
+
image = rembg.remove(image)
|
| 190 |
+
cropped_image = crop_image(image)
|
| 191 |
+
return cropped_image
|
| 192 |
+
|
| 193 |
+
def get_text_width(font_path, font_size, text):
|
| 194 |
+
font = ImageFont.truetype(font_path, font_size)
|
| 195 |
+
test_bbox = font.getbbox(text, anchor="la")
|
| 196 |
+
test_width = test_bbox[2] - test_bbox[0]
|
| 197 |
+
return test_width
|
| 198 |
+
def compose_guest_poster(name, name_pinyin, guest_photo, titles):
|
| 199 |
+
|
| 200 |
+
background = Image.open("src/assets/Guest04.jpg").convert("RGBA")
|
| 201 |
+
background = background.resize((1080*2, 2400*2))
|
| 202 |
+
|
| 203 |
+
title_list = titles.split("\n")
|
| 204 |
+
# guest = Image.open("src/assets/Guest.png").convert("RGBA")
|
| 205 |
+
# higher_mask = Image.open("src/assets/Guest01.png").convert("RGBA")
|
| 206 |
+
H_PADDING_RATION = 0.08
|
| 207 |
+
V_PADDING = 5
|
| 208 |
+
V_START = 950
|
| 209 |
+
MAX_HEIGHT = 520
|
| 210 |
+
# foreground = Image.open(logo_path_list[FOREGROUND_ID])
|
| 211 |
+
bg_width, bg_height = background.size
|
| 212 |
+
background = embed_image(background, guest_photo, vertical_offset=1300, standard_fg_size=(2160, 2600))
|
| 213 |
+
lower_mask = Image.open("src/assets/Guest03.png").convert("RGBA")
|
| 214 |
+
background = embed_image(background, lower_mask)
|
| 215 |
+
higher_mask = Image.open("src/assets/Guest02.png").convert("RGBA")
|
| 216 |
+
background = embed_image(background, higher_mask)
|
| 217 |
+
seal = Image.open("src/assets/Guest01.png").convert("RGBA")
|
| 218 |
+
background = embed_image(background, seal)
|
| 219 |
+
# background = background.convert("RGB")
|
| 220 |
+
|
| 221 |
+
font_size=210
|
| 222 |
+
name, background, next_height = write_text(name, font_path_zihun59, background, H_PADDING_RATION, 3020, font_size)
|
| 223 |
+
|
| 224 |
+
next_height += 50
|
| 225 |
+
font_size = 100
|
| 226 |
+
if name_pinyin == "":
|
| 227 |
+
pinyin_list = pinyin(name.text, style=Style.NORMAL)
|
| 228 |
+
name_pinyin = (pinyin_list[0][0]+' ' + "".join([item[0] for item in pinyin_list[1:]])).title()
|
| 229 |
+
name_pinyin, background, next_height = write_text(name_pinyin, font_path_notosans, background, H_PADDING_RATION,next_height+V_PADDING, font_size)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
next_height += 100
|
| 233 |
+
font_size = 70
|
| 234 |
+
for title in title_list:
|
| 235 |
+
title, background, next_height = write_text(title, font_path_notosans, background, H_PADDING_RATION,next_height+V_PADDING, font_size)
|
| 236 |
+
|
| 237 |
+
background = background.convert("RGB")
|
| 238 |
+
img_stream = io.BytesIO()
|
| 239 |
+
background.save(img_stream, format="JPEG")
|
| 240 |
+
img_stream.seek(0)
|
| 241 |
+
if os.path.exists("src/assets/output"):
|
| 242 |
+
background.save("src/assets/output/嘉宾海报.jpg")
|
| 243 |
+
return img_stream
|
| 244 |
+
|
| 245 |
+
def make_guest_poster(name, name_pinyin, photo_link, original_photo_link, titles):
|
| 246 |
+
if photo_link != '':
|
| 247 |
+
res = requests.get(photo_link)
|
| 248 |
+
guest_photo = Image.open(io.BytesIO(res.content)).convert("RGBA")
|
| 249 |
+
elif original_photo_link != '':
|
| 250 |
+
res = requests.get(original_photo_link)
|
| 251 |
+
original_guest_photo = Image.open(io.BytesIO(res.content))
|
| 252 |
+
guest_photo = rm_img_bg(original_guest_photo)
|
| 253 |
+
if os.path.exists("src/assets/output"):
|
| 254 |
+
guest_photo.save("src/assets/output/rmbg.png")
|
| 255 |
+
else:
|
| 256 |
+
return None
|
| 257 |
+
img_stream = compose_guest_poster(name, name_pinyin, guest_photo, titles)
|
| 258 |
+
return img_stream
|
| 259 |
+
|
| 260 |
+
def write_text(text, font_path, image, H_PADDING_RATION, start_height, font_size):
|
| 261 |
+
bg_width = image.size[0]
|
| 262 |
+
text_width = get_text_width(font_path, font_size, text)
|
| 263 |
+
text = text_info(text=text, font_size=font_size,
|
| 264 |
+
position=[bg_width*(1-H_PADDING_RATION) - text_width, start_height],
|
| 265 |
+
color=(255,255,255),
|
| 266 |
+
max_width=bg_width*(1-2*H_PADDING_RATION),
|
| 267 |
+
font_path=font_path)
|
| 268 |
+
image, next_height = write_text_on_image(image, text)
|
| 269 |
+
return text,image,next_height
|
| 270 |
+
# return img_stream
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
img_path_dict = {
|
| 275 |
+
"old": "src/assets/InvitationTemplateCompressed.pdf",
|
| 276 |
+
"common": "src/assets/旧空白邀请函图片.jpg",
|
| 277 |
+
"观礼": "src/assets/带名字.jpg",
|
| 278 |
+
"演讲": "src/assets/带名字和会.jpg",
|
| 279 |
+
"致辞": "src/assets/带名字和会.jpg",
|
| 280 |
+
}
|
| 281 |
+
font_path = "src/assets/ZhouZiSongTi7000Zi-2.otf"
|
| 282 |
+
font_path = "src/assets/FangZhengHeiTiJianTi-1.ttf"
|
| 283 |
+
|
| 284 |
+
def writeInvitationPDF(person, debug=False):
|
| 285 |
+
"旧空白邀请函图片.jpg"
|
| 286 |
+
font_size=40
|
| 287 |
+
resized_name_font_size = font_size*5/len(person.name+person.title)
|
| 288 |
+
resized_conference_font_size = font_size*9/len(person.conference)
|
| 289 |
+
img_path = img_path_dict[person.category]
|
| 290 |
+
image = Image.open(img_path)
|
| 291 |
+
draw = ImageDraw.Draw(image)
|
| 292 |
+
text_color = (255, 255, 255) # 白色 RGB
|
| 293 |
+
|
| 294 |
+
draw.text((182, 150+max(0, font_size-resized_name_font_size)), person.name+person.title, fill=text_color, font=ImageFont.truetype(font_path, min(font_size, resized_name_font_size)))
|
| 295 |
+
draw.text((102, 532+max(0, font_size-resized_conference_font_size)), person.conference, fill=text_color, font=ImageFont.truetype(font_path, min(font_size, resized_conference_font_size)))
|
| 296 |
+
imgStream = io.BytesIO()
|
| 297 |
+
# image.save(imgStream)
|
| 298 |
+
# doc.save(pdfStream)
|
| 299 |
+
# doc.close()
|
| 300 |
+
if debug:
|
| 301 |
+
image.save("output/image_with_text.jpg")
|
| 302 |
+
|
| 303 |
+
return imgStream
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
if __name__ == "__main__":
|
| 309 |
+
# image_path = 'src/assets/邀请函封面.jpg' # 替换为你的图片文件路径
|
| 310 |
+
# text_to_write = "你好,世界!"
|
| 311 |
+
# font_path = "src/assets/FangZhengHeiTiJianTi-1.ttf"
|
| 312 |
+
# position = (120, 140)
|
| 313 |
+
# write_text_on_image(image_path, text_to_write, position=position, font_path=font_path)
|
| 314 |
+
from collections import namedtuple
|
| 315 |
+
Person = namedtuple('Person', ['name', 'title', 'category', 'conference', 'respond_person'])
|
| 316 |
+
person = Person(name="一二三四五六", title="先生", category="演讲", conference="8月21日中欧智慧论坛,做主题演讲", respond_person="Eazy")
|
| 317 |
+
output_path = 'output/output.pdf'
|
| 318 |
+
# writeInvitationPDF(person, debug=True)
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
text_list = ["""Shanghai 协砺科技有限公司专注于青少年科技教育12年,通过STEAM25教育模式培养青少年的科学思维和创新能力。在全国设有数十家学习中心,服务上万名学员,提供科创、机器人和编程课程。公司还设有赛事研究中心,由专业工程师团队提供赛事研究和策略支持。学员在各类科技赛事中屡获佳绩。协砺科技致力于推动中国科技教育的创新与发展,为国家繁荣贡献力量。
|
| 322 |
+
|
| 323 |
+
【主要产品】科创、编程、赛事
|
| 324 |
+
【核心特点】为青少年提供优质的科创学习规划。
|
| 325 |
+
【核心亮点】高效提供更优质的赛事解决方案——快速赋能当地传统教育企业转型智能化教育赛道。""",
|
| 326 |
+
"""天天乐学创办于2015年6月,总部位于北京,创始团队均来自清北等知名高校,旗下产品天天乐学定制app,主打立体化教学和趣味性学习,可深度匹配教学需求,实现个性化教学。目前合作校近三万家,遍布全国31个省,300座城市,是国内知名的教育科技公司。
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
【主要产品】独立教师·机构·绘本馆英语APP定制
|
| 330 |
+
【核心特点】内容深度定制,帮助机构实现线上线下联动教学
|
| 331 |
+
""",
|
| 332 |
+
"""【】星瀚之光是明德传承集团旗下文化传媒品牌
|
| 333 |
+
成立于2023年,立足深圳,布局全国九大中心城市,是国内首家定位于价值观驱动的P产业链开发的MCN机构,拥有知识P孵化事业群和商业P孵化事业群两大主营板块,旗下涵盖了创始人P孵化直播电商、私域消费电商、企业整合营销、艺人经纪等业务,致力于成为中国规模最大的P生态搭建领军企业。
|
| 334 |
+
|
| 335 |
+
【】坚守“自利利他、奋斗为本、价值为纲、成就客户”的企业价值观
|
| 336 |
+
公司成立以来发展迅猛,迅速孵化出各大赛道的多个头部P,霸榜各大短视频平台榜单,包括:有家庭教育赛道:白瑞(家庭教育赛道头部主播);身心灵赛道:尚致胜(新中式心理学首创者);女性成长赛道:黄欢;商业赛道:苏建诚(亚细亚集团联合创始人)、王怀南(宝宝树创始人)、杨晓燕(长江商学院助理院长)、何慕(联众智达董事长)等;国学赛道:沈德斌(了凡四训学者)、钱锦国老师等。""",
|
| 337 |
+
"""学趋坊是一家为学校、机构、智能自习室等提供在线化和智能化解决方案的数字化教育内容供应商。已经为多个知名的在线教育品牌、智能自习室品牌提供0DM和OEM服务,产品用户累计超过1000万。
|
| 338 |
+
|
| 339 |
+
【主要产品】智能校本
|
| 340 |
+
【核心特点】为本地化内容插上科技的翅膀。
|
| 341 |
+
【核心亮点】高质量高效本地化教研—快速赋能当地传统教育企业转型智能化教育赛道。"""
|
| 342 |
+
]
|
| 343 |
+
logo_path_list = [
|
| 344 |
+
"src/assets/logo/学区房logo.png",
|
| 345 |
+
"src/assets/logo/星瀚之光logo.jpg",
|
| 346 |
+
"src/assets/logo/软科起点logo.png",
|
| 347 |
+
"src/assets/logo/天天乐学logo.png",
|
| 348 |
+
]
|
| 349 |
+
slogan_list = [
|
| 350 |
+
"探索科技,启迪未来。",
|
| 351 |
+
"价值观驱动的知识P全域孵化领先平台",
|
| 352 |
+
"定制英语APP学习系统"
|
| 353 |
+
]
|
| 354 |
+
FOREGROUND_ID = 3 # 0, 1, 2, 3
|
| 355 |
+
SLOGAN_ID = 1 # 0, 1, 2
|
| 356 |
+
TEXT_ID = 2 # 0, 1, 2, 3
|
| 357 |
+
|
| 358 |
+
LOGO_LINK = "https://weboffice-temporary.ks3-cn-beijing.wpscdn.cn/thumbnail/tJUjbgWdD6R5k5rECKka1yK_100.png?Expires=1720890000&KSSAccessKeyId=AKLTmoJhggaFT1CHuozGZqbC&Signature=IxmcUo8eX3VzsRltml9F8AQ5%2FDY%3D&response-cache-control=public%2Cmax-age%3D86400"
|
| 359 |
+
# make_exhibitor_poster(LOGO_LINK, slogan_list[SLOGAN_ID], text_list[TEXT_ID])
|
| 360 |
+
name, titles = "王千里", "全国政协文员\n好好先生"
|
| 361 |
+
PHOTO_LINK = "https://weboffice-temporary.ks3-cn-beijing.wpscdn.cn/thumbnail/tpcdnbPEKEJ3ge831dSHEd3_100.png?Expires=1720890000&KSSAccessKeyId=AKLTmoJhggaFT1CHuozGZqbC&Signature=jeKkEy%2BTx3BAMNzqrmjUbg6uBK8%3D&response-cache-control=public%2Cmax-age%3D86400"
|
| 362 |
+
ORIGINAL_PHOTO_LINK = "https://weboffice-temporary.ks3-cn-beijing.wpscdn.cn/thumbnail/tZCeTRiNFQbksEEF4WMR8ZX_100.jpeg?Expires=1721070000&KSSAccessKeyId=AKLTmoJhggaFT1CHuozGZqbC&Signature=Hc2iv3U3EBQ86xqQLztLqdU3zuk%3D&response-cache-control=public%2Cmax-age%3D86400"
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
# res = requests.get(ORIGINAL_PHOTO_LINK)
|
| 366 |
+
# # guest_photo = Image.open(io.BytesIO(res.content)).convert("RGBA")
|
| 367 |
+
# original_guest_photo = Image.open(io.BytesIO(res.content)).convert("RGBA")
|
| 368 |
+
# guest_photo = rm_img_bg(original_guest_photo)
|
| 369 |
+
|
| 370 |
+
# compose_guest_poster(name, guest_photo, titles)
|
| 371 |
+
|
| 372 |
+
make_guest_poster(name, "", "", ORIGINAL_PHOTO_LINK, titles)
|
| 373 |
+
# for file in os.listdir("src/assets/GuestPoster/original_guest"):
|
| 374 |
+
# image = Image.open(os.path.join("src/assets/GuestPoster/original_guest", file))
|
| 375 |
+
# image = rm_img_bg(image)
|
| 376 |
+
# image.save(os.path.join("src/assets/output", file.replace("jpg", "png")))
|
| 377 |
+
|