Home > Enterprise >  TypeError: tuple indices must be integers or slices, not tuple when cropping OpenCV image taken from
TypeError: tuple indices must be integers or slices, not tuple when cropping OpenCV image taken from

Time:05-27

I am trying to crop an image taken with the camera using OpenCV with the below code on a Raspberry Pi 3B running Raspbian GNU/Linux 11 (bullseye) and I'm getting this output:

Traceback (most recent call last):
File "/home/pi/Desktop/test-code-elias/camquali.py", line 141, in <module>
right_wall = img[1:1944, 1:1000]
TypeError: tuple indices must be integers or slices, not tuple

My Code:

img = cam.read()
right_wall = img[0:1944, 0:1000]

CodePudding user response:

img is a tuple. Apparently cam.read() returns a tuple and not whatever you're expecting. Possibly one of the elements in the tuple is the image array that you're looking for, so you would have to extract that first. Tuples are one-dimensional, you're providing indices for two dimensions, that's where the error message is coming from.

If, as was kindly added by https://stackoverflow.com/users/16487900/berak, the output is (success, img), you need to change your code like that:

success, img = cam.read()
assert success
right_wall = img[0:1944, 0:1000]
  • Related