利用Python代碼制作過年春聯(lián)
一、春聯(lián)一
1.效果展示

2.代碼展示
index.html
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <title>css3春聯(lián)切換</title> <link rel="stylesheet" href="css/style.css" rel="external nofollow" > </head> <body> <div class="rotating-text"> <p>春聯(lián)Show</p> <p> <span class="word alizarin">上聯(lián):這個(gè)需求很簡(jiǎn)單</span> <span class="word wisteria">下聯(lián):怎么實(shí)現(xiàn)我不管</span> <span class="word peter-river">橫批:明天上線!</span> </p> </div> <script src="js/script.js"></script> </body> </html>
style.css
@import url(https://fonts.googleapis.com/css?family=Lato:600);
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #222;
}
.rotating-text {
font-family: Lato, sans-serif;
font-weight: 600;
font-size: 36px;
color: white;
transform: translateX(-80px);
}
.rotating-text p {
display: inline-flex;
margin: 0;
vertical-align: top;
}
.rotating-text p .word {
position: absolute;
display: flex;
opacity: 0;
}
.rotating-text p .word .letter {
transform-origin: center center 25px;
}
.rotating-text p .word .letter.out {
transform: rotateX(90deg);
transition: 0.32s cubic-bezier(0.6, 0, 0.7, 0.2);
}
.rotating-text p .word .letter.in {
transition: 0.38s ease;
}
.rotating-text p .word .letter.behind {
transform: rotateX(-90deg);
}
.alizarin {
color: #e74c3c;
}
.wisteria {
color: #8e44ad;
}
.peter-river {
color: #3498db;
}
.emerald {
color: #2ecc71;
}
.sun-flower {
color: #f1c40f;
}
script.js
var words = document.querySelectorAll(".word");
words.forEach(function (word) {
var letters = word.textContent.split("");
word.textContent = "";
letters.forEach(function (letter) {
var span = document.createElement("span");
span.textContent = letter;
span.className = "letter";
word.append(span);
});
});
var currentWordIndex = 0;
var maxWordIndex = words.length - 1;
words[currentWordIndex].style.opacity = "1";
var rotateText = function () {
var currentWord = words[currentWordIndex];
var nextWord = currentWordIndex === maxWordIndex ? words[0] : words[currentWordIndex + 1];
// rotate out letters of current word
Array.from(currentWord.children).forEach(function (letter, i) {
setTimeout(function () {
letter.className = "letter out";
}, i * 80);
});
// reveal and rotate in letters of next word
nextWord.style.opacity = "1";
Array.from(nextWord.children).forEach(function (letter, i) {
letter.className = "letter behind";
setTimeout(function () {
letter.className = "letter in";
}, 340 + i * 80);
});
currentWordIndex =
currentWordIndex === maxWordIndex ? 0 : currentWordIndex + 1;
};
rotateText();
setInterval(rotateText, 4000);
二、春聯(lián)二
1.環(huán)境準(zhǔn)備
當(dāng)缺少庫(kù)時(shí)會(huì)有相應(yīng)提示 黑窗口執(zhí)行下方命令+包名即可下載安裝
博主此處用的idea 直接Alt+Enter直接下載就成了
idea配置python環(huán)境也可以參考此文:Python及PyCharm下載與安裝教程
2.效果展示


3.代碼
import io
from PIL import Image
#import numpy as np
import requests
def get_word(ch, quality):
"""獲取單個(gè)漢字(字符)的圖片
ch - 單個(gè)漢字或英文字母(僅支持大寫)
quality - 單字分辨率,H-640像素,M-480像素,L-320像素
"""
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):
"""獲取春聯(lián)背景的圖片"""
return get_word('bg', quality)
def write_couplets(text, HorV='V', quality='L', out_file=None):
"""生成春聯(lián)
text - 春聯(lián)內(nèi)容,以空格斷行
HorV - H-橫排,V-豎排
quality - 單字分辨率,H-640像素,M-480像素,L-320像素
out_file - 輸出文件名
"""
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 = '思前想后幾行代碼筑萬載春秋 扶內(nèi)保外一千精英帶五千干將' #對(duì)聯(lián)內(nèi)容
write_couplets(text, HorV='V', quality='M', out_file='春聯(lián).jpg') #生成普天同慶.jpg對(duì)聯(lián)圖片
以上就是利用Python代碼制作過年春聯(lián)的詳細(xì)內(nèi)容,更多關(guān)于Python制作春聯(lián)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Windows下Anaconda和PyCharm的安裝與使用詳解
這篇文章主要介紹了Windows下Anaconda和PyCharm的安裝與使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python使用textcase庫(kù)輕松實(shí)現(xiàn)文本格式處理
在Python開發(fā)中,規(guī)范的文本格式處理是提升代碼可讀性和維護(hù)性的關(guān)鍵一環(huán),本文將系統(tǒng)講解textcase庫(kù)的核心功能,典型應(yīng)用場(chǎng)景及性能優(yōu)化策略,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
python實(shí)戰(zhàn)之PyQt5實(shí)現(xiàn)漫畫臉
本文詳細(xì)講解了python實(shí)戰(zhàn)之PyQt5實(shí)現(xiàn)漫畫臉的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
python實(shí)現(xiàn)在字符串中查找子字符串的方法
這篇文章主要介紹了python實(shí)現(xiàn)在字符串中查找子字符串的方法,涉及Python中find方法的相關(guān)使用技巧,需要的朋友可以參考下2015-07-07

