-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPicGenerator.py
113 lines (90 loc) · 3.62 KB
/
PicGenerator.py
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
# -*- coding: utf-8 -*-
from PIL import Image, ImageFont, ImageDraw
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import time, sys
from io import BytesIO
reload(sys)
sys.setdefaultencoding('utf-8')
try:
import cStringIO as StringIO
except ImportError:
import StringIO
import pic_config
W, H = pic_config.BACKGROUND_W, pic_config.BACKGROUND_H
class SharingPicGenerator(object):
def __init__(self, background_fp, w, h):
self.bk_im = Image.open(background_fp).convert()
self.paste_list = []
self.text_list = []
self.w = w
self.h = h
self.bk_im.save('string_io_out.png')
def add_paste_task(self, pic_fp, region_box, mask=None):
"""
:param mask:
:param pic_fp: anything can be opened by Image.open()
:param region_box: tuple of for lines
:return:
"""
pic_foo = Image.open(pic_fp)
width = region_box[2] - region_box[0]
height = region_box[3] - region_box[1]
resized_pic = pic_foo.resize((width, height), Image.ANTIALIAS)
if mask:
resized_pic.putalpha(255)
self.paste_list.append(tuple([resized_pic, region_box, resized_pic])) # 【-1】有问题
else:
self.paste_list.append(tuple([resized_pic, region_box, None]))
def __do_paste(self):
for task in self.paste_list:
self.bk_im.paste(task[0], task[1], task[2])
def add_text_task(self, text_args, *font_args):
"""
中文必须是Unicode
如果是向右对其的,需要xy是右上角的点,x是最右边距离左边框的像素数
:param text_args:
:param font_args:
:return:
"""
_font = ImageFont.truetype(*font_args, index=text_args['index'])
draw = ImageDraw.Draw(self.bk_im)
if text_args['align'] == 'center':
# w, h = _font.getsize(text_args['text'])
# os = _font.getoffset(text_args['text'])
w, h = draw.textsize(text_args['text'], font=_font)
# w0 = len(text_args['text']) * font_args[-1]
new_xy = ((W - w) / 2, text_args['xy'][-1])
self.text_list.append(tuple([new_xy, unicode(text_args['text']), _font]))
elif text_args['align'] == 'right':
# 由右上角计算出左上角
w, h = draw.textsize(text_args['text'], font=_font)
new_xy = ((text_args['xy'][0] - w), text_args['xy'][-1])
self.text_list.append(tuple([new_xy, unicode(text_args['text']), _font]))
else:
self.text_list.append(tuple([text_args['xy'], unicode(text_args['text']), _font]))
def __add_text(self):
draw = ImageDraw.Draw(self.bk_im)
for task in self.text_list:
draw.text(xy=task[0], text=task[1], font=task[2]) # 这里需要改进
def generate(self):
self.__do_paste()
self.__add_text()
rs = BytesIO()
self.bk_im.save(rs, format='PNG')
return rs
if __name__ == '__main__':
with open('background.png',
'rb') as f_in:
tmp = f_in.read()
io_t = BytesIO()
io_t.write(tmp)
tmp_foo = SharingPicGenerator(io_t)
tmp_foo.add_paste_task('default_photo.png', (210, 210, 330, 330))
# import emoji
text_args = {'text': u'测试', 'xy': (0, 75), 'align': 'center'}
tmp_foo.add_text_task(text_args, pic_config.DEFAULT_FONT, 45)
text_args = {'text': u'测试', 'xy': (W - 102, 544), 'align': 'right'}
tmp_foo.add_text_task(text_args, pic_config.DEFAULT_FONT, 28)
with open('out_test_pingfang_2.png', 'wb') as f_o:
f_o.write(tmp_foo.generate().getvalue())