Shapes or index errors in NumPy

  • Python
  • NumPy

You may see

ValueError: operands could not be broadcast together with shapes (2,3) (3,2)
IndexError: index 500 is out of bounds for axis 0 with size 480

Why it happens

  • Pixels are img[y, x], not img[x, y].
  • Two pictures have different sizes or one is grey.

How to fix it

  1. Print both shapes.
  2. Use cv2.resize so the sizes match.
  3. Use img[y, x], but (x, y) for drawing.

It worked if: The shapes match.

Seen in a real run (some lines are close variants of it). Produced with Python 3.13 on Windows. With NumPy 2.2.6.

Didn't help? Search another error or ask us.