更新一下:
- 增加选取颜色的功能;
功能介绍
选择一张图片,然后输入希望改变哪种颜色的色号,然后输入希望变成新颜色的色号。然后点击提交,就可以完成图片预览和保存功能。此功能和PS里面的魔术棒功能差不多。
注:此功能也有色差的选择,目前默认是30色差。后续再优化一下,色差也尽量可输入。
工具下载
https://www.jianguoyun.com/p/DVVtc94Q4MGKChiGxvIEIAA
源码展示
由于非科班出身,而且只是闲来无聊随便写写,所以代码漏洞比较多。也不打算继续优化代码。
#encoding=utf-8
import cv2 as cv
import numpy as np
import tkinter
from tkinter import *
from tkinter.filedialog import asksaveasfilename
import PIL
from PIL import Image,ImageTk
def mouse_click(event, x, y, flags, para):
if event == cv.EVENT_LBUTTONDOWN: # 左边鼠标点击
result_dict['posX']=x
result_dict['posY'] = y #将点选的坐标输出,这里的坐标是相对图像窗口的坐标,所以可以以此来获取颜色值。
# print('PIX:', result_dict['posX'], result_dict['posY'])
def funPickColor():
image = cv.imdecode(np.fromfile(result_dict['file_path'], dtype=np.uint8), -1)
cv.namedWindow("click anykey to exit")
cv.setMouseCallback("click anykey to exit", mouse_click)
while True:
cv.imshow('click anykey to exit', image)
if cv.waitKey() != ord('q'):
break
temp = image[result_dict['posY'], result_dict['posX']] #把坐标对应的颜色值获取到
temp = temp[::-1] #获取的颜色值是BGR的,所以需要做倒序处理。
temp=list(temp)
# 先清空输入框
inputR.delete(0,END)
inputG.delete(0, END)
inputB.delete(0, END)
# 再载入输入框的值
inputR.insert(0,str(temp[0]))
inputG.insert(0, str(temp[1]))
inputB.insert(0, str(temp[2]))
cv.destroyAllWindows()
def changeColor(picName,oldColor,chromaticAberration,newColor):
oldColor1 = oldColor[0]
oldColor2 = oldColor[1]
oldColor3 = oldColor[2]
newColor1 = newColor[0]
newColor2 = newColor[1]
newColor3 = newColor[2]
image = cv.imdecode(np.fromfile(picName, dtype=np.uint8), -1)
h = image.shape[0] ## 图像高度
w = image.shape[1] ## 图像宽度
for j in range(0,w):
for i in range(0,h):
a = image.item(i,j,0)
b = image.item(i,j,1)
c = image.item(i,j,2)
ifa = False
ifb = False
ifc = False
if a >oldColor1-chromaticAberration and a < oldColor1+chromaticAberration:
ifa = True
if b >oldColor2-chromaticAberration and b < oldColor2+chromaticAberration:
ifb = True
if c >oldColor3-chromaticAberration and c < oldColor3+chromaticAberration:
ifc = True
if ifa and ifb and ifc:
image.itemset((i, j, 0),newColor1)
image.itemset((i, j, 1), newColor2)
image.itemset((i, j, 2), newColor3)
return image
def showChangeColor():
btnSavePic['state']=tkinter.NORMAL
inputGvalue = inputG.get()
inputBvalue = inputB.get()
inputRvalue = inputR.get()
inputG2value = inputG2.get()
inputB2value = inputB2.get()
inputR2value = inputR2.get()
image = changeColor(result_dict['file_path'], (int(inputBvalue), int(inputGvalue), int(inputRvalue)), 30,(int(inputB2value), int(inputG2value), int(inputR2value)))
image = image[:, :, ::-1] #将image参数修正为BGR
img_pil = Image.fromarray(image) #此时img_pil是BGR通道。
img_pil = PIL.ImageTk.PhotoImage(img_pil)
labelDisplayPic2 = tkinter.Label(root, image=img_pil) # 生成标签并将标签添加到主窗口
labelDisplayPic2.image = img_pil
labelDisplayPic2.grid(row=6, column=0, columnspan=7, sticky="w")
def saveChangeColor():
inputGvalue = inputG.get()
inputBvalue = inputB.get()
inputRvalue = inputR.get()
inputG2value = inputG2.get()
inputB2value = inputB2.get()
inputR2value = inputR2.get()
image = changeColor(result_dict['file_path'], (int(inputBvalue), int(inputGvalue), int(inputRvalue)), 30,
(int(inputB2value), int(inputG2value), int(inputR2value)))
cv.imwrite("change1.png", image) # 此时image参数是RGB通道
btnSavePic['state'] = tkinter.DISABLED
def funOpenFile():
picName = tkinter.filedialog.askopenfilename()
result_dict['file_path'] = picName # 把结果放在字典里,作用相当于 return
# labelDisplayFileName.text(result_dict['file_path'])
labelDisplayFileName.configure(text=result_dict['file_path'])
photo = PIL.ImageTk.PhotoImage(Image.open(picName))
# print(type(photo))
labelDisplayPic = tkinter.Label(root,image=photo) # 生成标签并将标签添加到主窗口
labelDisplayPic.image=photo
labelDisplayPic.grid(row=5, column=0, columnspan=7, sticky="w")
result_dict = {'file_path':"123",'posX':"0",'posY':"0"} #用来存储函数中需返回的值
root=tkinter.Tk()
root.minsize(500,300)
root.maxsize(800,600)
root.title("选图换颜色V1.1")
openFile = tkinter.Button(root,text='选择图片文件',command=funOpenFile) #生成button
openFile.grid(row=0,column=0) #将button添加到root主窗口
labelDisplayFileName=tkinter.Label(root,text='请选择图片文件:') #生成标签并将标签添加到主窗口
labelDisplayFileName.grid(row=0,column=1,columnspan=6,sticky="w")
##################################################################
# 展示希望替换的颜色色号
label1=tkinter.Label(root,text='请输入希望替换的颜色色号:') #生成标签并将标签添加到主窗口
label1.grid(row=1,column=0)
labelR=tkinter.Label(root,text='R:',width=5) #生成标签并将标签添加到主窗口
labelR.grid(row=1,column=1,sticky="w")
inputR=tkinter.Entry(root,width=5)
inputR.grid(row=1,column=2,sticky="w")
labelG=tkinter.Label(root,text='G:',width=5) #生成标签并将标签添加到主窗口
labelG.grid(row=1,column=3,sticky="w")
inputG=tkinter.Entry(root,width=5)
inputG.grid(row=1,column=4,sticky="w")
labelB=tkinter.Label(root,text='B:',width=5) #生成标签并将标签添加到主窗口
labelB.grid(row=1,column=5,sticky="w")
inputB=tkinter.Entry(root,width=5)
inputB.grid(row=1,column=6,sticky="w")
################################################################
# 展示希望新颜色色号
label2=tkinter.Label(root,text='请输入希望新颜色的色号:') #生成标签并将标签添加到主窗口
label2.grid(row=2,column=0)
labelR2=tkinter.Label(root,text='R:',width=5) #生成标签并将标签添加到主窗口
labelR2.grid(row=2,column=1,sticky="w")
inputR2=tkinter.Entry(root,width=5)
inputR2.grid(row=2,column=2,sticky="w")
labelG2=tkinter.Label(root,text='G:',width=5) #生成标签并将标签添加到主窗口
labelG2.grid(row=2,column=3,sticky="w")
inputG2=tkinter.Entry(root,width=5)
inputG2.grid(row=2,column=4,sticky="w")
labelB2=tkinter.Label(root,text='B:',width=5) #生成标签并将标签添加到主窗口
labelB2.grid(row=2,column=5,sticky="w")
inputB2=tkinter.Entry(root,width=5)
inputB2.grid(row=2,column=6,sticky="w")
############################################
btnPickColor=tkinter.Button(root,text='选取色号',command=funPickColor) #生成button
btnPickColor.grid(row=3,column=0,columnspan=2)
button1=tkinter.Button(root,text='提交修改',command=showChangeColor) #生成button
button1.grid(row=3,column=1,columnspan=2)
btnSavePic=tkinter.Button(root,text='保存修改图',command=saveChangeColor) #生成button
btnSavePic.grid(row=3,column=2,columnspan=3)
btnSavePic['state']=tkinter.DISABLED
root.mainloop()
2023年1月19日