from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import qrcode
import cv2
import numpy as np
from widgets import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("二维码生成器")
self.setMinimumSize(600, 800)
self.widget = QWidget()
self.setCentralWidget(self.widget)
self.layout = QGridLayout()
self.widget.setLayout(self.layout)
self.bg_selector = FileSelector("背景图片")
self.layout.addWidget(self.bg_selector, 0, 0, 1, 1)
self.logo_selector = FileSelector("Logo")
self.layout.addWidget(self.logo_selector, 0, 1, 1, 1)
self.text_edit = QRCodeTextEdit()
self.layout.addWidget(self.text_edit, 1, 0, 1, 2)
self.bar = QRCodeGeneratorBar()
self.layout.addWidget(self.bar, 2, 0, 1, 2)
self.preview = QLabel()
self.preview.setFixedHeight(350)
self.preview.setScaledContents(True)
self.preview.setStyleSheet("border: 1px solid black")
self.layout.addWidget(self.preview, 3, 0, 1, 2)
# 默认输入
self.bg_selector.text.setText("./imgs/bg.png")
self.bg_selector.show_img()
self.logo_selector.text.setText("./imgs/logo.png")
self.logo_selector.show_img()
self.text_edit.text.setText("https://blog.csdn.net/")
self.generate_qrcode()
# 事件
self.bar.button.clicked.connect(self.generate_qrcode)
def generate_qrcode(self):
'''
根据设置生成二维码
'''
# 背景图片
bg_path = self.bg_selector.text.text() # 背景图片路径
bg_img = cv2.imread(bg_path) # 读取图片
bg_min_size = min(bg_img.shape[:2]) # 获取最小边长
bg_center = (bg_img.shape[0]//2, bg_img.shape[1]//2) # 获取图片中心坐标
# 二维码图片
text = self.text_edit.text.toPlainText() # 二维码文本
qrcode_ratio = float(self.bar.line_edit1.text()) # 二维码与背景图片比例
qrcode_size = round(bg_min_size*qrcode_ratio) # 计算二维码边长
qrcode_img = qrcode.make(text) # 生成二维码
qrcode_img = np.array(qrcode_img, dtype=np.uint8) *255 # 转换为numpy类型
qrcode_img = cv2.merge([qrcode_img, qrcode_img, qrcode_img]) # 转换为BGR格式
qrcode_img = cv2.resize(qrcode_img, (qrcode_size, qrcode_size)) # 调整二维码大小
# Logo图片
logo_path = self.logo_selector.text.text() # Logo图片路径
logo_ratio = float(self.bar.line_edit2.text()) # Logo与二维码比例
logo_size = round(qrcode_size*logo_ratio) # 计算Logo边长
logo_img = cv2.imread(logo_path) # 读取图片
logo_img = cv2.resize(logo_img, (logo_size, logo_size)) # 调整Logo大小
# 合成图片
result = bg_img
result[bg_center[0]-qrcode_size//2:bg_center[0]-qrcode_size//2+qrcode_size, bg_center[1]-qrcode_size//2:bg_center[1]-qrcode_size//2+qrcode_size] = qrcode_img
result[bg_center[0]-logo_size//2:bg_center[0]-logo_size//2+logo_size, bg_center[1]-logo_size//2:bg_center[1]-logo_size//2+logo_size] = logo_img
cv2.imwrite("./result.png", result) # 保存图片
# 显示图片
result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
result = QImage(result, result.shape[1], result.shape[0], result.shape[1] * 3, QImage.Format_RGB888)
self.preview.setFixedWidth(round(result.width()/result.height()*self.preview.height()))
self.preview.setPixmap(QPixmap(result))
if __name__ == "__main__":
app = QApplication([])
win = MainWindow()
win.show()
app.exec_()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
# Python生成二维码图片 制作带logo和背景的二维码 可视化界面 1. 使用 qrcode 库将文本或链接生成二维码图片; 2. 使用 opencv-python 和 numpy 库进行图像操作,将背景、logo和二维码合成一张图片; 3. PyQt5 GUI界面,可以修改背景、logo图片、二维码文本,调整叠加比例,点击按钮生成图片。窗口中可以预览导入的背景、logo图片和生成的图片效果。
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![spec](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 5 条
- 1
![avatar](https://profile-avatar.csdnimg.cn/8b66c0ad159a4f3eb249f266a6f1d6a9_baidu_36499789.jpg!1)
两只程序猿
- 粉丝: 373
- 资源: 159
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
- 3
- 4
前往页