How to create a Color picker using Python ?

I am a Engineer
Search for a command to run...

I am a Engineer
No comments yet. Be the first to comment.
Scalable Wireless Distributed Consensus for Autonomous Vehicles at Unsignalized Intersections

The Ultimate VLSI Roadmap is designed to help students and professionals gain clarity in choosing a role for their future careers.

A convolutional neural network (CNN) is a type of artificial neural network used primarily for image recognition and processing, due to its ability to

Natural language processing (NLP) is a branch of artificial intelligence that helps computers understand, interpret, and manipulate human language.

Introduction to Python

Color pickers are widely used to find a particular color in an image which can be used for theme selection and font colors for applications. In this blog, we will see how to create a color picker using python.
Creating a color picker using python programming language
from tkinter import *
from tkinter import colorchooser ,filedialog
from PIL import Image, ImageTk
import PIL
tkinter is for the UI of the Application . PIL is used for getting image information
root=Tk()
root.title(" Click on any where on image to pick a color ")
c=Canvas(root)
First, we are creating a window in which we are going to place a canvas
def _from_rgb(rgb):
return "#%02x%02x%02x" % rgb
def colorpic(e):
global width,height
global entry,entry1,disp
global newFrame
b1=Image.open(glb_img_name).resize((width,height),Image.ANTIALIAS).convert("RGB")
pixs=b1.getpixel((e.x,e.y))
ds=_from_rgb((pixs))
color=ds
entry.delete(0,END)
entry.insert(0,str(pixs))
entry1.delete(0,END)
entry1.insert(0,str(color))
disp.config(bg=color,fg=color)
newFrame.grid(row=0,column=1)
def width_height(owidth,oheight):
while((oheight>680 )):
owidth,oheight=int(owidth/1.1),int(oheight/1.1)
while((owidth>930 )):
owidth,oheight=int(owidth/1.1),int(oheight/1.1)
return owidth,oheight
def open_another():
global c
global newFrame
global myimg
global glb_img_name
global width,height
newFrame.grid_forget()
imgname=filedialog.askopenfilename(initialdir="/Desktop/python codes",title="open images",filetypes=(("png files","*.png"),("jpg files","*.jpg"),("jpeg files","*.jpeg")))
copy=Image.open(imgname)
owidth,oheight=copy.size
width,height=width_height(owidth,oheight)
myimg=ImageTk.PhotoImage(Image.open(imgname).resize((width,height),Image.ANTIALIAS))
c.grid_forget()
glb_img_name=imgname
c.config(width=width,height=height)
c.create_image(0,0,anchor=NW,image=myimg)
c.grid(row=0,column=0)
this function will convert rgb tuple into colorcode value
this function will get the x,y position where the mouse clicked in the image then it uses the x,y position to extract the pixel from the image then it will display the colorcode and rgb values
this function will reduce the original image width and height to fit in the screen
this function will open another image after opening the initial image
imgname=filedialog.askopenfilename(initialdir="/Desktop/python codes",title="open images",filetypes=(("png files","*.png"),("jpg files","*.jpg"),("jpeg files","*.jpeg")))
imagename=imgname
glb_img_name=imagename
copy=Image.open(imagename)
owidth,oheight=copy.size
width,height=width_height(owidth,oheight)
myimg=ImageTk.PhotoImage(Image.open(glb_img_name).resize((width,height),Image.ANTIALIAS))
c.config(width=width,height=height)
c.create_image(0,0,anchor=NW,image=myimg)
c.grid(row=0,column=0)
c.bind("<Button-1>",colorpic)
newFrame=Frame(root)
entry= Entry(newFrame, width= 10)
entry.grid(row=0,column=0,padx=10)
entry1= Entry(newFrame, width= 10)
entry1.grid(row=0,column=1,padx=10)
disp=Button(newFrame,text=" ")
disp.grid(row=0,column=2,padx=10)
sel=Button(newFrame,text="Open",command=open_another)
sel.grid(row=0,column=3)
root.mainloop()
initially in this, we are opening an image from any directory then that image will get displayed on the canvas . Then if any click is detected in the canvas it will invoke the colorpic function and display the chosen color information .
final code - https://github.com/vasanthkumar7/Color-picker
