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.
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.
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.
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") f.write(RGB_color) f.close() #Mouse Callback function def show_color(event,x,y,flags,param): B=img[y,x] G=img[y,x] R=img[y,x] color_explore [:] = (B,G,R) if event == cv2.EVENT_LBUTTONDOWN: color_selected [:] = (B,G,R) if event == cv2.EVENT_RBUTTONDOWN: B=color_selected[10,10] G=color_selected[10,10] R=color_selected[10,10] print(R,G,B) write_to_file(R,G,B) print(hex(R),hex(G),hex(B)) #live update color with cursor cv2.namedWindow('color_explore') cv2.resizeWindow("color_explore", 50,50); #Show selected color when left mouse button pressed cv2.namedWindow('color_selected') cv2.resizeWindow("color_selected", 50,50); #image window for sample image cv2.namedWindow('image') #sample image path img_path="sample_img.png" #read sample image img=cv2.imread(img_path) #mouse call back function declaration cv2.setMouseCallback('image',show_color) #while loop to live update while (1): cv2.imshow('image',img) cv2.imshow('color_explore',color_explore) cv2.imshow('color_selected',color_selected) k = cv2.waitKey(1) & 0xFF if k == 27: break cv2.destroyAllWindows()
Stay updated with the latest advancements Explore event, webinars and conferences