【问题标题】:AttributeError: module 'PIL' has no attribute ImageAttributeError:模块“PIL”没有属性图像
【发布时间】:2021-02-17 23:25:18
【问题描述】:
import cv2
import numpy as np
from google.colab.patches import cv2_imshow

img = cv2.imread('test.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

img_gaussian = cv2.GaussianBlur(gray, (3, 3), 0)
img_sobelx = cv2.Sobel(img_gaussian, cv2.CV_8U, 1, 0, ksize=5)
img_sobely = cv2.Sobel(img_gaussian, cv2.CV_8U, 0, 1, ksize=5)
img_sobel = img_sobelx + img_sobely

kernelx = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
kernely = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])

img_prewittx = cv2.filter2D(img_gaussian, -1, kernelx)
img_prewitty = cv2.filter2D(img_gaussian, -1, kernely)
img_prewitt = img_prewittx + img_prewitty

cv2_imshow(img)
cv2_imshow(np.hstack((img_sobelx, img_sobely, img_sobel)))
cv2_imshow(np.hstack((img_prewittx, img_prewitty, img_prewitt)))

我不知道为什么会出现此错误?我要做的只是图像平滑/锐化。 我也收到此警告(“IPython.utils.traitlets 已移至顶级 traitlets 包。”)

这是错误信息

/home/saurabh/anaconda3/lib/python3.7/site-packages/IPython/utils/traitlets.py:5:
UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package. warn("IPython.utils.traitlets has moved to a top-level traitlets package.") 
Traceback (most recent call last): File "exp10.py", line 18, in <module> cv2_imshow(img) File "/home/saurabh/anaconda3/lib/python3.7/site-packages/google/colab/patches/__init__.py", line 29, in cv2_imshow display.display(PIL.Image.fromarray(a)) AttributeError: module 'PIL' has no attribute 'Image' –

【问题讨论】:

  • 你能分享一下确切的错误信息吗?
  • /home/saurabh/anaconda3/lib/python3.7/site-packages/IPython/utils/traitlets.py:5:用户警告:IPython.utils.traitlets 已移至顶级 traitlets包裹。 warn("IPython.utils.traitlets 已移至顶级 traitlets 包。") Traceback(最近一次调用最后一次):文件 "exp10.py",第 18 行,在 cv2_imshow(img) 文件 "/home /saurabh/anaconda3/lib/python3.7/site-packages/google/colab/patches/__init__.py”,第 29 行,在 cv2_imshow display.display(PIL.Image.fromarray(a)) AttributeError: module 'PIL'没有属性“图像”

标签: python opencv python-imaging-library


【解决方案1】:

尝试将此添加到文件的顶部

from PIL import Image

【讨论】:

    猜你喜欢
    • 2020-11-29
    • 1970-01-01
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2017-01-02
    • 2020-01-01
    • 2022-07-16
    • 2019-07-20
    相关资源
    最近更新 更多