[Python] cv2.rectangle not working when using a tuple from a np array as color

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I was trying to make rectangles with random colors in a script, but I came across this really weird bug where, when I use a hard-coded color everything works fine (like (255, 0, 0)), but when I try to make a random color using np.random.randint, it gives me the following error:


    TypeError: function takes exactly 4 arguments (2 given)
    ​

    This minimal code reproduces my problem.

    import numpy as np
    from cv2 import cv2

    img = np.ones((900, 1200), dtype='uint8')
    color = tuple(np.random.randint(0, 256, 3, dtype='uint8')) # Does not work
    color = (127, 127, 127) # Works
    cv2.rectangle(img, (100, 100), (300, 300), color, 4)

    Continue reading...

Compartilhe esta Página