Eye Dropper Color Picker tool in Python using OpenCV

Eye Dropper Color Picker tool in Python using OpenCV

A eye dropper tool also called as color picker is used to pick color from an image.

An image is made of pixels (Read : Anatomy Of a Digital Image) and each pixel has a RGB value, a color picker tool extracts the RGB value of a pixel in an image by hovering the mouse pointer on the image and clicking.

Eye Dropper tool

The eyedropper is one of the default tools of any image manipulation software, it would be fun to make one ourselves and learn how it works.

In this tutorial we will learn how to make a color picker from scratch in python using OpenCV.

Color Picker in OpenCV Python

In this color picker application we will hav 3 windows :

  • Image Window ( to show the sample image )
  • Live Color Update window ( color in the window will update as mouse hover over the sample image)
  • Selected Color Window ( color is selected when user click left mouse button )

After the user picks a color he can save the RGB value of the selected color in a text file by clicking the Right mouse button.

Python Color Picker widows

Color Picker Pseudocode

  • Import the necessary packages cv2 and numpy
  • create 2 np array to store color picker values (color_explore, color_selected)
  • define a function to save the RGB value of selected color in file ( def write_to_file ) (Read: How to write file in python )
  • define a mouse callback function which:
    • update the color value of color explore window on mouse hover
    • update the color value of the selected color window on mouse left click
    • call write_to_file function to write RGB value in file on mouse right click
  • create named window for color_explore, color_selected, sample image
  • read image from path
  • bind mousecall back function with sample image named window ( cv2.setMouseCallback(‘image’,show_color) )
  • use while loop to shoe and refresh the named windows.
Color picker Working Demo

Code For Color Picker in OpenCV python

import cv2
import numpy as np

color_explore = np.zeros((150,150,3), np.uint8)  
color_selected = np.zeros((150,150,3), np.uint8)

#save selected color RGB in file
def write_to_file(R,G,B):
	f = open("saved_color.txt", "a")
	RGB_color=str(R) + "," + str(G) + "," + str(B) + str("\n")

#Mouse Callback function
def show_color(event,x,y,flags,param): 
	color_explore [:] = (B,G,R)

	if event == cv2.EVENT_LBUTTONDOWN:
		color_selected [:] = (B,G,R)

	if event == cv2.EVENT_RBUTTONDOWN:

#live update color with cursor
cv2.resizeWindow("color_explore", 50,50);

#Show selected color when left mouse button pressed
cv2.resizeWindow("color_selected", 50,50);

#image window for sample image

#sample image path

#read sample image

#mouse call back function declaration

#while loop to live update
while (1):
	k = cv2.waitKey(1) & 0xFF
	if k == 27:


Stay updated with the latest advancements Explore event, webinars and conferences

Thank you for reading, Happy Learning, drop your suggestion in the comments.

Feel free to follow us on Youtube, Linked In , Instagram

Loading comments...