Python 官方文档:入门教程 => 点击学习
目录一、写在前面二、先看看效果三、代码展示1.需要用到的模块2.窗口名字/图标/大小/布局3.必要组件的定义4.输入框5.保存/生成按钮6.下拉框7.组件布局8.事件绑定9.生成签名
很多兄弟觉得自己字写的不好什么的,没事,咱们直接用python来生成,对着多练练就能写出一手好看的艺术签名了,小姐姐看了直呼好看~
可以选择字体、颜色,这里我们用默认的看看的效果。
啊这,一笔学不来,算了,咱们敲代码吧…
import requests
from PIL import Image
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtGui
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_())
我从来不吝啬源码,都直接放出来,不像那些,哈哈哈,懂的都懂。
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_())
以上就是利用Python轻松生成艺术签名的详细内容,更多关于Python艺术签名的资料请关注编程网其它相关文章!
--结束END--
本文标题: 利用Python轻松生成艺术签名
本文链接: https://lsjlt.com/news/162360.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0