Python艺术签名
嗨学编程 人气:2一、写在前面
很多兄弟觉得自己字写的不好什么的,没事,咱们直接用python来生成,对着多练练就能写出一手好看的艺术签名了,小姐姐看了直呼好看~
二、先看看效果
可以选择字体、颜色,这里我们用默认的看看的效果。
啊这,一笔学不来,算了,咱们敲代码吧…
三、代码展示
1.需要用到的模块
import requests from PIL import Image from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5 import QtWidgets, QtGui
2.窗口名字/图标/大小/布局
self.setFixedSize(600, 500) self.setWindowTitle('嗨学编程—艺术签名生成器') self.setWindowIcon(QIcon('resource/icon/icon.jpg')) self.grid = QGridLayout()
3.必要组件的定义
self.show_label = QLabel() self.show_label.setScaledContents(True) self.show_label.setMaximumSize(600, 400) self.show_image = Image.open('resource/image/ori.jpg').convert('RGB') self.updateimage() self.show_image_ext = 'jpg' self.name_label = QLabel('输入您的姓名:') self.font_label = QLabel('艺术签名字体:') self.color_label = QLabel('艺术签名颜色:')
4.输入框
self.name_edit = QLineEdit() self.name_edit.setText('签名生成器')
5.保存/生成按钮
self.generate_button = QPushButton('生成艺术签名') self.save_button = QPushButton('保存艺术签名')
6.下拉框
self.font_combobox = QComboBox() for item in ['一笔艺术签', '连笔商务签', '一笔商务签', '真人手写', '暴躁字']: self.font_combobox.addItem(item) self.color_combobox = QComboBox() for item in ['Black', 'Blue', 'Red', 'Green', 'Yellow', 'Pink', 'DeepSkyBlue', 'Cyan', 'Orange', 'Seashell']: self.color_combobox.addItem(item)
7.组件布局
self.grid.addWidget(self.show_label, 0, 0, 5, 5) self.grid.addWidget(self.name_label, 5, 0, 1, 1) self.grid.addWidget(self.name_edit, 5, 1, 1, 4) self.grid.addWidget(self.font_label, 6, 0, 1, 1) self.grid.addWidget(self.font_combobox, 6, 1, 1, 4) self.grid.addWidget(self.color_label, 7, 0, 1, 1) self.grid.addWidget(self.color_combobox, 7, 1, 1, 4) self.grid.addWidget(self.generate_button, 8, 3, 1, 1) self.grid.addWidget(self.save_button, 8, 4, 1, 1) self.setLayout(self.grid)
8.事件绑定
self.generate_button.clicked.connect(self.generate) self.save_button.clicked.connect(self.save)
9.生成签名
def generate(self): font2ids_dict = { '一笔艺术签': ['901', '15'], '连笔商务签': ['904', '15'], '一笔商务签': ['905', '14'], '真人手写': ['343', '14'], '卡通趣圆字': ['397', '14'], '暴躁字': ['380', '14'] } color2ids_dict = { 'Black': ['#000000', '#FFFFFF'], 'Blue': ['#0000FF', '#FFFFFF'], 'Red': ['#FF0000', '#FFFFFF'], 'Green': ['#00FF00', '#FFFFFF'], 'Yellow': ['#FFFF00', '#FFFFFF'], 'Pink': ['#FFC0CB', '#FFFFFF'], 'DeepSkyBlue': ['#00BFFF', '#FFFFFF'], 'Cyan': ['#00FFFF', '#FFFFFF'], 'Orange': ['#FFA500', '#FFFFFF'], 'Seashell': ['#FFF5EE', '#FFFFFF'] } url = 'http://www.jiqie.com/a/re14.php' headers = { 'Referer': 'http://www.jiqie.com/a/14.htm', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36', 'Host': 'www.jiqie.com', 'Origin': 'http://www.jiqie.com' } ids_0 = font2ids_dict[self.font_combobox.currentText()] ids_1 = color2ids_dict[self.color_combobox.currentText()] data = { 'id': self.name_edit.text(), 'zhenbi': '20191123', 'id1': ids_0[0], 'id2': ids_0[1], 'id3': ids_1[0], 'id5': ids_1[1] } res = requests.post(url, headers=headers, data=data) image_url = re.findall(r'src="(.*?)"', res.text)[0] self.show_image_ext = image_url.split('.')[-1].split('?')[0] res = requests.get(image_url) fp = open('tmp.%s' % self.show_image_ext, 'wb') fp.write(res.content) fp.close() self.show_image = Image.open('tmp.%s' % self.show_image_ext).convert('RGB') self.updateimage() os.remove('tmp.%s' % self.show_image_ext)
10.更新界面上的图片
def updateimage(self): if self.show_image is None: return fp = io.BytesIO() self.show_image.save(fp, 'JPEG') qtimage = QtGui.QImage() qtimage.loadFromData(fp.getvalue(), 'JPEG') qtimage_pixmap = QtGui.QPixmap.fromImage(qtimage) self.show_label.setPixmap(qtimage_pixmap)
11.签名保存
def save(self): if self.show_image is None: return filename = QFileDialog.getSaveFileName(self, '保存', './sign.%s' % self.show_image_ext, '所有文件(*)') if filename[0]: self.show_image.save(filename[0]) QDialog().show()
12.run
if __name__ == '__main__': app = QApplication(sys.argv) gui = ArtSignGenerator() gui.show() sys.exit(app.exec_())
四、全部代码
我从来不吝啬源码,都直接放出来,不像那些,哈哈哈,懂的都懂。
import os import re import io import sys import requests from PIL import Image from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5 import QtWidgets, QtGui class ArtSignGenerator(QWidget): def __init__(self, parent=None): super(ArtSignGenerator, self).__init__(parent) self.setFixedSize(600, 500) self.setWindowTitle('嗨学编程—艺术签名生成器') self.setWindowIcon(QIcon('resource/icon/icon.jpg')) self.grid = QGridLayout() self.show_label = QLabel() self.show_label.setScaledContents(True) self.show_label.setMaximumSize(600, 400) self.show_image = Image.open('resource/image/ori.jpg').convert('RGB') self.updateimage() self.show_image_ext = 'jpg' self.name_label = QLabel('输入您的姓名:') self.font_label = QLabel('艺术签名字体:') self.color_label = QLabel('艺术签名颜色:') self.name_edit = QLineEdit() self.name_edit.setText('签名生成器') self.generate_button = QPushButton('生成艺术签名') self.save_button = QPushButton('保存艺术签名') self.font_combobox = QComboBox() for item in ['一笔艺术签', '连笔商务签', '一笔商务签', '真人手写', '暴躁字']: self.font_combobox.addItem(item) self.color_combobox = QComboBox() for item in ['Black', 'Blue', 'Red', 'Green', 'Yellow', 'Pink', 'DeepSkyBlue', 'Cyan', 'Orange', 'Seashell']: self.color_combobox.addItem(item) self.grid.addWidget(self.show_label, 0, 0, 5, 5) self.grid.addWidget(self.name_label, 5, 0, 1, 1) self.grid.addWidget(self.name_edit, 5, 1, 1, 4) self.grid.addWidget(self.font_label, 6, 0, 1, 1) self.grid.addWidget(self.font_combobox, 6, 1, 1, 4) self.grid.addWidget(self.color_label, 7, 0, 1, 1) self.grid.addWidget(self.color_combobox, 7, 1, 1, 4) 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.generate_button.clicked.connect(self.generate) self.save_button.clicked.connect(self.save) def generate(self): font2ids_dict = { '一笔艺术签': ['901', '15'], '连笔商务签': ['904', '15'], '一笔商务签': ['905', '14'], '真人手写': ['343', '14'], '卡通趣圆字': ['397', '14'], '暴躁字': ['380', '14'] } color2ids_dict = { 'Black': ['#000000', '#FFFFFF'], 'Blue': ['#0000FF', '#FFFFFF'], 'Red': ['#FF0000', '#FFFFFF'], 'Green': ['#00FF00', '#FFFFFF'], 'Yellow': ['#FFFF00', '#FFFFFF'], 'Pink': ['#FFC0CB', '#FFFFFF'], 'DeepSkyBlue': ['#00BFFF', '#FFFFFF'], 'Cyan': ['#00FFFF', '#FFFFFF'], 'Orange': ['#FFA500', '#FFFFFF'], 'Seashell': ['#FFF5EE', '#FFFFFF'] } url = 'http://www.jiqie.com/a/re14.php' headers = { 'Referer': 'http://www.jiqie.com/a/14.htm', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36', 'Host': 'www.jiqie.com', 'Origin': 'http://www.jiqie.com' } ids_0 = font2ids_dict[self.font_combobox.currentText()] ids_1 = color2ids_dict[self.color_combobox.currentText()] data = { 'id': self.name_edit.text(), 'zhenbi': '20191123', 'id1': ids_0[0], 'id2': ids_0[1], 'id3': ids_1[0], 'id5': ids_1[1] } res = requests.post(url, headers=headers, data=data) image_url = re.findall(r'src="(.*?)"', res.text)[0] self.show_image_ext = image_url.split('.')[-1].split('?')[0] res = requests.get(image_url) fp = open('tmp.%s' % self.show_image_ext, 'wb') fp.write(res.content) fp.close() self.show_image = Image.open('tmp.%s' % self.show_image_ext).convert('RGB') self.updateimage() os.remove('tmp.%s' % self.show_image_ext) def updateimage(self): if self.show_image is None: return fp = io.BytesIO() self.show_image.save(fp, 'JPEG') qtimage = QtGui.QImage() qtimage.loadFromData(fp.getvalue(), 'JPEG') qtimage_pixmap = QtGui.QPixmap.fromImage(qtimage) self.show_label.setPixmap(qtimage_pixmap) def save(self): if self.show_image is None: return filename = QFileDialog.getSaveFileName(self, '保存', './sign.%s' % self.show_image_ext, '所有文件(*)') if filename[0]: self.show_image.save(filename[0]) QDialog().show() if __name__ == '__main__': app = QApplication(sys.argv) gui = ArtSignGenerator() gui.show() sys.exit(app.exec_())
加载全部内容