目录
1.效果演示
步骤:
1.自定义上下联,横批。
2.选择字体、字体颜色,背景。
3.点击生成,即可在右边预览效果。
4.如有需要也可以点击保存,在程序同目录下生成。
2.说明
本程序在博主天元浪子的程序基础上包装改进而成,他本人已经同意。仅用于交流学习使用。转载请注明出处。
最新版的Python写春联,支持行书隶书楷书,不再有缺失汉字_Python作业辅导员 - 天元浪子-CSDN博客
3.开发工具及环境。
开发工具:pycharm,Qtdesigner,qtuic(把ui文件转化为py文件的外部工具)
系统环境:win11
第三方库:PIL, cv2, numpy, freetype, pyqt5,pyinstaller
PIL cv2:图像处理库
freetype:实现矢量字体显示
pyqt5,Qtdesigner,qtuic:开发图像界面工具
pyinstaller:程序的包装。(生成exe文件,即使电脑没有python环境,也可以运行)
4.文件关系
5.开发流程
5.1明确功能
首先,明确我们的程序需要包含哪几个功能。先确定好,方便图像界面,和程序的对应开发。这里我列出功能:
1.自定义春联文字内容。
2.自定义字体
3.自定义字体颜色
4.定义背景图片
5.生成图片
6.在界面中显示
7.保存图片
5.2设计界面
使用Qt designer 设计界面。注意要包含我们的功能。教程csdn上也有。
保存ui文件
5.3将ui文件转化为py文件。
使用pyuic外部工具。(需要自己安装的PyQt5界面快速开发(QtDesigner与PyUIC工具)_YMilton的专栏-CSDN博客_pyqt pyuic)
生成的py文件
5.4编写按钮和接口
我们按下按钮,就会发出一个信号,这个信号会触发一个函数,这个函数有个名字,叫做槽。
具体教程请参照:pyqt5自定义的信号与槽
这里我们定义了“生成按钮”的槽。
def create_slot(self): self.up_couplet_str = self.lineEdit_up.text() self.down_couplet_str = self.lineEdit_down.text() self.h_couplet_str = self.lineEdit_h.text() # 字体颜色设置 self.font_color = self.comboBox_font_color.currentText() if self.font_color == '黑色字': self.rgb = (0,0,0) elif self.font_color == '金色字': self.rgb = (255,215,0) else: self.rgb = (255,255,255) #背景 self.bg = self.comboBox_bg.currentText() #字体 self.font = self.comboBox_font.currentText() #生成图片 self.img_up = create_img.write_couplets(self.up_couplet_str,color=self.rgb, bg=self.bg, font=self.font, horv='V', quality='H') self.img_down = create_img.write_couplets(self.down_couplet_str, color=self.rgb, bg=self.bg, font=self.font, horv='V', quality='H') self.img_h = create_img.write_couplets(self.h_couplet_str, color=self.rgb, bg=self.bg, font=self.font, horv='H', quality='H') self.show_img_in_gui(self.img_up,position='up') self.show_img_in_gui(self.img_down, position='down') self.show_img_in_gui(self.img_h, position='h')
再讲信号与槽连接起来就好啦。
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton_create.clicked.connect(self.create_slot)
self.pushButton_save.clicked.connect(self.save_slot)
5.5 pyinstaller打包
转到程序目录下,命令行输入:
pyinstaller -F -w RunGUI.py
就开始打包啦。
最后显示success就好啦
把生成的exe放到原来程序的位置就好啦
就可以正常运行啦。
6.附上所有代码
请尊重劳动成果,转载请注明出处。
感谢耐心阅读。
工程文件下载:
链接:https://pan.baidu.com/s/1h6XVGM42lc-Su-OmYd10Jg
提取码:i5pg
6.1RunGUI.py
import sys import my_ui_converted #同一目录下的py文件(ui文件转化来的) from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5 import QtCore if __name__ == '__main__': app = QApplication(sys.argv) win = QMainWindow() ui = my_ui_converted.Ui_MainWindow() ui.setupUi(win) win.show() sys.exit(app.exec_())
6.2create_img.py
import os import freetype import numpy as np from PIL import Image FONT_FILE = r'./font/STLiti.ttf' BG_FILE = r'./bg/bg2.png' def text2image(word, font_file, size=128, color=(218, 179, 0)): """使用指定字库将单个汉字转为图像 word - 单个汉字字符串 font_file - 矢量字库文件名 size - 字号,默认128 color - 颜色,默认黑色 """ face = freetype.Face(font_file) face.set_char_size(size * size) face.load_char(word) btm_obj = face.glyph.bitmap w, h = btm_obj.width, btm_obj.rows pixels = np.array(btm_obj.buffer, dtype=np.uint8).reshape(h, w) dx = int(face.glyph.metrics.horiBearingX / 64) if dx > 0: patch = np.zeros((pixels.shape[0], dx), dtype=np.uint8) pixels = np.hstack((patch, pixels)) #r = np.ones(pixels.shape) * color[0] * 255 #g = np.ones(pixels.shape) * color[1] * 255 #b = np.ones(pixels.shape) * color[2] * 255 r = np.ones(pixels.shape) * color[0] g = np.ones(pixels.shape) * color[1] b = np.ones(pixels.shape) * color[2] im = np.dstack((r, g, b, pixels)).astype(np.uint8) return Image.fromarray(im) def write_couplets(text, horv='V', quality='L', color =(0,0,0) ,bg = '传统红背景',font = '华文隶书',out_file=None): """写春联 text - 春联字符串 bg - 背景图片路径 horv - H-横排,V-竖排 quality - 单字分辨率,H-640像素,L-320像素 out_file - 输出文件名 """ size, tsize = (320, 128) if quality == 'L' else (640, 180) ow, oh = (size, size * len(text)) if horv == 'V' else (size * len(text), size) im_out = Image.new('RGBA', (ow, oh), '#f0f0f0') # 背景选择 if bg == '经典红背景': im_bg = Image.open('./bg/bg1.png') elif bg == '卡通背景': im_bg = Image.open('./bg/bg2.png') if size < 640: im_bg = im_bg.resize((size, size)) if horv == 'H': im_bg = im_bg.rotate(90) #字体选择 if font == '华文隶书': font_file = './font/STLiti.ttf' elif font == '华文行楷': font_file = './font/hwxk.ttf' # 生成图片 for i, w in enumerate(text): im_w = text2image(w, font_file, size=tsize, color=color) w, h = im_w.size dw, dh = (size - w) // 2, (size - h) // 2 if horv == 'V': im_out.paste(im_bg, (0, i * size)) im_out.paste(im_w, (dw, i * size + dh), mask=im_w) else: im_out.paste(im_bg, (i * size, 0)) im_out.paste(im_w, (i * size + dw, dh), mask=im_w) #im_out.save('%s.png' % text) #os.startfile('%s.png' % text) return im_out if __name__ == '__main__': write_couplets('欢度春节', horv='V', quality='H') #write_couplets('国泰民安', horv='H', quality='H')
6.3my_ui_converted.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'my_ui.ui' # # Created by: PyQt5 UI code generator 5.15.4 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets import create_img import cv2 from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QGraphicsScene, QGraphicsPixmapItem import numpy class Ui_MainWindow(object): def __init__(self): self.up_couplet_str = '测试上联' self.down_couplet_str = '测试下联' self.h_couplet_str = '测试横批' def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(950, 679) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.frame = QtWidgets.QFrame(self.centralwidget) self.frame.setGeometry(QtCore.QRect(478, 79, 463, 591)) self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel) self.frame.setFrameShadow(QtWidgets.QFrame.Raised) self.frame.setObjectName("frame") self.layoutWidget = QtWidgets.QWidget(self.frame) self.layoutWidget.setGeometry(QtCore.QRect(140, 90, 321, 391)) self.layoutWidget.setObjectName("layoutWidget") self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.layoutWidget) self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.graphicsView_down = QtWidgets.QGraphicsView(self.layoutWidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(1) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.graphicsView_down.sizePolicy().hasHeightForWidth()) self.graphicsView_down.setSizePolicy(sizePolicy) self.graphicsView_down.setObjectName("graphicsView_down") self.horizontalLayout_3.addWidget(self.graphicsView_down) spacerItem = QtWidgets.QSpacerItem(120, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_3.addItem(spacerItem) self.graphicsView_up = QtWidgets.QGraphicsView(self.layoutWidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(1) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.graphicsView_up.sizePolicy().hasHeightForWidth()) self.graphicsView_up.setSizePolicy(sizePolicy) self.graphicsView_up.setObjectName("graphicsView_up") self.horizontalLayout_3.addWidget(self.graphicsView_up) self.layoutWidget1 = QtWidgets.QWidget(self.frame) self.layoutWidget1.setGeometry(QtCore.QRect(170, 10, 251, 73)) self.layoutWidget1.setObjectName("layoutWidget1") self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.layoutWidget1) self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_4.setObjectName("horizontalLayout_4") self.graphicsView_h = QtWidgets.QGraphicsView(self.layoutWidget1) self.graphicsView_h.setObjectName("graphicsView_h") self.horizontalLayout_4.addWidget(self.graphicsView_h) self.line = QtWidgets.QFrame(self.frame) self.line.setGeometry(QtCore.QRect(70, -10, 20, 491)) self.line.setFrameShape(QtWidgets.QFrame.VLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.label_7 = QtWidgets.QLabel(self.frame) self.label_7.setGeometry(QtCore.QRect(350, 480, 91, 16)) self.label_7.setObjectName("label_7") self.label_8 = QtWidgets.QLabel(self.frame) self.label_8.setGeometry(QtCore.QRect(190, 500, 551, 20)) self.label_8.setObjectName("label_8") self.label_9 = QtWidgets.QLabel(self.frame) self.label_9.setGeometry(QtCore.QRect(310, 530, 461, 12)) self.label_9.setObjectName("label_9") self.widget = QtWidgets.QWidget(self.centralwidget) self.widget.setObjectName("widget") self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.widget) self.horizontalLayout_5.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_5.setObjectName("horizontalLayout_5") spacerItem1 = QtWidgets.QSpacerItem(250, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_5.addItem(spacerItem1) self.label = QtWidgets.QLabel(self.widget) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(36) self.label.setFont(font) self.label.setObjectName("label") self.horizontalLayout_5.addWidget(self.label) spacerItem2 = QtWidgets.QSpacerItem(250, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_5.addItem(spacerItem2) self.widget1 = QtWidgets.QWidget(self.centralwidget) self.widget1.setGeometry(QtCore.QRect(10, 80, 511, 541)) self.widget1.setObjectName("widget1") self.gridLayout_3 = QtWidgets.QGridLayout(self.widget1) self.gridLayout_3.setContentsMargins(0, 0, 0, 0) self.gridLayout_3.setObjectName("gridLayout_3") self.gridLayout = QtWidgets.QGridLayout() self.gridLayout.setObjectName("gridLayout") self.label_5 = QtWidgets.QLabel(self.widget1) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) self.label_5.setFont(font) self.label_5.setObjectName("label_5") self.gridLayout.addWidget(self.label_5, 0, 0, 1, 1) self.lineEdit_up = QtWidgets.QLineEdit(self.widget1) self.lineEdit_up.setObjectName("lineEdit_up") self.gridLayout.addWidget(self.lineEdit_up, 0, 1, 1, 1) spacerItem3 = QtWidgets.QSpacerItem(18, 15, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed) self.gridLayout.addItem(spacerItem3, 1, 1, 1, 1) self.label_3 = QtWidgets.QLabel(self.widget1) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) self.label_3.setFont(font) self.label_3.setObjectName("label_3") self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1) self.lineEdit_down = QtWidgets.QLineEdit(self.widget1) self.lineEdit_down.setObjectName("lineEdit_down") self.gridLayout.addWidget(self.lineEdit_down, 2, 1, 1, 1) spacerItem4 = QtWidgets.QSpacerItem(18, 15, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed) self.gridLayout.addItem(spacerItem4, 3, 1, 1, 1) self.label_4 = QtWidgets.QLabel(self.widget1) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) self.label_4.setFont(font) self.label_4.setObjectName("label_4") self.gridLayout.addWidget(self.label_4, 4, 0, 1, 1) self.lineEdit_h = QtWidgets.QLineEdit(self.widget1) self.lineEdit_h.setObjectName("lineEdit_h") self.gridLayout.addWidget(self.lineEdit_h, 4, 1, 1, 1) spacerItem5 = QtWidgets.QSpacerItem(18, 15, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed) self.gridLayout.addItem(spacerItem5, 5, 1, 1, 1) self.gridLayout_3.addLayout(self.gridLayout, 0, 0, 1, 2) self.gridLayout_2 = QtWidgets.QGridLayout() self.gridLayout_2.setObjectName("gridLayout_2") self.label_10 = QtWidgets.QLabel(self.widget1) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) self.label_10.setFont(font) self.label_10.setObjectName("label_10") self.gridLayout_2.addWidget(self.label_10, 0, 0, 1, 1) self.label_6 = QtWidgets.QLabel(self.widget1) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) self.label_6.setFont(font) self.label_6.setObjectName("label_6") self.gridLayout_2.addWidget(self.label_6, 1, 3, 1, 1) self.comboBox_bg = QtWidgets.QComboBox(self.widget1) self.comboBox_bg.setObjectName("comboBox_bg") self.comboBox_bg.addItem("") self.comboBox_bg.addItem("") self.comboBox_bg.addItem("") self.gridLayout_2.addWidget(self.comboBox_bg, 1, 4, 1, 1) self.label_2 = QtWidgets.QLabel(self.widget1) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.gridLayout_2.addWidget(self.label_2, 1, 0, 1, 1) self.comboBox_font_color = QtWidgets.QComboBox(self.widget1) self.comboBox_font_color.setObjectName("comboBox_font_color") self.comboBox_font_color.addItem("") self.comboBox_font_color.addItem("") self.comboBox_font_color.addItem("") self.gridLayout_2.addWidget(self.comboBox_font_color, 1, 1, 1, 1) self.comboBox_font = QtWidgets.QComboBox(self.widget1) self.comboBox_font.setObjectName("comboBox_font") self.comboBox_font.addItem("") self.comboBox_font.addItem("") self.gridLayout_2.addWidget(self.comboBox_font, 0, 1, 1, 1) self.gridLayout_3.addLayout(self.gridLayout_2, 1, 0, 1, 3) self.pushButton_create = QtWidgets.QPushButton(self.widget1) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton_create.sizePolicy().hasHeightForWidth()) self.pushButton_create.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.pushButton_create.setFont(font) self.pushButton_create.setObjectName("pushButton_create") self.gridLayout_3.addWidget(self.pushButton_create, 2, 0, 1, 1) spacerItem6 = QtWidgets.QSpacerItem(70, 20, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum) self.gridLayout_3.addItem(spacerItem6, 2, 1, 1, 1) self.pushButton_save = QtWidgets.QPushButton(self.widget1) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton_save.sizePolicy().hasHeightForWidth()) self.pushButton_save.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Arial") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.pushButton_save.setFont(font) self.pushButton_save.setObjectName("pushButton_save") self.gridLayout_3.addWidget(self.pushButton_save, 2, 2, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 950, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) self.pushButton_create.clicked.connect(self.create_slot) self.pushButton_save.clicked.connect(self.save_slot) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "春联生成器")) self.label_5.setText(_translate("MainWindow", "上联:")) self.label_3.setText(_translate("MainWindow", "下联:")) self.label_4.setText(_translate("MainWindow", "横批:")) self.pushButton_create.setText(_translate("MainWindow", "生成")) self.pushButton_save.setText(_translate("MainWindow", "保存")) self.label_2.setText(_translate("MainWindow", "字体颜色:")) self.label_6.setText(_translate("MainWindow", "背景:")) self.comboBox_bg.setItemText(0, _translate("MainWindow", "经典红背景")) self.comboBox_bg.setItemText(1, _translate("MainWindow", "平滑背景")) self.comboBox_bg.setItemText(2, _translate("MainWindow", "可莉")) self.label_7.setText(_translate("MainWindow", "version 1.2")) self.label_8.setText(_translate("MainWindow", "bilibili:千雪official csdn:快乐的太阳")) self.label_9.setText(_translate("MainWindow", "开源工具,全部免费")) self.comboBox_font_color.setItemText(0, _translate("MainWindow", "黑色字")) self.comboBox_font_color.setItemText(1, _translate("MainWindow", "金色字")) self.comboBox_font_color.setItemText(2, _translate("MainWindow", "金色闪亮亮字")) self.lineEdit_up.setText(_translate("MainWindow", "金牛辞岁寒风去")) self.lineEdit_down.setText(_translate("MainWindow", "瑞虎迎春喜气来")) self.lineEdit_h.setText(_translate("MainWindow", "虎年大吉")) self.label_10.setText(_translate("MainWindow", "字体:")) self.comboBox_font.setItemText(0, _translate("MainWindow", "华文隶书")) self.comboBox_font.setItemText(1, _translate("MainWindow", "华文行楷")) def show_img_in_gui(self,img,position): img = cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 转换图像通道 if position == 'h': img = cv2.resize(img,(249,71)) else: img = cv2.resize(img, (94, 389)) x = img.shape[1] # 获取图像大小 y = img.shape[0] xs = x * 3 frame = QImage(img.data, x, y, xs, QImage.Format_RGB888) # 处理图片虚影问 pix = QPixmap.fromImage(frame) item = QGraphicsPixmapItem(pix) # 创建像素图元 scene = QGraphicsScene() # 创建场景 scene.addItem(item) if position == 'up': self.graphicsView_up.setScene(scene) elif position == 'down': self.graphicsView_down.setScene(scene) elif position == 'h': self.graphicsView_h.setScene(scene) def create_slot(self): self.up_couplet_str = self.lineEdit_up.text() self.down_couplet_str = self.lineEdit_down.text() self.h_couplet_str = self.lineEdit_h.text() # 字体颜色设置 self.font_color = self.comboBox_font_color.currentText() if self.font_color == '黑色字': self.rgb = (0,0,0) elif self.font_color == '金色字': self.rgb = (255,215,0) else: self.rgb = (255,255,255) #背景 self.bg = self.comboBox_bg.currentText() #字体 self.font = self.comboBox_font.currentText() #生成图片 self.img_up = create_img.write_couplets(self.up_couplet_str,color=self.rgb, bg=self.bg, font=self.font, horv='V', quality='H') self.img_down = create_img.write_couplets(self.down_couplet_str, color=self.rgb, bg=self.bg, font=self.font, horv='V', quality='H') self.img_h = create_img.write_couplets(self.h_couplet_str, color=self.rgb, bg=self.bg, font=self.font, horv='H', quality='H') self.show_img_in_gui(self.img_up,position='up') self.show_img_in_gui(self.img_down, position='down') self.show_img_in_gui(self.img_h, position='h') def save_slot(self): self.up_couplet_str = self.lineEdit_up.text() self.down_couplet_str = self.lineEdit_down.text() self.h_couplet_str = self.lineEdit_h.text() # 字体颜色设置 self.font_color = self.comboBox_font_color.currentText() if self.font_color == '黑色字': self.rgb = (0, 0, 0) elif self.font_color == '金色字': self.rgb = (255, 215, 0) else: self.rgb = (255, 255, 255) # 背景 self.bg = self.comboBox_bg.currentText() # 字体 self.font = self.comboBox_font.currentText() # 生成图片 self.img_up = create_img.write_couplets(self.up_couplet_str, color=self.rgb, bg=self.bg, font=self.font, horv='V', quality='H') self.img_down = create_img.write_couplets(self.down_couplet_str, color=self.rgb, bg=self.bg, font=self.font, horv='V', quality='H') self.img_h = create_img.write_couplets(self.h_couplet_str, color=self.rgb, bg=self.bg, font=self.font, horv='H', quality='H') self.show_img_in_gui(self.img_up, position='up') self.show_img_in_gui(self.img_down, position='down') self.show_img_in_gui(self.img_h, position='h') self.img_up.save('上联-%s.png'%self.up_couplet_str) self.img_down.save('下联-%s.png' % self.down_couplet_str) self.img_h.save('横批-%s.png' % self.h_couplet_str)