一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

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

服務器之家 - 腳本之家 - Python - 火遍網絡的python中秋節賀卡現在學還趕得上

火遍網絡的python中秋節賀卡現在學還趕得上

2022-01-08 00:15顧木子吖 Python

中秋將至,我用python編寫了個火遍網絡的中秋節賀卡,現在學起來還不晚,文中給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

導語

火遍網絡的python中秋節賀卡現在學還趕得上?

轉眼,八月十五中秋節即將到來,中秋節以月之圓兆人之團圓,

為寄托思念故鄉,思念親人之情,也是我國最具團圓意味的一個傳統節日。

?火遍網絡的python中秋節賀卡現在學還趕得上

?佳節來臨,我的侄女兒跟我打視頻,說起了他們的中秋老師布置的小作業,每個孩子都會制作專屬的中秋賀卡送給家人。火遍網絡的python中秋節賀卡現在學還趕得上??

?火遍網絡的python中秋節賀卡現在學還趕得上

果不其然,這又成了我的一個靈感,跟小侄女兒打完視頻就開始了我的賀卡制作之路。

佳節來臨,不如制作一份精美的手工賀卡,在中秋之夜送去真摯的祝福!跟著小編來學學吧~

正文

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

(1)首先準備好相應的素材、如文字字體、賀卡背景等,大家可以隨機制作。

?火遍網絡的python中秋節賀卡現在學還趕得上

?火遍網絡的python中秋節賀卡現在學還趕得上

(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("
")
        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()

好啦, 一張完整的賀卡顯示就出來啦如下:

????????火遍網絡的python中秋節賀卡現在學還趕得上

?????????火遍網絡的python中秋節賀卡現在學還趕得上

附完整代碼:

"""
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("
")
        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中秋節賀卡現在學還趕得上的文章就介紹到這了,更多相關python 賀卡內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/weixin_55822277/article/details/120220287

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 全黄h全肉细节修仙玄幻文 全彩调教侵犯h本子全彩妖气he | waswaswas免费| 国产日韩精品一区二区在线观看 | 国产精品久久久久一区二区三区 | 日韩手机在线观看 | 国产全部理论片线观看 | 欧美一区不卡二区不卡三区 | xx18-19xxxxhd| 国内精品国语自产拍在线观看55 | 国产午夜精品一区二区三区 | 国内精品久久久久久久 | 调教校花浣肠开菊 | 国产传媒在线播放 | 日韩国产欧美成人一区二区影院 | 赤坂丽女医bd无删减在线观看 | 99视频在线免费 | 天堂中文在线免费观看 | 欧美日韩精品一区二区三区视频播放 | 天天碰夜夜操 | 午夜一级免费视频 | 国产亚洲女在线精品 | 国产精品久久毛片蜜月 | chinese男同志videos | 喷奶水榨乳ova动漫无修 | 五月激激激综合网色播免费 | 亚洲第一区二区快射影院 | 色网在线视频 | 国产性视频 | 国产高清免费午夜在线视频 | 欧美久久影院 | 国产思妍小仙女一二区 | 欧美一级在线视频 | 国产精品成人免费福利 | 男女刺激高清视频在线观看 | 91在线亚洲精品一区 | 日本免费全黄一级裸片视频 | 超碰av| 欧美日韩视频在线第一区二区三区 | 性伴交换多p | 色老太bbbbb| 国产成人精品综合在线观看 |