@[toc]
# 前言:支付宝 2022 集五福活动正式开启
**1 月 19 日消息,随着春节临近,2022 年支付宝集五福活动今日正式开启。此次集五福活动时间为 2022 年 1 月 19 日 00:00-1 月 31 日 22:00。**
数据显示,过去六年累计参与支付宝集五福的人数已经超过了 7 亿,每 2 个中国人里就有 1 个曾扫福、集福、送福。
# 一,扫五福活动如此火爆,为何不自己利用编程来生成福字!
首先作品奉上:
![在这里插入图片描述](https://img-blog.csdnimg.cn/ced22e744a56462090e2aa30a7acbb35.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LiK6L-b5bCP6I-c54yq,size_14,color_FFFFFF,t_70,g_se,x_16)
## ①,导入python库
```bash
# 作者CSDN:上进小菜猪 华为云社区同名账号
#csdn主页:https://blog.csdn.net/weixin_52908342
## 感谢关注
import io
from PIL import Image
import requests
```
## ②,利用爬虫,获取单个汉字
```bash
# 作者CSDN:上进小菜猪 华为云社区同名账号
#csdn主页:https://blog.csdn.net/weixin_52908342
## 感谢关注
def get_word(ch, quality):
fp = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch':ch}).content)
im = Image.open(fp)
w, h = im.size
if quality == 'M':
w, h = int(w*0.75), int(0.75*h)
elif quality == 'L':
w, h = int(w*0.5), int(0.5*h)
return im.resize((w,h))
```
`def get_word 的作用为爬取我们需要的汉字模型。`
如图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/3b632874faa54792a1d63ea39be8528d.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LiK6L-b5bCP6I-c54yq,size_13,color_FFFFFF,t_70,g_se,x_16)
## ③,爬取背景底图
```bash
# 作者CSDN:上进小菜猪 华为云社区同名账号
#csdn主页:https://blog.csdn.net/weixin_52908342
## 感谢关注
def get_bg(quality):
return get_word('bg', quality)
```
## ④,图片格式大小,配置函数
```python
# 作者CSDN:上进小菜猪 华为云社区同名账号
#csdn主页:https://blog.csdn.net/weixin_52908342
## 感谢关注
def write_couplets(text, HorV='V', quality='L', out_file=None):
usize = {'H':(640,23), 'M':(480,18), 'L':(320,12)}
bg_im = get_bg(quality)
text_list = [list(item) for item in text.split()]
rows = len(text_list)
cols = max([len(item) for item in text_list])
if HorV == 'V':
ow, oh = 40+rows*usize[quality][0]+(rows-1)*10, 40+cols*usize[quality][0]
else:
ow, oh = 40+cols*usize[quality][0], 40+rows*usize[quality][0]+(rows-1)*10
out_im = Image.new('RGBA', (ow, oh), '#f0f0f0')
for row in range(rows):
if HorV == 'V':
row_im = Image.new('RGBA', (usize[quality][0], cols*usize[quality][0]), 'white')
offset = (ow-(usize[quality][0]+10)*(row+1)-10, 20)
else:
row_im = Image.new('RGBA', (cols*usize[quality][0], usize[quality][0]), 'white')
offset = (20, 20+(usize[quality][0]+10)*row)
for col, ch in enumerate(text_list[row]):
if HorV == 'V':
pos = (0, col*usize[quality][0])
else:
pos = (col*usize[quality][0],0)
ch_im = get_word(ch, quality)
row_im.paste(bg_im, pos)
row_im.paste(ch_im, (pos[0]+usize[quality][1], pos[1]+usize[quality][1]), mask=ch_im)
out_im.paste(row_im, offset)
if out_file:
out_im.convert('RGB').save(out_file)
out_im.show()
```
## ⑤,成品展示
![在这里插入图片描述](https://img-blog.csdnimg.cn/19b9198fe0aa4bb0ac6a2ae6d953b1b0.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5LiK6L-b5bCP6I-c54yq,size_20,color_FFFFFF,t_70,g_se,x_16)
# 二,完整代码
完整代码奉上,需要先安装需要的python库。
```python
# 作者CSDN:上进小菜猪 华为云社区同名账号
#csdn主页:https://blog.csdn.net/weixin_52908342
## 感谢关注
import io
from PIL import Image
import requests
def get_word(ch, quality):
fp = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch':ch}).content)
im = Image.open(fp)
w, h = im.size
if quality == 'M':
w, h = int(w*0.75), int(0.75*h)
elif quality == 'L':
w, h = int(w*0.5), int(0.5*h)
return im.resize((w,h))
def get_bg(quality):
return get_word('bg', quality)
def write_couplets(text, HorV='V', quality='L', out_file=None):
usize = {'H':(640,23), 'M':(480,18), 'L':(320,12)}
bg_im = get_bg(quality)
text_list = [list(item) for item in text.split()]
rows = len(text_list)
cols = max([len(item) for item in text_list])
if HorV == 'V':
ow, oh = 40+rows*usize[quality][0]+(rows-1)*10, 40+cols*usize[quality][0]
else:
ow, oh = 40+cols*usize[quality][0], 40+rows*usize[quality][0]+(rows-1)*10
out_im = Image.new('RGBA', (ow, oh), '#f0f0f0')
for row in range(rows):
if HorV == 'V':
row_im = Image.new('RGBA', (usize[quality][0], cols*usize[quality][0]), 'white')
offset = (ow-(usize[quality][0]+10)*(row+1)-10, 20)
else:
row_im = Image.new('RGBA', (cols*usize[quality][0], usize[quality][0]), 'white')
offset = (20, 20+(usize[quality][0]+10)*row)
for col, ch in enumerate(text_list[row]):
if HorV == 'V':
pos = (0, col*usize[quality][0])
else:
pos = (col*usize[quality][0],0)
ch_im = get_word(ch, quality)
row_im.paste(bg_im, pos)
row_im.paste(ch_im, (pos[0]+usize[quality][1], pos[1]+usize[quality][1]), mask=ch_im)
out_im.paste(row_im, offset)
if out_file:
out_im.convert('RGB').save(out_file)
out_im.show()
text = '福'
write_couplets(text, HorV='V', quality='M', out_file='福.jpg')
```
# 三,后言
**祝大家都能扫出敬业福,除夕开大奖!!!!**
这个图片保存下来可以打印,清晰度很高,打印机性能好的话,可以打印出市场卖的那种品质。
(有任何问题的话,欢迎评论区或者私信我嗷,我都会看的!)
❤️❤️❤️如果你喜欢的话,就不要吝惜你的一键三连了~