前言
嗨喽,大家好呀~这里是爱看美女的茜茜呐
又到了学Python时刻~
每逢春节,无论城市还是农村,
家家户户都要挑漂亮的红春联贴于门上,辞旧迎新,增加喜庆的节日气氛。
据说这一习俗起于宋代,在明代开始盛行,
到了清代,春联的思想性和艺术性都有了很大的提高。
现在贴春联已成风俗,红色的对联贴在大门上,房子顿时生辉。
正如诗云:“喜气临门红色妍,家家户户贴春联;旧年辞别迎新岁,时序车轮总向前。”
今天,我们就用python代码来实现一个春联生成器吧~
春联turtir
代码展示
from turtle import *
bgcolor("lightsalmon")
pensize(5)
setup(1400, 1000)
update()
fillcolor("chocolate")
pencolor("brown")
pu()
goto(-330, 200)
seth(-90)
pd()
begin_fill()
for i in range(2):
fd(560)
left(90)
fd(180)
left(90)
end_fill()
pu()
goto(-150, 200)
seth(-90)
pd()
begin_fill()
for i in range(2):
left(90)
fd(180)
left(90)
end_fill()
fillcolor("lightsalmon")
pu()
goto(-150, -80)
seth(180)
fd(40)
right(90)
pd()
begin_fill()
circle(20)
end_fill()
pu()
seth(0)
fd(80)
right(90)
pd()
begin_fill()
circle(20)
end_fill()
fillcolor("red")
pencolor("firebrick2")
pu()
goto(-330, 250)
seth(0)
pd()
begin_fill()
for i in range(2):
fd(360)
left(90)
fd(100)
left(90)
end_fill()
pencolor("black")
pu()
texts = ["安", "国", "泰", "民"]
for text in texts:
fd(72)
write(text, align="center", font=("华文行楷", 60, "normal"))
pencolor("firebrick2")
goto(-380, 200)
seth(180)
pd()
begin_fill()
for i in range(2):
fd(100)
left(90)
fd(560)
left(90)
end_fill()
pu()
goto(-430, 200)
pencolor("black")
seth(-90)
texts = "民安国泰逢盛世"
for text in texts:
fd(80)
write(text, align="center", font=("华文行楷", 60, "normal"))
pencolor("firebrick2")
goto(180, 200)
seth(180)
pd()
begin_fill()
for i in range(2):
fd(100)
left(90)
fd(560)
left(90)
end_fill()
pu()
goto(130, 200)
pencolor("black")
seth(-90)
texts = "风调雨顺颂华年"
for text in texts:
fd(80)
write(text, align="center", font=("华文行楷", 60, "normal"))
# snowman
pencolor("black")
pensize(2)
fillcolor("white")
pu()
goto(400, -100)
seth(0)
pd()
begin_fill()
circle(65)
end_fill()
begin_fill()
circle(-105)
end_fill()
pensize(6)
pu()
goto(355, -10)
seth(50)
pd()
circle(-25, 100)
pu()
goto(355, -40)
seth(-45)
pd()
fillcolor("orange")
pensize(3)
begin_fill()
circle(-15, 90)
goto(295, -55)
goto(355, -40)
end_fill()
pu()
goto(400, -100)
seth(-90)
colors = ["red", "orange", "blue", "green"]
for i in range(4):
fd(42)
dot(20, colors[i])
pensize(3)
goto(420, -150)
seth(-75)
pd()
fd(100)
right(20)
for i in range(3):
fd(30)
bk(30)
left(20)
fillcolor("black")
pencolor("black")
pensize(3)
def shape1(pos):
pu()
goto(pos)
seth(115)
pd()
begin_fill()
circle(5)
end_fill()
fd(40)
right(80)
fd(60)
right(100)
fd(40)
begin_fill()
circle(-5)
end_fill()
def shape2(pos):
pu()
goto(pos)
seth(60)
pd()
begin_fill()
circle(5)
end_fill()
fd(40)
right(180)
circle(20, 40)
circle(-20, 45)
def shape3(pos):
pu()
goto(pos)
seth(90)
begin_fill()
circle(5)
end_fill()
fd(45)
shape1((200, 280))
shape2((300, 240))
shape3((340, 180))
shape3((-400, 320))
shape2((-480, 280))
shape1((-455, 380))
ht()
done()
效果展示
春联生成器
准备
第三方模块使用:
- PIL
- numpy
- requests
安装python第三方模块:
-
win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
-
在pycharm中点击Terminal(终端) 输入安装命令
代码展示
导入模块
import io
from PIL import Image
import numpy as np
import requests
"""
获取单个汉字(字符)的图片
ch
- 单个汉字或英文字母(仅支持大写)
quality
- 单字分辨率,H-640像素,M-480像素,L-320像素
"""
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)
"""
生成春联
text
- 春联内容,以空格断行
HorV
- H-横排,V-竖排
quality
- 单字分辨率,H-640像素,M-480像素,L-320像素
out_file
- 输出文件名
"""
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()
text1 = '四季财源顺时来 一年好景同春到'
text2 = '财源广进'
write_couplets(text1, HorV='V', quality='M', out_file='上下批.jpg')
write_couplets(text2, HorV='H', quality='M', out_file='横批.jpg')
效果展示
尾语
感谢你观看我的文章呐~本次航班到这里就结束啦 🛬
希望本篇文章有对你带来帮助 🎉,有学习到一点知识~
躲起来的星星🍥也在努力发光,你也要努力加油(让我们一起努力叭)。
网友评论