廾匸--SH 2022-10-08 20:20 采纳率: 33.3%
浏览 82
已结题

在 '__init__.py' 中找不到引用 'divide'

问题遇到的现象和发生背景

使用cv2处理图形

用代码块功能插入代码,请勿粘贴截图

-- coding: utf-8 --

Form implementation generated from reading ui file 'test2.ui'

Created by: PyQt5 UI code generator 5.15.4

WARNING: Any manual changes made to this file will be lost when pyuic5 is

run again. Do not edit this file unless you know what you are doing.

import sys

import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QFileDialog

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
# 给MainWindow设置图标
MainWindow.setWindowIcon(QIcon('D:\download\xj.ico')) # 路径错误找不到问题所在

    # 给MainWindow设置背景图片
    # palette = QPalette()
    # palette.setBrush(QPalette.Background, QBrush(QPixmap('D:\\python\\RRJ\\pycharmproject\\Practice\\chep2\\bdd'
    #                                                      '\\background3.jpg')))
    # MainWindow.setPalette(palette)

    MainWindow.resize(994, 783)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(10, 0, 961, 721))
    self.label.setStyleSheet("font:28px;\n"
                             "border-style:solid;\n"
                             "border-width:1px;\n"
                             "border-color:rgb(0, 0, 0);\n"
                             "\n"
                             "")
    self.label.setText("")
    self.label.setObjectName("label")
    self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_2.setGeometry(QtCore.QRect(50, 150, 121, 41))
    self.pushButton_2.setStyleSheet("font:22px;")
    self.pushButton_2.setObjectName("pushButton_2")
    self.label_2 = QtWidgets.QLabel(self.centralwidget)
    self.label_2.setGeometry(QtCore.QRect(340, 30, 321, 81))
    self.label_2.setStyleSheet("font: 75 26pt \"Segoe Print\";\n"
                               "color:rgb(255, 85, 0);\n"
                               "text-align:center;\n"
                               "letter-spacing:4pt;")
    self.label_2.setObjectName("label_2")
    self.line = QtWidgets.QFrame(self.centralwidget)
    self.line.setGeometry(QtCore.QRect(10, 120, 961, 20))
    self.line.setFrameShape(QtWidgets.QFrame.HLine)
    self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
    self.line.setObjectName("line")
    self.line_2 = QtWidgets.QFrame(self.centralwidget)
    self.line_2.setGeometry(QtCore.QRect(10, 200, 961, 16))
    self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
    self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
    self.line_2.setObjectName("line_2")
    self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
    self.lineEdit_3.setGeometry(QtCore.QRect(170, 150, 321, 41))
    self.lineEdit_3.setObjectName("lineEdit_3")
    self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_3.setGeometry(QtCore.QRect(530, 150, 151, 41))
    self.pushButton_3.setStyleSheet("font: 22px;")
    self.pushButton_3.setObjectName("pushButton_3")
    self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton_4.setGeometry(QtCore.QRect(730, 150, 151, 41))
    self.pushButton_4.setStyleSheet("font:22px;")
    self.pushButton_4.setObjectName("pushButton_4")
    self.label_3 = QtWidgets.QLabel(self.centralwidget)
    self.label_3.setGeometry(QtCore.QRect(50, 270, 411, 421))
    self.label_3.setStyleSheet("font:28px;\n"
                               "border-style:solid;\n"
                               "border-width:1px;\n"
                               "border-color:rgb(45, 45, 45);\n"
                               "\n"
                               "")
    self.label_3.setText("")
    self.label_3.setObjectName("label_3")
    self.label_4 = QtWidgets.QLabel(self.centralwidget)
    self.label_4.setGeometry(QtCore.QRect(540, 270, 401, 421))
    self.label_4.setStyleSheet("font:28px;\n"
                               "border-style:solid;\n"
                               "border-width:1px;\n"
                               "border-color:rgb(45, 45, 45);\n"
                               "\n"
                               "")
    self.label_4.setText("")
    self.label_4.setObjectName("label_4")
    self.label_5 = QtWidgets.QLabel(self.centralwidget)
    self.label_5.setGeometry(QtCore.QRect(220, 230, 91, 31))
    self.label_5.setStyleSheet("font: 14pt \"Arial\";")
    self.label_5.setObjectName("label_5")
    self.label_6 = QtWidgets.QLabel(self.centralwidget)
    self.label_6.setGeometry(QtCore.QRect(710, 230, 72, 31))
    self.label_6.setStyleSheet("font: 14pt \"Arial\";")
    self.label_6.setObjectName("label_6")
    self.label.raise_()
    self.pushButton_2.raise_()
    self.line.raise_()
    self.line_2.raise_()
    self.label_2.raise_()
    self.lineEdit_3.raise_()
    self.pushButton_3.raise_()
    self.pushButton_4.raise_()
    self.label_3.raise_()
    self.label_4.raise_()
    self.label_5.raise_()
    self.label_6.raise_()
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 994, 26))
    self.menubar.setObjectName("menubar")
    self.menutest2 = QtWidgets.QMenu(self.menubar)
    self.menutest2.setObjectName("menutest2")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)
    self.actiondemo1 = QtWidgets.QAction(MainWindow)
    self.actiondemo1.setObjectName("actiondemo1")
    self.actiondemo2 = QtWidgets.QAction(MainWindow)
    self.actiondemo2.setObjectName("actiondemo2")
    self.menutest2.addAction(self.actiondemo1)
    self.menutest2.addAction(self.actiondemo2)
    self.menubar.addAction(self.menutest2.menuAction())

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

    # 按钮关联函数
    self.pushButton_2.clicked.connect(self.openImage)
    self.pushButton_3.clicked.connect(self.startAction)
    self.pushButton_4.clicked.connect(self.saveImage)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "素描图像"))
    self.pushButton_2.setText(_translate("MainWindow", "选择图片"))
    self.label_2.setText(_translate("MainWindow", "素描图像生成器"))
    self.pushButton_3.setText(_translate("MainWindow", "开始"))
    self.pushButton_4.setText(_translate("MainWindow", "保存"))
    self.label_5.setText(_translate("MainWindow", "原图像"))
    self.label_6.setText(_translate("MainWindow", "素描图"))
    self.menutest2.setTitle(_translate("MainWindow", "test2"))
    self.actiondemo1.setText(_translate("MainWindow", "demo1"))
    self.actiondemo2.setText(_translate("MainWindow", "demo2"))

# 选择本地图片上传
def openImage(self):
    global imgNamepath  # 这里为了方便别的地方引用图片路径,将其设置为全局变量
    # 弹出一个文件选择框,第一个返回值imgName记录选中的文件路径+文件名,第二个返回值imgType记录文件的类型
    # QFileDialog就是系统对话框的那个类第一个参数是上下文,第二个参数是弹框的名字,第三个参数是默认打开的路径,第四个参数是需要的格式
    imgNamepath, imgType = QFileDialog.getOpenFileName(self.centralwidget, "选择图片",
                                                       "D:/",
                                                       "*.jpg;;*.png;;All Files(*)")
    # 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
    img = QtGui.QPixmap(imgNamepath).scaled(self.label_3.width(), self.label_3.height())
    # 在label控件上显示选择的图片
    self.label_3.setPixmap(img)
    # 显示所选图片的路径
    self.lineEdit_3.setText(imgNamepath)

# 保存图片到本地(第二种方式:首先提取相对应Qlabel中的图片,然后打开一个保存文件的弹出框,最后保存图片到选中的路径)
def saveImage(self):
    # 提取Qlabel中的图片
    img = self.label_4.pixmap().toImage()
    fpath, ftype = QFileDialog.getSaveFileName(self.centralwidget, "保存图片", "d:\\", "*.jpg;;*.png;;All Files(*)")
    img.save(fpath)

# 生成素描图
def startAction(self):
    img = cv2.imread(imgNamepath)
    img = cv2.resize(img, dsize=(768, 1080))
    # 图像转灰度图像
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 灰度图像到反转灰度图像
    inverted_gray_image = 255 - gray_image
    # 模糊倒置灰度图像
    blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19, 19), 0)
    # 反转模糊图像
    inverted_blurred_image = 255 - blurred_inverted_gray_image
    # 准备照片素描
    sketck = cv2.divide(gray_image, inverted_blurred_image, scale=256.0)

    path = "D:\\"
    # 因为不知道怎么将<class 'numpy.ndarray'>转换为<class 'PyQt5.QtGui.QPixmap'>类型,因此采用暂存再读出的方式
    cv2.imwrite(path + 'ZC.jpg', sketck)
    # pyqt5从路径读取图片
    imgShow = QPixmap(path + 'ZC.jpg')
    self.label_4.setScaledContents(True)
    self.label_4.setPixmap(imgShow)

if name == "main":
app = QtWidgets.QApplication(sys.argv) # 创建一个QApplication,也就是你要开发的软件app
MainWindow = QtWidgets.QMainWindow() # 创建一个QMainWindow,用来装载你需要的各种组件、控件
ui = Ui_MainWindow() # ui是Ui_MainWindow()类的实例化对象
ui.setupUi(MainWindow) # 执行类中的setupUi方法,方法的参数是第二步中创建的QMainWindow
MainWindow.show() # 执行QMainWindow的show()方法,显示这个QMainWindow
sys.exit(app.exec_())

运行结果及报错内容

在 'init.py' 中找不到引用 'resize'
在 'init.py' 中找不到引用 'cvtColor'

我的解答思路和尝试过的方法

是因为CV2库不匹配的原因吗

我想要达到的结果

可以将图片素描化

  • 写回答

1条回答 默认 最新

  • Ko-walski 2022-10-08 20:49
    关注

    已解决

    img

    下次不要在搞这么长的代码了 很费劲

    问题一:图标直接用png 比如这样的你可以下载直接使用

    img


    player.png

    问题二:不能素描是因为你的图片路径有中文只要不是中文就OK 其次保存路径改成 将/ 改成\
    完整代码
    我没有D盘 所以把你里面的改成F盘了
    完整代码如下:

    import sys
    
    import cv2
    from PyQt5 import QtCore, QtGui, QtWidgets
    from PyQt5.QtGui import QIcon
    from PyQt5.QtGui import QPixmap
    from PyQt5.QtWidgets import QFileDialog
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.setWindowIcon(QIcon('F:\PythonDemo\player.png'))  # 路径错误找不到问题所在
            # 给MainWindow设置背景图片
            # palette = QPalette()
            # palette.setBrush(QPalette.Background, QBrush(QPixmap('F:\\python\\RRJ\\pycharmproject\\Practice\\chep2\\bdd'
            #                                                      '\\background3.jpg')))
            # MainWindow.setPalette(palette)
    
            MainWindow.resize(994, 783)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.label = QtWidgets.QLabel(self.centralwidget)
            self.label.setGeometry(QtCore.QRect(10, 0, 961, 721))
            self.label.setStyleSheet("font:28px;\n"
                                     "border-style:solid;\n"
                                     "border-width:1px;\n"
                                     "border-color:rgb(0, 0, 0);\n"
                                     "\n"
                                     "")
            self.label.setText("")
            self.label.setObjectName("label")
            self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton_2.setGeometry(QtCore.QRect(50, 150, 121, 41))
            self.pushButton_2.setStyleSheet("font:22px;")
            self.pushButton_2.setObjectName("pushButton_2")
            self.label_2 = QtWidgets.QLabel(self.centralwidget)
            self.label_2.setGeometry(QtCore.QRect(340, 30, 321, 81))
            self.label_2.setStyleSheet("font: 75 26pt \"Segoe Print\";\n"
                                       "color:rgb(255, 85, 0);\n"
                                       "text-align:center;\n"
                                       "letter-spacing:4pt;")
            self.label_2.setObjectName("label_2")
            self.line = QtWidgets.QFrame(self.centralwidget)
            self.line.setGeometry(QtCore.QRect(10, 120, 961, 20))
            self.line.setFrameShape(QtWidgets.QFrame.HLine)
            self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
            self.line.setObjectName("line")
            self.line_2 = QtWidgets.QFrame(self.centralwidget)
            self.line_2.setGeometry(QtCore.QRect(10, 200, 961, 16))
            self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
            self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
            self.line_2.setObjectName("line_2")
            self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit_3.setGeometry(QtCore.QRect(170, 150, 321, 41))
            self.lineEdit_3.setObjectName("lineEdit_3")
            self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton_3.setGeometry(QtCore.QRect(530, 150, 151, 41))
            self.pushButton_3.setStyleSheet("font: 22px;")
            self.pushButton_3.setObjectName("pushButton_3")
            self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
            self.pushButton_4.setGeometry(QtCore.QRect(730, 150, 151, 41))
            self.pushButton_4.setStyleSheet("font:22px;")
            self.pushButton_4.setObjectName("pushButton_4")
            self.label_3 = QtWidgets.QLabel(self.centralwidget)
            self.label_3.setGeometry(QtCore.QRect(50, 270, 411, 421))
            self.label_3.setStyleSheet("font:28px;\n"
                                       "border-style:solid;\n"
                                       "border-width:1px;\n"
                                       "border-color:rgb(45, 45, 45);\n"
                                       "\n"
                                       "")
            self.label_3.setText("")
            self.label_3.setObjectName("label_3")
            self.label_4 = QtWidgets.QLabel(self.centralwidget)
            self.label_4.setGeometry(QtCore.QRect(540, 270, 401, 421))
            self.label_4.setStyleSheet("font:28px;\n"
                                       "border-style:solid;\n"
                                       "border-width:1px;\n"
                                       "border-color:rgb(45, 45, 45);\n"
                                       "\n"
                                       "")
            self.label_4.setText("")
            self.label_4.setObjectName("label_4")
            self.label_5 = QtWidgets.QLabel(self.centralwidget)
            self.label_5.setGeometry(QtCore.QRect(220, 230, 91, 31))
            self.label_5.setStyleSheet("font: 14pt \"Arial\";")
            self.label_5.setObjectName("label_5")
            self.label_6 = QtWidgets.QLabel(self.centralwidget)
            self.label_6.setGeometry(QtCore.QRect(710, 230, 72, 31))
            self.label_6.setStyleSheet("font: 14pt \"Arial\";")
            self.label_6.setObjectName("label_6")
            self.label.raise_()
            self.pushButton_2.raise_()
            self.line.raise_()
            self.line_2.raise_()
            self.label_2.raise_()
            self.lineEdit_3.raise_()
            self.pushButton_3.raise_()
            self.pushButton_4.raise_()
            self.label_3.raise_()
            self.label_4.raise_()
            self.label_5.raise_()
            self.label_6.raise_()
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 994, 26))
            self.menubar.setObjectName("menubar")
            self.menutest2 = QtWidgets.QMenu(self.menubar)
            self.menutest2.setObjectName("menutest2")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.actiondemo1 = QtWidgets.QAction(MainWindow)
            self.actiondemo1.setObjectName("actiondemo1")
            self.actiondemo2 = QtWidgets.QAction(MainWindow)
            self.actiondemo2.setObjectName("actiondemo2")
            self.menutest2.addAction(self.actiondemo1)
            self.menutest2.addAction(self.actiondemo2)
            self.menubar.addAction(self.menutest2.menuAction())
    
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
            # 按钮关联函数
            self.pushButton_2.clicked.connect(self.openImage)
            self.pushButton_3.clicked.connect(self.startAction)
            self.pushButton_4.clicked.connect(self.saveImage)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "素描图像"))
            self.pushButton_2.setText(_translate("MainWindow", "选择图片"))
            self.label_2.setText(_translate("MainWindow", "素描图像生成器"))
            self.pushButton_3.setText(_translate("MainWindow", "开始"))
            self.pushButton_4.setText(_translate("MainWindow", "保存"))
            self.label_5.setText(_translate("MainWindow", "原图像"))
            self.label_6.setText(_translate("MainWindow", "素描图"))
            self.menutest2.setTitle(_translate("MainWindow", "test2"))
            self.actiondemo1.setText(_translate("MainWindow", "demo1"))
            self.actiondemo2.setText(_translate("MainWindow", "demo2"))
    
        # 选择本地图片上传
        def openImage(self):
            global imgNamepath  # 这里为了方便别的地方引用图片路径,将其设置为全局变量
            # 弹出一个文件选择框,第一个返回值imgName记录选中的文件路径+文件名,第二个返回值imgType记录文件的类型
            # QFileDialog就是系统对话框的那个类第一个参数是上下文,第二个参数是弹框的名字,第三个参数是默认打开的路径,第四个参数是需要的格式
            imgNamepath, imgType = QFileDialog.getOpenFileName(self.centralwidget, "选择图片",
                                                               "F:\\",
                                                               "*.jpg;;*.png;;All Files(*)")
            # 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
            img = QtGui.QPixmap(imgNamepath).scaled(self.label_3.width(), self.label_3.height())
            # 在label控件上显示选择的图片
            self.label_3.setPixmap(img)
            # 显示所选图片的路径
            self.lineEdit_3.setText(imgNamepath)
    
        # 保存图片到本地(第二种方式:首先提取相对应Qlabel中的图片,然后打开一个保存文件的弹出框,最后保存图片到选中的路径)
        def saveImage(self):
            # 提取Qlabel中的图片
            img = self.label_4.pixmap().toImage()
            fpath, ftype = QFileDialog.getSaveFileName(self.centralwidget, "保存图片", "F:\\", "*.jpg;;*.png;;All Files(*)")
            img.save(fpath)
    
        # 生成素描图
        def startAction(self):
            img = cv2.imread(imgNamepath)
            img = cv2.resize(img, dsize=(768, 1080))
            # 图像转灰度图像
            gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # 灰度图像到反转灰度图像
            inverted_gray_image = 255 - gray_image
            # 模糊倒置灰度图像
            blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19, 19), 0)
            # 反转模糊图像
            inverted_blurred_image = 255 - blurred_inverted_gray_image
            # 准备照片素描
            sketck = cv2.divide(gray_image, inverted_blurred_image, scale=256.0)
    
            path = "F:\\"
            # 因为不知道怎么将<class 'numpy.ndarray'>转换为<class 'PyQt5.QtGui.QPixmap'>类型,因此采用暂存再读出的方式
            cv2.imwrite(path + 'ZC.jpg', sketck)
            # pyqt5从路径读取图片
            imgShow = QPixmap(path + 'ZC.jpg')
            self.label_4.setScaledContents(True)
            self.label_4.setPixmap(imgShow)
    
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)  # 创建一个QApplication,也就是你要开发的软件app
        MainWindow = QtWidgets.QMainWindow()  # 创建一个QMainWindow,用来装载你需要的各种组件、控件
        ui = Ui_MainWindow()  # ui是Ui_MainWindow()类的实例化对象
        ui.setupUi(MainWindow)  # 执行类中的setupUi方法,方法的参数是第二步中创建的QMainWindow
        MainWindow.show()  # 执行QMainWindow的show()方法,显示这个QMainWindow
        sys.exit(app.exec_())
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月18日
  • 已采纳回答 10月10日
  • 创建了问题 10月8日

悬赏问题

  • ¥15 研发一个商城APP制作底部tabbar的时候运行不了,如何解决?
  • ¥15 miniconda安装不了
  • ¥20 python代码编写
  • ¥20 使用MPI广播数据遇到阻塞
  • ¥15 TinyMCE如何去掉自动弹出的“链接…”工具?
  • ¥15 微信支付转账凭证,如何解决
  • ¥15 在win10下使用指纹登录时,界面上的文字最后一个字产生换行现象
  • ¥20 使用AT89C51微控制器和MAX7219驱动器来实现0到99秒的秒表计数,有开始和暂停以及复位功能,下面有仿真图,请根据仿真图来设计c语言程序
  • ¥15 51单片机 双路ad同步采样
  • ¥15 使用xdocreport 生成word