由于篇幅有限,完整源代碼的獲取方式放在文末了,
【閱讀全文】
需求說明:
通過在界面上輸入春聯的上、下批和橫批漢字從而生成春聯影像,最后將春聯圖片保存,有實際需要的還可以將春聯列印,


實作程序:
實作思路是先下載好春聯的背景圖片,再下載每個漢字的文字圖片將文字圖片粘貼到春聯背景上,所以這里有用了一個春聯圖片的三方獲取地址,
http://xufive.sdysit.com/tk
春聯生成部分參考了 CSDN 博客平臺,
網路資料獲取相關模塊
import io # python IO 處理模塊
from PIL import Image # 影像處理模塊
import requests # 網路請求模塊
UI 相關模塊
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
主題樣式模塊參考
from QCandyUi import CandyWindow
應用操作相關模塊
import sys
import os
UI界面主要代碼展示
def init_ui(self):
self.setWindowTitle('春聯生成器')
self.setWindowIcon(QIcon('春聯.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('資訊展示區域')
self.brower.ensureCursorVisible()
form = QFormLayout()
self.up_label = QLabel()
self.up_label.setText('設定上聯')
self.up_text = QLineEdit()
self.up_text.setPlaceholderText('請輸入上聯')
self.down_label = QLabel()
self.down_label.setText('設定下聯')
self.down_text = QLineEdit()
self.down_text.setPlaceholderText('請輸入下聯')
self.h_label = QLabel()
self.h_label.setText('設定橫批')
self.h_text = QLineEdit()
self.h_text.setPlaceholderText('請輸入橫批')
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('存盤路徑')
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('開始生成春聯')
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)
槽函式的應用
def update_log(self, text):
'''
槽函式:向文本瀏覽器中寫入內容
: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('由于上下聯不好預覽,請使用圖片查看器預覽,目前僅支持橫批圖片預覽...')
春聯文字獲取主題代碼
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('引數設定不允許為空,請設定好后重新開始!')
self.finished.emit(True)
else:
text = up_text + ' ' + down_text
self.generate_image(text, layout='V', pre=0.75, out_file=save_path + '/上下聯.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):
'''
單文字圖片下載函式
:param ch: 默認網路請求引數'bg'
:param pre: 單個文字物件
:return: 影像物件
'''
res = io.BytesIO(requests.post(url='http://xufive.sdysit.com/tk', data=https://www.cnblogs.com/lwsbc/archive/2022/01/20/{'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)) # 單個文字的形狀是正方形,所以這里的長、寬都是一致的
完整原始碼獲取方式:公眾號內回復"春聯生成器",

我是 [Python 集中營]、很高興您看到了最后, 我是一個專注于 Python 知識分享的公眾號,希望可以得到您的關注~

【往期推薦】
記錄一下python中的十大%占位符對應的格式化...
PyQt5 UI 制作一個豆瓣電影資訊查看器,初識QThread多執行緒...
PyQt5 最小化到托盤,升級小鬧鐘...
pyinstaller打包exe檔案太大,利用pipenv輕松解決!
PyQt5 小工具:Excel資料分組匯總器...
歡迎關注作者公眾號【Python 集中營】,專注于后端編程,每天更新技術干貨,不定時分享各類資料!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/417071.html
標籤:其他
