ImportError: cannot import name 'imshow' - python

unfortunalty I have not found a solution for this problem.
from imgutils import imshow
import cv2
img3 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
plt.figure(figsize=(20,10))
plt.subplot(1,2,1); imshow(img)
plt.subplot(1,2,2); imshow(img3)
I get the following traceback:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-31-8006396b3a04> in <module>
----> 1 from imgutils import imshow
2
3 img3 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
4
5 plt.figure(figsize=(20,10))
ImportError: cannot import name 'imshow'
I am working on a Mac OS X 10.11, with a anaconda environment, python 3.5.
I have search the web to the imgutils module and have not a module, which have a imshow function.
Any suggestions?
Update
from cv2 import imshow
img3 = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
plt.figure(figsize=(20,10))
plt.subplot(1,2,1); imshow(img)
plt.subplot(1,2,2); imshow(img3)
Traceback
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-d01b4c235975> in <module>
5 plt.figure(figsize=(20,10))
6
----> 7 plt.subplot(1,2,1); imshow(img)
8 plt.subplot(1,2,2); imshow(img3)
TypeError: Required argument 'mat' (pos 2) not found

imgutils does not seem to contain an imshow attribute (and therefore definitely not a function). It does not contain an imshow submodule, nor does it imports an imshow function in the __init__.py file: it basically reimports elements from the submodules like, but a fast search does not yield an imshow function.
You probably wan to use the imshow from the matplotlib.pyplot module, so you should replace:
from imgutils import imshow
with:
from matplotlib.pyplot import imshow
This then imports the cv2.imshow [pyplot-doc] function.

Related

Getting the following error while using scikit-image to read images "AttributeError: 'PngImageFile' object has no attribute '_PngImageFile__frame' "

I am using scikit-image to load a random image from a folder. OpenCV is being used for operations later on..
Code is as follows (only relevant parts included)
import imageio
import cv2 as cv
import fileinput
from collections import Counter
from data.apple_dataset import AppleDataset
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor
from torchvision.transforms import functional as F
import utility.utils as utils
import utility.transforms as T
from PIL import Image
import skimage.io
from skimage.viewer import ImageViewer
from matplotlib import pyplot as plt
%matplotlib inline
APPLE_IMAGE_PATH = r"__mypath__\samples\apples\images"
# Load a random image from the images folder
FILE_NAMES = next(os.walk(APPLE_IMAGE_PATH))[2]
random_apple_in_folder = os.path.join(APPLE_IMAGE_PATH, random.choice(FILE_NAMES))
apple_image = skimage.io.imread(random_apple_in_folder)
apple_image_cv = cv.imread(random_apple_in_folder)
apple_image_cv = cv.cvtColor(apple_image_cv, cv.COLOR_BGR2RGB)
Error is as follows
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-9575eed18f18> in <module>
11 FILE_NAMES = next(os.walk(APPLE_IMAGE_PATH))[2]
12 random_apple_in_folder = os.path.join(APPLE_IMAGE_PATH, random.choice(FILE_NAMES))
---> 13 apple_image = skimage.io.imread(random_apple_in_folder)
14 apple_image_cv = cv.imread(random_apple_in_folder)
AttributeError: 'PngImageFile' object has no attribute '_PngImageFile__frame'
How do i proceed from here? What should i change???
This is a bug in Pillow 7.1.0. You can upgrade Pillow with pip install -U pillow. See this bug report for more information:
https://github.com/scikit-image/scikit-image/issues/4548

Error while opening the image in opencv and python

I am trying to start with basics of opencv with python but when i executed the below code :
import cv2
import numpy as np
img = cv2.imread('bg.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
But i am getting this error:-
Traceback (most recent call last):
File "F:\OpenCV Programs\OpenCV1.py", line 7, in <module>
cv2.imshow('image',img)
error: C:\build\master_winpack-bindings-win32-vc14-
static\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0
&& size.height>0 in function cv::imshow
Plz help me out thank you..!
The code you posted works, if there is an image called bg.jpg within the same directory where you are running the code.
Anyway, you could try to reference the full path to the image:
import os
img_path = os.path.join(os.getcwd(), 'bg.jpg')
img = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
The image you attempt to load does not exist.
As you cannot display a non-existing image you get an error messsage for trying.
You'll most likely messed up the path. Maybe use an absolute path to the image.

Error when using cv2.findContours

I tried to find contours for image, but it gives an error.
My code is:
import cv2
import numpy as np
img = cv2.imread('star.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
contours,hierarchy = cv2.findContours(thresh, 1, 2)
error is:
Traceback (most recent call last): File "C:\Python27\OpenCVContore.py", line 5, in <module> contours,hierarchy,thresh = cv2.findContours(thresh, 1, 2) error: ........\opencv\modules\core\src\matrix.cpp:236: error: (-215) step[dims-1] == (size_t)CV_ELEM_SIZE(flags) in function cv::Mat::create
I am using Python 2.7 OpenCV 2.4.7,2.4.11
Any help is appreciated.
Solved this problem by updating my python version to 3.4 and opencv version 3. But cannot find real solutions for this. Why python 2.x versions not fully support for openCV

matplotlib: ImportError: cannot import name 'pyplot'

I have matplotlib lib installed using pip but when I run this code it gives me this error:
shar#shar-Lenovo-G50-30 ~ $ python3 opencv.py
Traceback (most recent call last):
File "opencv.py", line 3, in <module>
from matplotlib import pyplot as plt
ImportError: cannot import name 'pyplot'
My code is:
import numpy as np
import cv2
from matplotlib import pyplot as plt
img1 = cv2.imread('/home/shar/home.jpg',0) # queryImage
img2 = cv2.imread('/home/shar/home2.jpg',0) # trainImage
# Initiate SIFT detector
orb = cv2.ORB()
# find the keypoints and descriptors with SIFT
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
# create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# Match descriptors.
matches = bf.match(des1,des2)
# Sort them in the order of their distance.
matches = sorted(matches, key = lambda x:x.distance)
# Draw first 10 matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10], flags=2)
plt.imshow(img3),plt.show()
I also tried to install matplotlib from source and that still give me the error.
Make sure you don't have any file names matplotlib.py that you have created if any then rename or delete that file. It worked for me
Did you get error saying "cannot import name 'pyplot' from partially initialized module 'matplotlib' " ?
You could have unknowingly saved your file name as matplotlib.py, since it comes as default file name suggestion.
So change the file name and Bingo, you will not get that error again..

Python PIL NameError global name Image is not defined

I installed PIL 1.1.7 using the executable for Python 2.7. But when I run this code :
import requests
from PIL import *
def main() :
target = "http://target/image.php" #returns binary data for a PNG.
cookies = {"mycookie1", "mycookie2"}
r = requests.get(target, cookies=cookies)
im = Image.open(r.content) #r.content contains binary data for the PNG.
print im
if __name__ == "__main__" :
main()
It gives the error :
Traceback (most recent call last):
File "D:\Programming\Python\code\eg_cc1.py", line 17, in <module>
main()
File "D:\Programming\Python\code\eg_cc1.py", line 13, in main
im = Image.open(r.content)
NameError: global name 'Image' is not defined
I installed PIL in Lib\site-packages.
You need to explicitly import the Image module:
>>> from PIL import *
>>> Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Image' is not defined
>>> import PIL.Image
>>> Image
<module 'Image' from 'c:\Python27\lib\site-packages\PIL\Image.pyc'>
>>>
Or just
>>> import Image
Use
from PIL import Image
This way it will work, and your code will be forward-compatible with Pillow. On the other hand, import Image will not work with Pillow.
The development of PIL has been stopped in mid 2011, without any official announcement or explication. Pillow is a fork of original PIL and is actively developed.
If
import Image
is not working.
Try:
from PIL import Image
There is compatibility issue with all the packages in PIL. Try downloading PIL package manually for the latest version.
Actually worked with this in Python 3.2 Version, everything is working fine.

Categories

Resources