AttributeError:模块“PIL.ImageQt”没有属性“ImageQt”

问题描述 投票:0回答:2
import sys
import pickle as p
from PyQt4 import QtCore, QtGui, uic
import numpy as n
import PIL.ImageQt as PQ
from PIL import Image
import cv2
import os

self.currentFrame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
self.currentSeg=Image.fromarray(thresh).convert('RGB')
self.currentSeg = PQ.ImageQt(self.currentSeg)
height,width=self.currentFrame.shape[:2]

我得到上面的错误,imageQt 没有属性。我试过改变

from PIL.ImageQt import ImageQt
但它给了
ImportError: cannot import name 'ImageQt' from 'PIL.ImageQt'

python image pyqt4 python-3.7 opencv-python
2个回答
0
投票

看来你确实在搞进口。详细看来

import PIL.ImageQt as PQ

冲突
self.currentSeg = PQ.ImageQt(self.currentSeg)

如果

ImageQt
是你想要访问的对象,那么我建议改变行如下:

import PIL.ImageQt as PQ
self.currentSeg = PQ(self.currentSeg)

0
投票

请注意,错误

ImportError: cannot import name 'ImageQt' from 'PIL.ImageQt'
可能具有误导性。

如果您查看 ImageQt.py 源代码,您会发现

ImageQt
类仅在它成功检测到 Qt 库之一(pyside2/6、PyQt5/6)时才被定义。

所以如果你的 Qt 安装有问题,你可能会得到这个错误。您可能想重新安装 PySide6 或您一直在使用的任何东西。

您还可以使用像

strace
这样的工具并监视 Python 进程(如果您使用的是 Linux)以准确查看它在寻找什么。

strace python -c 'from PIL.ImageQt import ImageQt'

我使用这种技术来追踪为什么这个导入在 Docker 下失败但在常规桌面上正常工作。

为了 (Ubuntu) Docker 容器正确支持 import 语句,我还必须安装这些缺失的库:

apt install -y libegl1 libgl1 libxkbcommon0
© www.soinside.com 2019 - 2024. All rights reserved.