用python写一个福字(附完整代码)
作者:上进小菜猪 时间:2022-10-18 12:37:40
前言:
支付宝 2022 集五福活动正式开启
数据显示,过去六年累计参与支付宝集五福的人数已经超过了 7 亿,每 2 个中国人里就有 1 个曾扫福、集福、送福。
一,扫五福活动如此火爆,为何不自己利用编程来生成福字!
首先作品奉上:
①,导入python库
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_word 的作用为爬取我们需要的汉字模型。
如图:
③,爬取背景底图
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()
⑤,成品展示
二,完整代码
完整代码奉上,需要先安装需要的python库。
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')
三,总结
来源:https://blog.csdn.net/weixin_52908342/article/details/122602064
标签:python,福
0
投稿
猜你喜欢
python中使用正则表达式的连接符示例代码
2021-07-08 09:34:43
Go1.18新特性使用Generics泛型进行流式处理
2024-02-17 07:12:42
IP地址正则表达式匹配方法
2023-06-11 18:58:03
MySQL存储过程中的sql_mode问题
2011-01-04 19:50:00
我所钟爱的HTML5资源
2010-07-23 09:25:00
Python cookbook(数据结构与算法)在字典中将键映射到多个值上的方法
2023-07-14 14:37:00
屏幕分辨率和布局简述
2009-04-20 20:27:00
python中判断数字是否为质数的实例讲解
2022-02-17 13:19:05
js三维正方体(兼容ie/ff)
2008-04-12 14:38:00
JavaScript Memoization
2008-05-01 12:48:00
Python 解析库json及jsonpath pickle的实现
2021-09-10 00:27:14
深入分析在Python模块顶层运行的代码引起的一个Bug
2021-06-29 01:26:29
python中numpy基础学习及进行数组和矢量计算
2023-01-22 16:32:04
Python Merge函数原理及用法解析
2021-10-02 00:16:11
阿里云CentOS7搭建Apache+PHP+MySQL环境
2023-11-23 02:44:59
pytorch model.cuda()花费时间很长的解决
2021-12-08 08:02:06
Python+Opencv实现图像匹配功能(模板匹配)
2022-06-28 08:56:35
Python 获取windows桌面路径的5种方法小结
2023-04-01 09:06:47
Python爬虫天气预报实例详解(小白入门)
2023-03-23 16:05:48
类似google的ASP分页代码[测试通过]
2009-03-13 12:43:00