File size: 10,348 Bytes
7f62904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# coding: utf-8
import os 
from datetime import datetime
from PIL import Image,ImageFilter,ImageFont,ImageDraw
import matplotlib.pyplot as plt 
import math
from PIL import Image, ImageDraw, ImageFont
import textwrap
import random

import IPython.display as ipd
post_img = "res_post.jpg"   # 底图,确定尺寸
import textwrap


# 最佳实践
# lines = textwrap.wrap(text, width=40)
# y_text = h
# for line in lines:
#     width, height = font.getsize(line)
#     draw.text(((w - width) / 2, y_text), line, font=font, fill=FOREGROUND)
#     y_text += height
    
#  将文字居中显示
def longTextCut(text, line_words):
    """cut the text to len / given_num  lines"""
    total_len = len(text)
    num_line = math.ceil(total_len/line_words)
    num_line = max(num_line, 1)
    res = []
    for i in range(num_line):
        start_inx = int(i*line_words)
        end_inx = int(line_words* (i+1))
        sub_seq = text[start_inx : end_inx]
        res.append(sub_seq)
    lines = textwrap.wrap(text, width=line_words)
    return res
    return lines

# lines = textwrap.wrap(text, width=10)

#  w文字变形,每行不超过多少字,方便阅读
def reshape_texts(lines, line_words):
    new_lines = []
    for it in lines:
        sub_lines = it.split("\n")
        split_lines = []
        for inx, sub_it in enumerate(sub_lines):
            if len(sub_it) > line_words:
                relen_text = longTextCut(sub_it, line_words)
                split_lines.extend(relen_text)
            else:
                split_lines.append(sub_it)
        new_lines.extend(split_lines)
    return new_lines


#  w文字变形,每行不超过多少字,方便阅读
def reshape_texts_end(lines, line_words):
    new_lines = []
    for it in lines:
        sub_lines = it.split("。")
        # t_ = [len(i) for i in sub_lines]
        # y_ = [i > line_words for i in t_]
        split_lines = []
        for inx, sub_it in enumerate(sub_lines):
            if len(sub_it) > line_words:
                relen_text = longTextCut(sub_it, line_words)
                split_lines.extend(relen_text)
            else:
                split_lines.append(sub_it)
        new_lines.extend(split_lines)
    return new_lines




#高斯模糊类
class MyGaussianBlur(ImageFilter.Filter):
    name = "GaussianBlur"
    def __init__(self, radius=2, bounds=None):
        self.radius = radius
        self.bounds = bounds
    def filter(self, image):
        if self.bounds:
            clips = image.crop(self.bounds).gaussian_blur(self.radius)
            image.paste(clips, self.bounds)
            return image
        else:
            return image.gaussian_blur(self.radius)

# self.background_url=get_bing_image()
#卡片生成类     16:9
class myPost:
    def __init__(self, front_img=None,
            img = r'./broadcast/kaobianBottem.jpeg'
        ):
        self.frontground = Image.open(front_img) if front_img else None  # bing img 
        #     if type(image) is str:
        #     return image
        # elif type(image) is Image.Image:
        #     return encode_pil_to_base64(image).decode()
        self.img= Image.open(img) if type(img) is str else Image.fromarray(img)  #底稿,确定尺寸
        self.icon=None 
        # self.icon=Image.open(self.icon_url).convert("RGBA")
        #纵向间距
        # self.spacing=spacing
        self.width, self.height=self.img.size

    #加载小图
    def loadIcon(self, point=(50,50)):
        """point=(x,y)"""
        pass
        # self.img.paste(self.icon,point,self.icon)
    def get_width_height(self):
        return self.width,self.height
    
    def getLinesCount(self, sz, spacing):
        words = int(self.width / sz) - 2
        rows = int( self.height / (sz + spacing))
        return (words, rows)

    #高斯模糊图片作为背景
    def drawBlur(self):
        backflur = self.frontground.resize((self.width,self.height), resample=3).filter(MyGaussianBlur(radius=35))
        self.img.paste(backflur,(0, 0))

    #添加前景图片            
    def drawFrontground(self):
        """添加一个前景图壁纸,高斯虚化 
        """
        if not self.frontground:
            return
        x=0
        y=int(self.height*2/3.3)  # 在下半部分画这个图
        srcwidth,srcheight=self.frontground.size
        height=int(srcheight * self.width / srcwidth)
        #重设图片尺寸
        frontground = self.frontground.resize((self.width, height),Image.Resampling.LANCZOS)
        
        alpha_layer = Image.new('L', (self.width, height), 0)
        draw = ImageDraw.Draw(alpha_layer)
        sz = (self.width/2-100, 0, self.width-30, height)
        # print("the area is :", sz)
        draw.ellipse(sz, fill=150)
        self.img.paste(frontground,(x,y),alpha_layer)

    def get_res(self):
        return self.img

    def postTextLine(self, title="Text--test", font="./simsun.ttc", sz=50, x=40, y=90, color = "red"):
        """main title"""
        draw = ImageDraw.Draw(self.img)
        font = ImageFont.truetype(font=font, size=sz)
        draw.text((x,y), title, font=font, fill=color)
        
    def postBoxText(self, texts=["Text--test\n sub text"], font="./simsun.ttc", sz=50, x=40, y=390,
                    color = "red", spacing=40, ali = "left", bac_color=None):
        """lines"""
        # 定义文本内容
        draw = ImageDraw.Draw(self.img)
        font = ImageFont.truetype(font=font, size=sz)
        
        text = "\n".join(texts)
        bbox = draw.multiline_textbbox((0, 0), text, font=font, spacing=spacing)
        
        # 计算文本的宽度和高度
        width = bbox[2] - bbox[0]
        width += int(width * 0.3)
        height = bbox[3] - bbox[1]
        height += int(height * 0.3)
        if bac_color is not None:
            draw.rectangle((x-int(width * 0.15), y-int(height * 0.15), x + width, y + height), fill=bac_color)
        # 计算文本的位置
        # x = (self.width - width) / 2
        # y = (remain_height - height) / 2  # 在剩下的高度中居中显示
        # y = 0 + spacing * 2    #  从头开始展示        
        # 绘制文本
        draw.multiline_text((x, y), text, fill=color, font=font, align=ali, spacing=spacing)

    # 放置文字
    def  postText(self, texts=["Text--test\n sub text"], font="./simsun.ttc", sz=50, x=40, y=390,
                  color = "red", spacing=20):
        draw = ImageDraw.Draw(self.img)
        # Set the font, text contents, and line spacing
        font = ImageFont.truetype(font=font, size=sz)
        # 定义文本内容
        text = "\n".join(texts)
        draw.multiline_text((x, y), text, fill=color, font=font, align="left", spacing=spacing)
        
        # 获取文本的边界框
        # bbox = draw.multiline_textbbox((0, 0), text, font=font, spacing=spacing)


        # 绘制文本
        # draw.text((x,title_y), title, font=title_front, fill="red")
        # draw.multiline_text((x, y), text, fill=color, font=font, align="left", spacing=spacing)


    def drawCard(self):
        if frontground:
            self.drawFrontground()
        self.postText(title, texts, sz = 40)  
        
    def drawCardExample(self):
        self.drawFrontground()
        self.postTextLine(title="Text--test", font="./simsun.ttc", sz=size, x=40, y=90, color = "red")
        self.postTextLine(title="content of sub text", font="./simsun.ttc", sz=size, x=40, y=90, color = "green")
    def show(self):
        pass

    #保存到本地
    def saveCard(self, path_name):
        save_name =  path_name
        # save_name = str(datetime.today()).split()[0]+'.png'
        self.img.save(save_name)
        return save_name

def generatePost(lines = [""], front_img=None, bac_img="./kaobianBottem.jpeg", fn_name = "test_v1.png"):
    """给定背景,前景,文字,size,生成单张图, 并环肥内存中的图对象"""
    postcard=myPost(front_img=front_img, img = bac_img)
    line_words, rows = postcard.getLinesCount(sz, spacing)
    texts = reshape_texts(lines, line_words)
    print("总行数",len(texts))
    postcard.drawFrontground()
    postcard.postText(texts[0], texts[1:], sz = 40)
    save_name=postcard.saveCard(fn_name)
    # ---opend image
    generated_image = postcard.get_res()
    return generated_image


if __name__ == '__main__':
    # 输入 -========================
    title = "大王叫我来巡山并发布公告,解决就业问题山中有老虎猴子承办问"
    lines = ['四、报名及网上缴费','本次公开考试招聘采取网络报名方式进行,不组织现场报名。报名时间:2023年3月10日至3月17日24:00。',
            '报名网站:广元人事考试网(http://gypta.e21cn.com/)。']
        
    # text = doc['fee_sj']
    # lines = content_with_date(text)  # 只需要有日期信息的字符串
    # texts = "\n".join(lines)
    result_post_file = "post_v1.png"
    bac_img = "./kaobianBottem.jpeg"
    # front_img = "./bingpost/2023-05-13.png"
    bing_post = "bingpost/"
    imgs_ = os.listdir(bing_post)
    imgs_ = [i for i in imgs_ if i.endswith("png")]
    ch_img = random.choice(imgs_)
    front_img = os.path.join(bing_post, ch_img)
    
    postcard=myPost(front_img=front_img, img = bac_img)
    postcard.drawFrontground()
    # 放置标题  + 字体字号
    sz=80
    spacing=20
    words, rows = postcard.getLinesCount(sz=sz, spacing=spacing)
    title_text = longTextCut(title, words)
    title_text = textwrap.wrap(title, width=words)

    print(title_text)
    postcard.postText(title_text, font="./simsun.ttc", sz = sz,
                      x=40, y=150, color = "blue", spacing=20)
            
    sz=50
    spacing=20
    words, rows = postcard.getLinesCount(sz=sz, spacing=spacing)
    texts = longTextCut(lines[1], words)
    print(texts)
    postcard.postText(texts, font="./simsun.ttc", sz = sz,
                      x=40, y=490, color = "green", spacing=spacing)

    
    texts = reshape_texts(lines, words)
    print("texts 长文本 总行数",len(texts))
    postcard.postText([lines[0]],   font="./simsun.ttc", sz = int(sz*1.5),x=40, y=790, color = "red", spacing=spacing)
    postcard.postText(texts,   font="./simsun.ttc", sz = sz,x=40, y=990, color = "green", spacing=spacing)
    save_name=postcard.saveCard(result_post_file)
    print(save_name)
    # postcard.show()