快过年了,写个工具给自己用用。
功能介绍
选择一张图片,然后输入希望改变哪种颜色的色号,然后输入希望变成新颜色的色号。然后点击提交,就可以完成图片预览和保存功能。此功能和PS里面的魔术棒功能差不多。
注:此功能也有色差的选择,目前默认是30色差。后续再优化一下,色差也尽量可输入。
工具下载
https://www.jianguoyun.com/p/DeUY9p8Q4MGKChjCxPIEIAA
源码展示
由于非科班出身,而且只是闲来无聊随便写写,所以代码漏洞比较多。也不打算继续优化代码。
#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 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")
def start():
inputGvalue = inputG.get()
inputBvalue = inputB.get()
inputRvalue = inputR.get()
inputG2value = inputG2.get()
inputB2value = inputB2.get()
inputR2value = inputR2.get()
changeColor(result_dict['file_path'],(int(inputBvalue),int(inputGvalue),int(inputRvalue)),30,(int(inputB2value),int(inputG2value),int(inputR2value)))
result_dict = {'file_path':"123"} #用来存储函数中需返回的值
root=tkinter.Tk()
root.minsize(400,300)
root.maxsize(800,600)
root.title("选图换颜色V1.0")
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")
############################################
button1=tkinter.Button(root,text='提交修改',command=showChangeColor) #生成button
button1.grid(row=3,column=0,columnspan=3)
btnSavePic=tkinter.Button(root,text='保存修改图',command=saveChangeColor) #生成button
btnSavePic.grid(row=3,column=1,columnspan=4)
btnSavePic['state']=tkinter.DISABLED
root.mainloop()
2023年1月19日