Skip to content

Commit bd3af68

Browse files
committed
提交代码
1 parent f4d78d5 commit bd3af68

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

xianhuan/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Python技术 公众号文章代码库
1010

1111
## 实例代码
1212

13+
[Python写春联,瑞雪兆丰年!](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/couplets): Python写春联,瑞雪兆丰年!
14+
1315
[回顾2021!爬取12万数据盘点热门大事件!](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/weibohot):回顾2021!爬取12万数据盘点热门大事件!
1416

1517
[不买礼物,准备给她画一棵精美圣诞树!](https://github.com/JustDoPython/python-examples/tree/master/xianhuan/christmastree):不买礼物,准备给她画一棵精美圣诞树!

xianhuan/couplets/draw1.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@author: 闲欢
5+
"""
6+
import io
7+
from PIL import Image
8+
import requests
9+
10+
11+
def get_word(ch, quality):
12+
"""获取单个汉字(字符)的图片
13+
ch - 单个汉字或英文字母(仅支持大写)
14+
quality - 单字分辨率,H-640像素,M-480像素,L-320像素
15+
"""
16+
17+
fp = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch': ch}).content)
18+
im = Image.open(fp)
19+
w, h = im.size
20+
if quality == 'M':
21+
w, h = int(w * 0.75), int(0.75 * h)
22+
elif quality == 'L':
23+
w, h = int(w * 0.5), int(0.5 * h)
24+
25+
return im.resize((w, h))
26+
27+
28+
def get_bg(quality):
29+
"""获取春联背景的图片"""
30+
31+
return get_word('bg', quality)
32+
33+
34+
def write_couplets(text, HorV='V', quality='L', out_file=None):
35+
"""生成春联
36+
37+
text - 春联内容,以空格断行
38+
HorV - H-横排,V-竖排
39+
quality - 单字分辨率,H-640像素,M-480像素,L-320像素
40+
out_file - 输出文件名
41+
"""
42+
43+
usize = {'H': (640, 23), 'M': (480, 18), 'L': (320, 12)}
44+
bg_im = get_bg(quality)
45+
text_list = [list(item) for item in text.split()]
46+
rows = len(text_list)
47+
cols = max([len(item) for item in text_list])
48+
49+
if HorV == 'V':
50+
ow, oh = 40 + rows * usize[quality][0] + (rows - 1) * 10, 40 + cols * usize[quality][0]
51+
else:
52+
ow, oh = 40 + cols * usize[quality][0], 40 + rows * usize[quality][0] + (rows - 1) * 10
53+
out_im = Image.new('RGBA', (ow, oh), '#f0f0f0')
54+
55+
for row in range(rows):
56+
if HorV == 'V':
57+
row_im = Image.new('RGBA', (usize[quality][0], cols * usize[quality][0]), 'white')
58+
offset = (ow - (usize[quality][0] + 10) * (row + 1) - 10, 20)
59+
else:
60+
row_im = Image.new('RGBA', (cols * usize[quality][0], usize[quality][0]), 'white')
61+
offset = (20, 20 + (usize[quality][0] + 10) * row)
62+
63+
for col, ch in enumerate(text_list[row]):
64+
if HorV == 'V':
65+
pos = (0, col * usize[quality][0])
66+
else:
67+
pos = (col * usize[quality][0], 0)
68+
69+
ch_im = get_word(ch, quality)
70+
row_im.paste(bg_im, pos)
71+
row_im.paste(ch_im, (pos[0] + usize[quality][1], pos[1] + usize[quality][1]), mask=ch_im)
72+
73+
out_im.paste(row_im, offset)
74+
75+
if out_file:
76+
out_im.convert('RGB').save(out_file)
77+
out_im.show()
78+
79+
80+
# text = '龙引千江水 虎越万重山'
81+
# write_couplets(text, HorV='V', quality='M', out_file='竖联.jpg')
82+
83+
text = '龙腾虎跃'
84+
write_couplets(text, HorV='H', quality='M', out_file='横联.jpg')

0 commit comments

Comments
 (0)