Ask Your Question
0

In opencv-python tutorials core operations basic operations on images accessing pixel value throws an error [closed]

asked 2017-07-13 01:25:27 -0600

AA gravatar image

updated 2017-07-13 01:30:58 -0600

berak gravatar image

Here is the code(with error as i am working directly on the shell as instructed by the website) same as on the website:

>>> import cv2
>>> import numpy as np
>>> img = cv2.imread('result.jpg')
>>> px = img[100,100]

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    px = img[100,100]
TypeError: 'NoneType' object has no attribute '__getitem__'

Thanks :)

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by AA
close date 2017-07-13 01:50:00.471940

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-07-13 01:35:31 -0600

berak gravatar image

updated 2017-07-13 01:40:29 -0600

that's a simple one - your image did not load (and thus is empty / None).

lesson here, whenever you read any resource from disc, you have to check if that operation succeeded !

 img = cv2.imread('result.jpg')
 if img not None: 
      ## do something with img
 else:
      ## most likely, the path to the image was wrong

even if you use exactly the same code, as other people, your filestructure might differ, so, again: CHECK !

edit flag offensive delete link more

Comments

Th directory shouldn't matter. I'm using raspberry pi 3 btw. Also what does has no attribute '__getitem__' mean? Thanks

AA gravatar imageAA ( 2017-07-13 01:45:48 -0600 )edit
  • The directory shouldn't matter. -- it sure does !
  • I mean it can find the image. -- obviously not !
  • what does has no attribute '__getitem__' mean -- it tries to find width and height of the image, but None does not have any properties like that.

your sample assumes, you run the script in the same folder, where the image is (relative path)

berak gravatar imageberak ( 2017-07-13 01:47:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-07-13 01:25:27 -0600

Seen: 391 times

Last updated: Jul 13 '17