-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstitchimages.py
128 lines (106 loc) · 3.68 KB
/
stitchimages.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
import os
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def stitchimages_v1(image_path, poetry, QRcode_path, font_path, copyright, copyright_font_path, output_path):
"""
:param image_path:
:param poetry:
:param QRcode_path:
:param font_path:
:param copyright:
:param copyright_font_path:
:param output_path:
:return:
"""
QR_size = 100
if os.path.exists(image_path):
img = Image.open(image_path)
img_w, img_h = img.size
else:
raise RuntimeError('image path :{} not exists '.format(image_path))
if os.path.exists(QRcode_path):
QRcode = Image.open(QRcode_path)
QRcode = resize_image(QRcode, (QR_size, QR_size))
else:
raise RuntimeError("QRcode path :{} not exists ".format(QRcode_path))
poetry_image, poetry_font_size = poetry2image(poetry, font_path)
poetry_w, poetry_h = poetry_image.size
copyright_image= copyright2image(copyright, copyright_font_path)
copyright_w, copyright_h = copyright_image.size
assert poetry_h > QR_size + copyright_h, "poetry height should be larger than QRcode+copyright height"
poetry_image.paste(QRcode, (poetry_w - QR_size, poetry_h - QR_size - copyright_h-poetry_font_size))
poetry_image.paste(copyright_image,(0,poetry_h - copyright_h))
assert img_w == 400 and poetry_w == 400 and copyright_w == 400, "check the width of image, poetry and copyright"
total_w = img_w
total_h = img_h + poetry_h + copyright_h
final_img = Image.new('RGB', (total_w, total_h), "white")
final_img.paste(img, (0, 0))
final_img.paste(poetry_image, (0, img_h))
final_img.show()
final_img.save(output_path)
# return final_img
def poetry2image(poetry, font_path=None):
"""
:param poetry:
:param font_path:
:return:
"""
font_size = 5
sentences = poetry.split()
num_lines = len(sentences)
total_h = (font_size) * (num_lines + 2)
total_h = max(200, total_h)
font_size = int(total_h / (num_lines + 2))
image = Image.new("RGB", (400, int(total_h)), "white")
draw = ImageDraw.Draw(image)
if font_path:
font = ImageFont.truetype(font_path, font_size)
else:
font = None
draw.multiline_text(xy=(font_size * 0.1, font_size*0.5), text=poetry, fill=(0, 0, 0), font=font,
spacing=font_size * 0.2, align='left')
del draw
return image, font_size
def copyright2image(copyright, font_path=None):
"""
:param copyright:
:param font_path:
:return:
"""
length = len(copyright)
font_size = 15
total_h = (font_size) * 1.1
image = Image.new("RGB", (400, int(total_h)), "white")
draw = ImageDraw.Draw(image)
if font_path:
font = ImageFont.truetype(font_path, font_size)
else:
font = None
draw.multiline_text(xy=(font_size * 0.1, 0), text=copyright, fill=(192, 192, 192), font=font,
align='left')
del draw
return image
def resize_image(img, size=(100, 100)):
"""
:param img:
:param size:
:return:
"""
try:
if img.mode not in ('L', 'RGB'):
img = img.convert('RGB')
img = img.resize(size)
except Exception(e):
pass
return img
if __name__ == "__main__":
image_path = './im2.png'
poetry = "一二三四五六七\n一二三四五六七\n一二三四五六七\n一二三四五六七"
QRcode_path = './QR2.png'
font_path = './fonts/fanti.TTF'
output_path = '.'
copyright = '来自 PoemScape\n'
copyright_font_path = './fonts/yuesong.TTF'
stitchimages_v1(image_path, poetry, QRcode_path, font_path, copyright, copyright_font_path, output_path)