How to Create a RGB Color Picker for Images using OpenCV Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.


First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3


The goal is to make you understand how to access image pixel to get RGB color values



retval=cv.imread(filename[, flags])

Loads an image from a file.

filename Name of file to be loaded.
flags Flag that can take values of cv::ImreadModes


None=cv.namedWindow(winname[, flags])

Creates a window.

winname Name of the window in the window caption that may be used as a window identifier.
flags Flags of the window. The supported flags are: (cv::WindowFlags)


None=cv.imshow(winname, mat)

Displays an image in the specified window.

winname Name of the window.
mat Image to be shown.


retval=cv.waitKey([, delay])

Waits for a pressed key.

delay Delay in milliseconds. 0 is the special value that means “forever”.



Destroys all of the HighGUI windows.


  • Load the Original image using cv2.imread()
  • Create a window by using cv2.namedWindow()
  • Then we need to set mouse callbacks using cv2.setMouseCallback()
  • Create a while loop to detect mouse movements on that window
  • Wait for the cv2.EVENT_MOUSEMOVE in the callback functions
  • We also need to convert the BGR value to RGB value by reversing the tupple value
  • Display all the images using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2

def Life2CodingRGB(event, x, y, flags, param):
    if event == cv2.EVENT_MOUSEMOVE :  # checks mouse moves
        colorsBGR = image[y, x]
        colorsRGB=tuple(reversed(colorsBGR)) #Reversing the OpenCV BGR format to RGB format
        print("RGB Value at ({},{}):{} ".format(x,y,colorsRGB))

# Read an image
image = cv2.imread("hanif.jpg")

# Create a window and set Mousecallback to a function for that window
cv2.setMouseCallback('Life2CodingRGB', Life2CodingRGB)

# Do until esc pressed
while (1):
    cv2.imshow('Life2CodingRGB', image)
    if cv2.waitKey(10) & 0xFF == 27:

# if esc is pressed, close all windows.


5cd7e331e1406 How to Create a RGB Color Picker for Images using OpenCV Python
life2coding_icon [] How to Create a RGB Color Picker for Images using OpenCV Python

One thought on “How to Create a RGB Color Picker for Images using OpenCV Python

  1. Waqar Ali


    could you please tell me how can i delete the old value so that it only shows the current coordinate value.

    Thanks in Advance.

    Waqar Ali

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.