脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 利用PyQt5生成过年春联

利用PyQt5生成过年春联

2022-08-28 12:17Python 集中营 Python

这篇文章主要介绍了如何利用PyQt5生成过年春联。通过在界面上输入春联的上、下批和横批汉字从而生成春联图像,最后将春联图片保存。需要的可以参考一下

需求说明:

通过在界面上输入春联的上、下批和横批汉字从而生成春联图像,最后将春联图片保存。有实际需要的还可以将春联打印。

利用PyQt5生成过年春联

利用PyQt5生成过年春联

实现过程:

实现思路是先下载好春联的背景图片,再下载每个汉字的文字图片将文字图片粘贴到春联背景上。所以这里有用了一个春联图片的三方获取地址。

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={"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))  # 单个文字的形状是正方形,所以这里的长、宽都是一致的

效果图

利用PyQt5生成过年春联

到此这篇关于利用PyQt5生成过年春联的文章就介绍到这了,更多相关PyQt5春联内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/lwsbc/p/15828476.html

延伸 · 阅读

精彩推荐
  • PythonPython实现批量更换指定目录下文件扩展名的方法

    Python实现批量更换指定目录下文件扩展名的方法

    这篇文章主要介绍了Python实现批量更换指定目录下文件扩展名的方法,结合完整实例分析了Python批量修改文件扩展名的技巧,并对比分析了shell命令及scandir的...

    RQSLT3222020-09-08
  • Pythonpython 19个值得学习的编程技巧

    python 19个值得学习的编程技巧

    这篇文章主要介绍了python 19个值得学习的编程技巧,帮助大家更好的理解和学习python,感兴趣的朋友可以了解下 ...

    用户16344494402020-08-15
  • Pythonpython使用knn实现特征向量分类

    python使用knn实现特征向量分类

    这篇文章主要为大家详细介绍了python使用knn实现特征向量分类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    RossieSeven7002021-05-08
  • Python使用Python机器学习降低静态日志噪声

    使用Python机器学习降低静态日志噪声

    今天小编就为大家分享一篇关于使用Python和机器学习的静态日志噪声的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友...

    Tristan de Cacqueray10292021-04-05
  • PythonPython中的引用知识点总结

    Python中的引用知识点总结

    在本文里我们给大家整理了关于Python中的引用知识点以及相关代码总结,需要的朋友们跟着学习下。...

    脚本之家10972021-06-28
  • PythonPython中字符串格式化str.format的详细介绍

    Python中字符串格式化str.format的详细介绍

    python从2.6开始支持format,新的更加容易读懂的字符串格式化方法,下面这篇文章主要介绍了Python中字符串格式化str.format的相关资料,需要的朋友可以参考借...

    旷世的忧伤6742020-09-21
  • PythonPython实现梯度下降法的示例代码

    Python实现梯度下降法的示例代码

    梯度下降法的机器学习的重要思想之一,梯度下降法的目标,是使得代价函数最小。本文将对梯度下降算法的原理及实现展开详细介绍,感兴趣的快跟随小...

    侯小啾6842022-08-08
  • PythonTensorflow实现卷积神经网络的详细代码

    Tensorflow实现卷积神经网络的详细代码

    这篇文章主要为大家详细介绍了Tensorflow实现卷积神经网络的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    ztchun8532021-02-24