Search code examples
numpycrop

Error while cropping image using Numpy


i was try this code

import cv2
img = cv2.imread("image.jpg")
crop_img = img[100:200, 200:400] # Crop from x, y, w, h -> 100, 200, 100, 200
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

it work for few times..but suddenly it come out with this error:

Traceback (most recent call last):
File "test5.py", line 7, in <module>
crop_img = img[100:200, 200:400] # Crop from x, y, w, h -> 100, 200, 100, 200``
TypeError: 'NoneType' object has no attribute '__getitem__'

anyone can help me..


Solution

  • If cv2.imread cannot find the file name it returns None. If img is None, then img[100:200, 200:400] will raise

    TypeError: 'NoneType' object has no attribute '__getitem__'