工具更健壮,更新日志后续再补充,源码已复制更新至V1.3!
Changelog
2023年1月20日13:57:37
- 增加去背景功能,可以保存成透明背景的png格式。
- 排版的代码从grid改为place方式。
- 增加menubar,帮助。
- 整理部分代码结构,减少了函数传参,采用字典方式。
功能介绍
选择一张图片,然后输入希望改变哪种颜色的色号,然后输入希望变成新颜色的色号。然后点击提交,就可以完成图片预览和保存功能。此功能和PS里面的魔术棒功能差不多。
注:此功能也有色差的选择,目前默认是30色差。后续再优化一下,色差也尽量可输入。
工具下载
https://www.jianguoyun.com/p/DaggDTQQ4MGKChiOzvIEIAA
源码展示
由于非科班出身,而且只是闲来无聊随便写写,所以代码漏洞比较多。也不打算继续优化代码。
#encoding=utf-8
import time
import cv2 as cv
import numpy as np
import tkinter
import tkinter.messagebox
from tkinter.filedialog import asksaveasfilename
from tkinter import END
import PIL
from PIL import Image,ImageTk
def test(content):
# 实时验证输入框是否只为数字!
return content.isdigit()
def helpYou():
tkinter.messagebox.showinfo("帮助", "就是一个PS工具,没那么难用,自己摸索一下!")
pass
def inputIsOk():
getOldColorInputToInt()
getNewColorInputToInt()
oldColor = (result_dict['oldG'], result_dict['oldB'], result_dict['oldR'])
newColor = (result_dict['newB'], result_dict['newG'], result_dict['newR'])
if max(oldColor,newColor)>255 or min(oldColor,newColor)<0:
tkinter.messagebox.showinfo("提示","输入范围:0-255!")
return False
else:
return True
def removeBackgroundColor():
global gl_imgtemp
if max(result_dict['oldG'],result_dict['oldB'],result_dict['oldR']) > 255:
tkinter.messagebox.showinfo("提示","当前输入框数值不是0-255 !")
else:
getOldColorInputToInt()
img = cv.imdecode(np.fromfile(result_dict['file_path'], dtype=np.uint8), -1)
result = cv.cvtColor(img, cv.COLOR_BGR2BGRA)
for i in range(0, img.shape[0]): # 访问所有行
for j in range(0, img.shape[1]): # 访问所有列
if img[i, j, 0] == result_dict['oldB'] and img[i, j, 1] == result_dict['oldG'] and img[i, j, 2] == result_dict['oldR']:
result[i, j, 3] = 0
print(type(result))
cv.imwrite('result.png', result)
removeBGWindow = tkinter.Toplevel()
removeBGWindow.minsize(img.shape[1], img.shape[0])
removeBGWindow.maxsize(img.shape[1], img.shape[0])
removeBGWindow.title("去掉背景预览")
photo = PIL.ImageTk.PhotoImage(Image.open('result.png'))
labelDisplayPic2 = tkinter.Label(removeBGWindow, image=photo) # 生成标签并将标签添加到主窗口
labelDisplayPic2.image = photo
labelDisplayPic2.place(x=0, y=0)
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'])
else:
pass
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
try:
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]))
getOldColorInputToInt()
cv.destroyAllWindows()
except:
tkinter.messagebox.showinfo("提示","未点选图像中的像素!")
def changeColor():
global gl_imgtemp
image = cv.imdecode(np.fromfile(result_dict['file_path'], dtype=np.uint8), -1)
result_dict['picHeight'] = image.shape[0] ## 图像高度
result_dict['picWidth'] = image.shape[1] ## 图像宽度
chromaticAberration = result_dict['chromaticAberration']
oldColor = (result_dict['oldG'], result_dict['oldB'], result_dict['oldR'])
newColor = (result_dict['newB'], result_dict['newG'], result_dict['newR'])
for j in range(0,result_dict['picWidth']):
for i in range(0,result_dict['picHeight']):
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 >=oldColor[0]-chromaticAberration and a <= oldColor[0]+chromaticAberration:
ifa = True
if b >=oldColor[1]-chromaticAberration and b <= oldColor[1]+chromaticAberration:
ifb = True
if c >=oldColor[2]-chromaticAberration and c <= oldColor[2]+chromaticAberration:
ifc = True
if ifa and ifb and ifc:
image.itemset((i, j, 0),newColor[0])
image.itemset((i, j, 1), newColor[1])
image.itemset((i, j, 2), newColor[2])
gl_imgtemp = image
result_dict['picExist']=True
def getOldColorInputToInt():
try:
result_dict['oldG'] = int(inputG.get())
result_dict['oldB'] = int(inputB.get())
result_dict['oldR'] = int(inputR.get())
except:
tkinter.messagebox.showinfo("提示","原颜色输入值不合理!")
def getNewColorInputToInt():
try:
result_dict['newG'] = int(inputG2.get())
result_dict['newB'] = int(inputB2.get())
result_dict['newR'] = int(inputR2.get())
except:
tkinter.messagebox.showinfo("提示","新颜色输入值不合理!")
def showChangeColor():
if inputIsOk():
changeColor()
global gl_imgtemp
cv.imshow("img2", gl_imgtemp)
else:
tkinter.messagebox.showinfo("提示","当前输入框数值不是0-255 !")
def saveChangeColor():
if result_dict['picExist']:
global gl_imgtemp
cv.imwrite('change1.png', gl_imgtemp)
else:
tkinter.messagebox.showinfo("提示","当前没有生成图片!")
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'])
try:
photo = PIL.ImageTk.PhotoImage(Image.open(picName))
# print(type(photo))
labelDisplayPic = tkinter.Label(root,image=photo) # 生成标签并将标签添加到主窗口
labelDisplayPic.image=photo
labelDisplayPic.place(x=20,y=200)
btnPickColor['state'] = tkinter.NORMAL
except:
pass
if __name__ == '__main__':
result_dict = {'file_path':"123",'posX':"-1",'posY':"-1",
'picWidth':"0",'picHeight':"0",
'oldR':256,'oldG':256,'oldB':256,
'newR':256,'newG':256,'newB':256,
'chromaticAberration':10,
'picExist':False} #用来存储函数中需返回的值
global gl_imgtemp
gl_imgtemp = []
root=tkinter.Tk()
root.minsize(800,600)
root.maxsize(1900,1000)
root.title("选图换颜色V1.3")
testCMD = root.register(test)
file_menu = tkinter.Menu(root)
file_menu.add_command(label="帮助",command=helpYou)
root.config(menu=file_menu)
openFile = tkinter.Button(root,text='选择图片文件',command=funOpenFile) #生成button
openFile.place(x=20,y=20) #将button添加到root主窗口
labelDisplayFileName=tkinter.Label(root,text='请选择图片文件:') #生成标签并将标签添加到主窗口
labelDisplayFileName.place(x=150,y=20)
##################################################################
# 展示希望替换的颜色色号
label1=tkinter.Label(root,text='原颜色色号:') #生成标签并将标签添加到主窗口
label1.place(x=20,y=70)
labelR=tkinter.Label(root,text='R:',width=5) #生成标签并将标签添加到主窗口
labelR.place(x=100,y=70)
inputR=tkinter.Entry(root,width=5, validate='key',validatecommand=(testCMD,'%P'))
inputR.place(x=150,y=70)
labelG=tkinter.Label(root,text='G:',width=5) #生成标签并将标签添加到主窗口
labelG.place(x=200,y=70)
inputG=tkinter.Entry(root,width=5, validate='key',validatecommand=(testCMD,'%P'))
inputG.place(x=250,y=70)
labelB=tkinter.Label(root,text='B:',width=5) #生成标签并将标签添加到主窗口
labelB.place(x=300,y=70)
inputB=tkinter.Entry(root,width=5, validate='key',validatecommand=(testCMD,'%P'))
inputB.place(x=350,y=70)
################################################################
# 展示希望新颜色色号
label2=tkinter.Label(root,text='新颜色的色号:') #生成标签并将标签添加到主窗口
label2.place(x=20,y=100)
labelR2=tkinter.Label(root,text='R:',width=5) #生成标签并将标签添加到主窗口
labelR2.place(x=100,y=100)
inputR2=tkinter.Entry(root,width=5, validate='key',validatecommand=(testCMD,'%P'))
inputR2.place(x=150,y=100)
labelG2=tkinter.Label(root,text='G:',width=5) #生成标签并将标签添加到主窗口
labelG2.place(x=200,y=100)
inputG2=tkinter.Entry(root,width=5, validate='key',validatecommand=(testCMD,'%P'))
inputG2.place(x=250,y=100)
labelB2=tkinter.Label(root,text='B:',width=5) #生成标签并将标签添加到主窗口
labelB2.place(x=300,y=100)
inputB2=tkinter.Entry(root,width=5, validate='key',validatecommand=(testCMD,'%P'))
inputB2.place(x=350,y=100)
############################################
btnPickColor=tkinter.Button(root,text='选取色号',command=funPickColor) #生成button
btnPickColor.place(x=20,y=150)
btnPickColor['state'] = tkinter.DISABLED
button1=tkinter.Button(root,text='提交修改',command=showChangeColor) #生成button
button1.place(x=150,y=150)
btnSavePic=tkinter.Button(root,text='保存修改图',command=saveChangeColor) #生成button
btnSavePic.place(x=250,y=150)
btnRemveBG = tkinter.Button(root, text='去除该颜色', command=removeBackgroundColor) # 生成button
btnRemveBG.place(x=350, y=150)
root.mainloop()