利用PyQt5生成過年春聯(lián)
需求說明:
通過在界面上輸入春聯(lián)的上、下批和橫批漢字從而生成春聯(lián)圖像,最后將春聯(lián)圖片保存。有實(shí)際需要的還可以將春聯(lián)打印。


實(shí)現(xiàn)過程:
實(shí)現(xiàn)思路是先下載好春聯(lián)的背景圖片,再下載每個(gè)漢字的文字圖片將文字圖片粘貼到春聯(lián)背景上。所以這里有用了一個(gè)春聯(lián)圖片的三方獲取地址。
http://xufive.sdysit.com/tk
春聯(lián)生成部分參考了 CSDN 博客平臺(tái)。
網(wǎng)絡(luò)數(shù)據(jù)獲取相關(guān)模塊
import io # python IO 處理模塊 from PIL import Image # 圖像處理模塊 import requests # 網(wǎng)絡(luò)請(qǐng)求模塊
UI 相關(guān)模塊
from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import *
主題樣式模塊引用
from QCandyUi import CandyWindow
應(yīng)用操作相關(guān)模塊
import sys import os
UI界面主要代碼展示
def init_ui(self):
self.setWindowTitle('春聯(lián)生成器')
self.setWindowIcon(QIcon('春聯(lián).ico'))
vbox_main = QVBoxLayout()
self.image_label = QLabel()
self.image_label.setScaledContents(True)
self.image_label.setMaximumSize(650,150)
self.image_label.setPixmap(QPixmap('橫批演示.jpg'))
hbox = QHBoxLayout()
self.brower = QTextBrowser()
self.brower.setFont(QFont('宋體', 8))
self.brower.setReadOnly(True)
self.brower.setPlaceholderText('信息展示區(qū)域')
self.brower.ensureCursorVisible()
form = QFormLayout()
self.up_label = QLabel()
self.up_label.setText('設(shè)置上聯(lián)')
self.up_text = QLineEdit()
self.up_text.setPlaceholderText('請(qǐng)輸入上聯(lián)')
self.down_label = QLabel()
self.down_label.setText('設(shè)置下聯(lián)')
self.down_text = QLineEdit()
self.down_text.setPlaceholderText('請(qǐng)輸入下聯(lián)')
self.h_label = QLabel()
self.h_label.setText('設(shè)置橫批')
self.h_text = QLineEdit()
self.h_text.setPlaceholderText('請(qǐng)輸入橫批')
self.thread_ = WorkThread(self)
self.thread_.trigger.connect(self.update_log)
self.thread_.finished.connect(self.finished)
self.save_path = QLineEdit()
self.save_path.setReadOnly(True)
self.save_btn = QPushButton()
self.save_btn.setText('存儲(chǔ)路徑')
self.save_btn.clicked.connect(self.save_btn_click)
form.addRow(self.up_label, self.up_text)
form.addRow(self.down_label, self.down_text)
form.addRow(self.h_label, self.h_text)
form.addRow(self.save_path, self.save_btn)
vbox = QVBoxLayout()
self.start_btn = QPushButton()
self.start_btn.setText('開始生成春聯(lián)')
self.start_btn.clicked.connect(self.start_btn_click)
vbox.addLayout(form)
vbox.addWidget(self.start_btn)
hbox.addWidget(self.brower)
hbox.addLayout(vbox)
vbox_main.addWidget(self.image_label)
vbox_main.addLayout(hbox)
self.setLayout(vbox_main)
槽函數(shù)的應(yīng)用
def update_log(self, text):
'''
槽函數(shù):向文本瀏覽器中寫入內(nèi)容
:param text:
:return:
'''
cursor = self.brower.textCursor()
cursor.movePosition(QTextCursor.End)
self.brower.append(text)
self.brower.setTextCursor(cursor)
self.brower.ensureCursorVisible()
def save_btn_click(self):
dicr = QFileDialog.getExistingDirectory(self, '選擇文件夾', os.getcwd())
self.save_path.setText(dicr)
def start_btn_click(self):
self.start_btn.setEnabled(False)
self.thread_.start()
def finished(self, finished):
if finished is True:
self.start_btn.setEnabled(True)
h_image = self.save_path.text().strip() + '/橫批.jpg'
if os.path.isfile(h_image):
self.image_label.setPixmap(QPixmap(h_image))
self.update_log('由于上下聯(lián)不好預(yù)覽,請(qǐng)使用圖片查看器預(yù)覽,目前僅支持橫批圖片預(yù)覽...')
春聯(lián)文字獲取主題代碼
def run(self):
up_text = self.parent.up_text.text().strip()
down_text = self.parent.down_text.text().strip()
h_text = self.parent.h_text.text().strip()
save_path = self.parent.save_path.text().strip()
if up_text == '' or down_text == '' or h_text == '' or save_path == '':
self.trigger.emit('參數(shù)設(shè)置不允許為空,請(qǐng)?jiān)O(shè)置好后重新開始!')
self.finished.emit(True)
else:
text = up_text + ' ' + down_text
self.generate_image(text, layout='V', pre=0.75, out_file=save_path + '/上下聯(lián).jpg')
self.generate_image(h_text, layout='H', pre=0.75, out_file=save_path + '/橫批.jpg')
self.finished.emit(True)
文字圖片獲取部分
def get_word_image(self, ch='bg', pre=1.0):
'''
單文字圖片下載函數(shù)
:param ch: 默認(rèn)網(wǎng)絡(luò)請(qǐng)求參數(shù)'bg'
:param pre: 單個(gè)文字對(duì)象
:return: 圖像對(duì)象
'''
res = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data={'ch': ch}).content)
image = Image.open(res)
w, h = image.size
w, h = int(w * float(pre)), int(h * float(pre))
return image.resize((w, h)) # 單個(gè)文字的形狀是正方形,所以這里的長(zhǎng)、寬都是一致的
效果圖

到此這篇關(guān)于利用PyQt5生成過年春聯(lián)的文章就介紹到這了,更多相關(guān)PyQt5春聯(lián)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch 實(shí)現(xiàn)查看網(wǎng)絡(luò)中的參數(shù)
今天小編就為大家分享一篇pytorch 實(shí)現(xiàn)查看網(wǎng)絡(luò)中的參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python項(xiàng)目實(shí)戰(zhàn)之使用Django框架實(shí)現(xiàn)支付寶付款功能
這篇文章主要介紹了Python項(xiàng)目實(shí)戰(zhàn)之使用Django框架實(shí)現(xiàn)支付寶付款功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Caffe卷積神經(jīng)網(wǎng)絡(luò)視覺層Vision?Layers及參數(shù)詳解
這篇文章主要為大家介紹了Caffe卷積神經(jīng)網(wǎng)絡(luò)視覺層Vision?Layers及參數(shù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
django序列化時(shí)使用外鍵的真實(shí)值操作
這篇文章主要介紹了django序列化時(shí)使用外鍵的真實(shí)值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
python條件語(yǔ)句和while循環(huán)語(yǔ)句
這篇文章主要介紹了python條件語(yǔ)句和while循環(huán)語(yǔ)句,文章基于python的相關(guān)資料展開對(duì)其條件語(yǔ)句及while循環(huán)語(yǔ)句的詳細(xì)內(nèi)容介紹,需要的小伙伴可以參考一下2022-04-04
Python數(shù)據(jù)分析庫(kù)PyGWalker的強(qiáng)大交互式功能界面探索
這篇文章主要介紹了Python數(shù)據(jù)分析庫(kù)PyGWalker的強(qiáng)大交互式功能界面探索有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
詳解python 拆包可迭代數(shù)據(jù)如tuple, list
拆包是指將一個(gè)結(jié)構(gòu)中的數(shù)據(jù)拆分為多個(gè)單獨(dú)變量中。下面通過本文給大家介紹python 拆包可迭代數(shù)據(jù)如tuple, list的相關(guān)資料,需要的朋友參考下吧2017-12-12

