python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python 贺卡

火遍网络的python中秋节贺卡现在学还赶得上

作者:顾木子吖

中秋将至,我用python编写了个火遍网络的中秋节贺卡,现在学起来还不晚,文中给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

导语

图片

转眼,八月十五中秋节即将到来,中秋节以月之圆兆人之团圆,

为寄托思念故乡,思念亲人之情,也是我国最具团圆意味的一个传统节日。

​佳节来临,我的侄女儿跟我打视频,说起了他们的中秋老师布置的小作业,每个孩子都会制作专属的中秋贺卡送给家人。​​

果不其然,这又成了我的一个灵感,跟小侄女儿打完视频就开始了我的贺卡制作之路。

佳节来临,不如制作一份精美的手工贺卡,在中秋之夜送去真挚的祝福!跟着小编来学学吧~

正文

本文是基于pyqt5做的界面化中秋贺卡生成器。

(1)首先准备好相应的素材、如文字字体、贺卡背景等,大家可以随机制作。

(2)咳咳咳!之前有人说我文章前文太长,让我直接上代码。

class newyearCardGUI(QtWidgets.QWidget):
    def __init__(self):
        super(newyearCardGUI, self).__init__()
        self.setFixedSize(600, 500)
        self.setWindowTitle('中秋贺卡生成器-源码基地:#959755565#')
        self.setWindowIcon(QIcon('icon/icon.png'))
        self.grid = QGridLayout()
        # 一些全局变量
        self.card_image = None
        self.font_size = 35
        # 定义组件
        # --Label
        self.content_label = QLabel('内容路径:')
        self.bg_label = QLabel('背景路径:')
        self.font_label = QLabel('字体路径:')
        self.fontcolor_label = QLabel('字体颜色:')
        self.show_label = QLabel()
        self.show_label.setScaledContents(True)
        self.show_label.setMaximumSize(600, 300)
        # --输入框
        self.content_edit = QLineEdit()
        self.content_edit.setText('contents/1.card')
        self.bg_edit = QLineEdit()
        self.bg_edit.setText('bgimages/1.png')
        self.font_edit = QLineEdit()
        self.font_edit.setText('fonts/font.TTF')
        # --按钮
        self.choose_content_button = QPushButton('选择路径')
        self.choose_bg_button = QPushButton('选择路径')
        self.choose_font_button = QPushButton('选择路径')
        self.generate_button = QPushButton('生成贺卡')
        self.save_button = QPushButton('保存贺卡')
        # --下拉框
        self.font_color_combobox = QComboBox()
        for color in ['red', 'white', 'black', 'blue', 'yellow', 'green']:
            self.font_color_combobox.addItem(color)
        # 布局
        self.grid.addWidget(self.show_label, 0, 0, 5, 5)
        self.grid.addWidget(self.content_label, 5, 0, 1, 1)
        self.grid.addWidget(self.content_edit, 5, 1, 1, 3)
        self.grid.addWidget(self.choose_content_button, 5, 4, 1, 1)
        self.grid.addWidget(self.bg_label, 6, 0, 1, 1)
        self.grid.addWidget(self.bg_edit, 6, 1, 1, 3)
        self.grid.addWidget(self.choose_bg_button, 6, 4, 1, 1)
        self.grid.addWidget(self.font_label, 7, 0, 1, 1)
        self.grid.addWidget(self.font_edit, 7, 1, 1, 3)
        self.grid.addWidget(self.choose_font_button, 7, 4, 1, 1)
        self.grid.addWidget(self.fontcolor_label, 8, 0, 1, 1)
        self.grid.addWidget(self.font_color_combobox, 8, 1, 1, 1)
        self.grid.addWidget(self.generate_button, 8, 3, 1, 1)
        self.grid.addWidget(self.save_button, 8, 4, 1, 1)
        self.setLayout(self.grid)
        # 事件绑定
        self.choose_content_button.clicked.connect(self.openContentFilepath)
        self.choose_bg_button.clicked.connect(self.openBGFilepath)
        self.choose_font_button.clicked.connect(self.openFontFilepath)
        self.generate_button.clicked.connect(self.generate)
        self.save_button.clicked.connect(self.save)
        self.generate()

(2)生成贺卡。

 def generate(self):
        # 检查路径是否存在
        content_path = self.content_edit.text()
        bg_path = self.bg_edit.text()
        font_path = self.font_edit.text()
        font_color = self.font_color_combobox.currentText()
        if (not self.checkFilepath(content_path)) or (not self.checkFilepath(bg_path)) or (not self.checkFilepath(font_path)):
            self.card_image = None
            return False
        # 写贺卡
        contents = open(content_path, encoding='utf-8').read().split('\n')
        font_card = ImageFont.truetype(font_path, self.font_size)
        image = Image.open(bg_path).convert('RGB')
        draw = ImageDraw.Draw(image)
        draw.text((180, 30), contents[0], font=font_card, fill=font_color)
        for idx, content in enumerate(contents[1: -1]):
            draw.text((220, 40+(idx+1)*40), content, font=font_card, fill=font_color)
        draw.text((180, 40+(idx+2)*40+10), contents[-1], font=font_card, fill=font_color)
        # 显示
        fp = io.BytesIO()
        image.save(fp, 'BMP')
        qtimg = QtGui.QImage()
        qtimg.loadFromData(fp.getvalue(), 'BMP')
        qtimg_pixmap = QtGui.QPixmap.fromImage(qtimg)
        self.show_label.setPixmap(qtimg_pixmap)
        self.card_image = image

(3)素材都是准备的多份,背景文字选取路径自己设置。

    def openContentFilepath(self):
        filepath = QFileDialog.getOpenFileName(self, "请选取贺卡内容文件", '.')
        self.content_edit.setText(filepath[0])
 
    def openBGFilepath(self):
        filepath = QFileDialog.getOpenFileName(self, "请选取贺卡背景图片", '.')
        self.bg_edit.setText(filepath[0])
 
    def openFontFilepath(self):
        filepath = QFileDialog.getOpenFileName(self, "请选取字体文件", '.')
        self.font_edit.setText(filepath[0])

(4)生成的贺卡保存下来。

 def save(self):
        filename = QFileDialog.getSaveFileName(self, '保存', './card.jpg', '所有文件(*)')
        if filename[0] != '' and self.card_image:
            self.card_image.save(filename[0])
            QDialog().show()

好啦, 一张完整的贺卡显示就出来啦如下:

​​​​​​​​

​​​​​​​​​

附完整代码:

'''
Function:
    生成中秋祝福贺卡
csdn账号:顾木子吖
'''
import os
import io
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtGui
from PIL import Image, ImageDraw, ImageFont
 
 
'''生成中秋祝福贺卡'''
class newyearCardGUI(QtWidgets.QWidget):
    def __init__(self):
        super(newyearCardGUI, self).__init__()
        self.setFixedSize(600, 500)
        self.setWindowTitle('中秋贺卡生成器-源码基地:#959755565#')
        self.setWindowIcon(QIcon('icon/icon.png'))
        self.grid = QGridLayout()
        # 一些全局变量
        self.card_image = None
        self.font_size = 35
        # 定义组件
        # --Label
        self.content_label = QLabel('内容路径:')
        self.bg_label = QLabel('背景路径:')
        self.font_label = QLabel('字体路径:')
        self.fontcolor_label = QLabel('字体颜色:')
        self.show_label = QLabel()
        self.show_label.setScaledContents(True)
        self.show_label.setMaximumSize(600, 300)
        # --输入框
        self.content_edit = QLineEdit()
        self.content_edit.setText('contents/1.card')
        self.bg_edit = QLineEdit()
        self.bg_edit.setText('bgimages/1.png')
        self.font_edit = QLineEdit()
        self.font_edit.setText('fonts/font.TTF')
        # --按钮
        self.choose_content_button = QPushButton('选择路径')
        self.choose_bg_button = QPushButton('选择路径')
        self.choose_font_button = QPushButton('选择路径')
        self.generate_button = QPushButton('生成贺卡')
        self.save_button = QPushButton('保存贺卡')
        # --下拉框
        self.font_color_combobox = QComboBox()
        for color in ['red', 'white', 'black', 'blue', 'yellow', 'green']:
            self.font_color_combobox.addItem(color)
        # 布局
        self.grid.addWidget(self.show_label, 0, 0, 5, 5)
        self.grid.addWidget(self.content_label, 5, 0, 1, 1)
        self.grid.addWidget(self.content_edit, 5, 1, 1, 3)
        self.grid.addWidget(self.choose_content_button, 5, 4, 1, 1)
        self.grid.addWidget(self.bg_label, 6, 0, 1, 1)
        self.grid.addWidget(self.bg_edit, 6, 1, 1, 3)
        self.grid.addWidget(self.choose_bg_button, 6, 4, 1, 1)
        self.grid.addWidget(self.font_label, 7, 0, 1, 1)
        self.grid.addWidget(self.font_edit, 7, 1, 1, 3)
        self.grid.addWidget(self.choose_font_button, 7, 4, 1, 1)
        self.grid.addWidget(self.fontcolor_label, 8, 0, 1, 1)
        self.grid.addWidget(self.font_color_combobox, 8, 1, 1, 1)
        self.grid.addWidget(self.generate_button, 8, 3, 1, 1)
        self.grid.addWidget(self.save_button, 8, 4, 1, 1)
        self.setLayout(self.grid)
        # 事件绑定
        self.choose_content_button.clicked.connect(self.openContentFilepath)
        self.choose_bg_button.clicked.connect(self.openBGFilepath)
        self.choose_font_button.clicked.connect(self.openFontFilepath)
        self.generate_button.clicked.connect(self.generate)
        self.save_button.clicked.connect(self.save)
        self.generate()
    '''生成贺卡'''
    def generate(self):
        # 检查路径是否存在
        content_path = self.content_edit.text()
        bg_path = self.bg_edit.text()
        font_path = self.font_edit.text()
        font_color = self.font_color_combobox.currentText()
        if (not self.checkFilepath(content_path)) or (not self.checkFilepath(bg_path)) or (not self.checkFilepath(font_path)):
            self.card_image = None
            return False
        # 写贺卡
        contents = open(content_path, encoding='utf-8').read().split('\n')
        font_card = ImageFont.truetype(font_path, self.font_size)
        image = Image.open(bg_path).convert('RGB')
        draw = ImageDraw.Draw(image)
        draw.text((180, 30), contents[0], font=font_card, fill=font_color)
        for idx, content in enumerate(contents[1: -1]):
            draw.text((220, 40+(idx+1)*40), content, font=font_card, fill=font_color)
        draw.text((180, 40+(idx+2)*40+10), contents[-1], font=font_card, fill=font_color)
        # 显示
        fp = io.BytesIO()
        image.save(fp, 'BMP')
        qtimg = QtGui.QImage()
        qtimg.loadFromData(fp.getvalue(), 'BMP')
        qtimg_pixmap = QtGui.QPixmap.fromImage(qtimg)
        self.show_label.setPixmap(qtimg_pixmap)
        self.card_image = image
    '''打开贺卡内容文件'''
    def openContentFilepath(self):
        filepath = QFileDialog.getOpenFileName(self, "请选取贺卡内容文件", '.')
        self.content_edit.setText(filepath[0])
    '''打开贺卡背景图片文件'''
    def openBGFilepath(self):
        filepath = QFileDialog.getOpenFileName(self, "请选取贺卡背景图片", '.')
        self.bg_edit.setText(filepath[0])
    '''打开字体路径'''
    def openFontFilepath(self):
        filepath = QFileDialog.getOpenFileName(self, "请选取字体文件", '.')
        self.font_edit.setText(filepath[0])
    '''保存贺卡'''
    def save(self):
        filename = QFileDialog.getSaveFileName(self, '保存', './card.jpg', '所有文件(*)')
        if filename[0] != '' and self.card_image:
            self.card_image.save(filename[0])
            QDialog().show()
    '''检查文件是否存在'''
    def checkFilepath(self, filepath):
        if not filepath:
            return False
        return os.path.isfile(filepath)
 
 
'''run'''
if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = newyearCardGUI()
    gui.show()
    sys.exit(app.exec_())

总结

好啦!中秋贺卡生成器就制作完成啦,制作不易,中秋快落~

到此这篇关于火遍网络的python中秋节贺卡现在学还赶得上的文章就介绍到这了,更多相关python 贺卡内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文