Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module 'PIL' has no attribute Image

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)))

I don't know why I'm getting this error? All I'mm trying to do is Image smoothing/sharpening. Also I get this warn("IPython.utils.traitlets has moved to a top-level traitlets package.")

This is the error message

/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' –
like image 787
Recommence Avatar asked Nov 06 '25 12:11

Recommence


1 Answers

Try adding this to the top of your file

from PIL import Image
like image 108
Oliver Hnat Avatar answered Nov 09 '25 05:11

Oliver Hnat