欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用python写一个福字(附完整代码)

程序员文章站 2022-03-09 08:32:36
目录前言:一,扫五福活动如此火爆,为何不自己利用编程来生成福字!二,完整代码三,总结前言:支付宝 2022 集五福活动正式开启数据显示,过去六年累计参与支付宝集五福的人数已经超过了 7 亿,每 2 个...

前言:

支付宝 2022 集五福活动正式开启

数据显示,过去六年累计参与支付宝集五福的人数已经超过了 7 亿,每 2 个中国人里就有 1 个曾扫福、集福、送福。

一,扫五福活动如此火爆,为何不自己利用编程来生成福字!

首先作品奉上:

用python写一个福字(附完整代码)

①,导入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 的作用为爬取我们需要的汉字模型。
如图:

用python写一个福字(附完整代码)

③,爬取背景底图

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写一个福字(附完整代码)

二,完整代码

完整代码奉上,需要先安装需要的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')

三,总结

到此这篇关于用python写一个福字(附完整代码)的文章就介绍到这了,更多相关python写福字内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: python