Search code examples
pythonopencvcolorstuplestinker

need help to change colour in cv2, with user input in tinker


I have a list of colours:

    red = (50, 50, 255)
    blue = (255, 127, 0)
    dark_blue = (127, 20, 0)
    green = (127, 255, 0)

initially, I set output colour:

    line_colour = red

this works when drawing a line:

    cv2.line(img, (x3, y3), (x2, y2), line_colour, 5)

I expanded the program to get colour input from user, via tinker:

    colour_list = ['red', 'blue', 'dark blue', 'green',
           'light green', 'yellow', 'pink', 'black', 'white']

I am able to get the colour:

    line_clicked.get()

Need help to change the cv2.line(......) so that the line colour is drawn with the user colour choice.


Solution

  • You can put all your colors in a dictionary, with the name as a key, and the RGB as a value.

    Something like:

    color_dict = {'red': (50, 50, 255),
                   'blue' : (255, 127, 0),
                   'dark blue': (127, 20, 0)}  # etc...
    

    Then if you have a color selected by the user:

    selected_colour_name = line_clicked.get()
    

    You can use the dictionary to get the RGB color and draw it:

    line_colour = colour_dict[selected_colour_name]
    cv2.line(img, (x3, y3), (x2, y2), line_colour, 5)