Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PyQt5 to generate Spring Festival couplets

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use PyQt5 to generate Spring Festival couplets. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

Requirements:

By inputting the upper, lower and horizontal batches of Chinese characters of Spring Festival couplets on the interface, the Spring Festival couplets images are generated, and finally the Spring Festival couplets images are saved. There are practical needs can also be printed Spring Festival couplets.

Implementation process:

The idea is to download the background picture of Spring Festival couplets first, then download the text picture of each Chinese character and paste the text picture on the background of Spring Festival couplets. So here's a three-way address that uses a couplet picture.

http://xufive.sdysit.com/tk

The Spring Festival couplet generation part refers to the CSDN blog platform.

Network data acquisition related module

import io # python IO processing module from PIL import Image #image processing module import requests #network request module

UI related modules

from PyQt5.QtWidgets import *from PyQt5.QtCore import *from PyQt5.QtGui import *

Theme Style Module Reference

from QCandyUi import CandyWindow

Application Operations Related Modules

import sysimport os

UI interface main code display

def init_ui(self): self.setWindowTitle ('Spring Festival couplet generator') self.setWindowIcon(QIcon ('Spring Festival couples')) vbox_main = QVBoxLayout() self.image_label = QLabel() self.image_label.setScaledContents(True) self.image_label.setMaximumSize(650,150) self.image_label.setPixmap(QPixmap ('horizontal batch presentation.jpg ')) hbox = QHBoxLayout() self.brower = QTextBrowser() self. browser.setFont(QFont ('Arithmetic ', 8)) self.brower.setReadOnly(True) self. browser.setPlaceholderText ('Information Display Area ') self.brower.ensureCursorVisible() form = QFormLayout() self.up_label = QLabel() self.up_label.setText ('set couple') self.up_text = QLineEdit() self.up_text.setPlaceholderText ('please enter the couple') self.down_label = QLabel() self.down_label.setText ('set next couple') self.down_text = QLineEdit() self.down_text.setPlaceholderText ('Please enter the next couple') self.h_label = QLabel() self.h_label.setText ('set horizontal batch') self.h_text = QLineEdit() self.h_text.setPlaceholderText ('Please enter horizontal batch') 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 ('Storage Path') 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 ('start generating couplets ') 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)

Application of Slot Function

def update_log(self, text): ''' Slot function: writing to text browser :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, 'select folders', 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() + '/horizontal batch.jpg' if os.path.isfile(h_image): self.image_label.setPixmap(QPixmap(h_image)) self.update_log ('Because the upper and lower links are not easy to preview, please use the image viewer to preview. Currently, only horizontal batch image preview is supported... ')

Spring Festival couplet text get theme code

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 ('parameter setting cannot be blank. Please start again after setting! ') self.finished.emit(True) else: text = up_text + ' ' + down_text self.generate_image(text, layout='V', pre=0.75, out_file=save_path + '/upper and lower couples.jpg') self.generate_image(h_text, layout='H', pre=0.75, out_file=save_path + '/horizontal batch.jpg') self.finished.emit(True)

Text picture acquisition section

def get_word_image(self, ch='bg', pre=1.0): ''' Single text picture download function :param ch: Default network request parameter 'bg' :param pre: Single text object :return: Image object ''' 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)) #The shape of a single text is square, so the length and width here are consistent

renderings

About "how to use PyQt5 to generate Spring Festival couplets" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report