火遍网络的python中秋节贺卡现在学还赶得上
程序员文章站
2022-03-10 09:53:54
导语转眼,八月十五中秋节即将到来,中秋节以月之圆兆人之团圆,为寄托思念故乡,思念亲人之情,也是我国最具团圆意味的一个传统节日。佳节来临,我的侄女儿跟我打视频,...
导语
转眼,八月十五中秋节即将到来,中秋节以月之圆兆人之团圆,
为寄托思念故乡,思念亲人之情,也是我国最具团圆意味的一个传统节日。
佳节来临,我的侄女儿跟我打视频,说起了他们的中秋老师布置的小作业,每个孩子都会制作专属的中秋贺卡送给家人。
果不其然,这又成了我的一个灵感,跟小侄女儿打完视频就开始了我的贺卡制作之路。
佳节来临,不如制作一份精美的手工贺卡,在中秋之夜送去真挚的祝福!跟着小编来学学吧~
正文
本文是基于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 贺卡内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 送些什么好