修改圖片顏色的小工具1.0

快過年了,寫個(gè)工具給自己用用。

功能介紹

image.png

image.png

選擇一張圖片,然后輸入希望改變哪種顏色的色號(hào),然后輸入希望變成新顏色的色號(hào)。然后點(diǎn)擊提交,就可以完成圖片預(yù)覽和保存功能。此功能和PS里面的魔術(shù)棒功能差不多。
注:此功能也有色差的選擇,目前默認(rèn)是30色差。后續(xù)再優(yōu)化一下,色差也盡量可輸入。

工具下載

https://www.jianguoyun.com/p/DeUY9p8Q4MGKChjCxPIEIAA

源碼展示

由于非科班出身,而且只是閑來無聊隨便寫寫,所以代碼漏洞比較多。也不打算繼續(xù)優(yōu)化代碼。

#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參數(shù)修正為BGR
    img_pil = Image.fromarray(image)    #此時(shí)img_pil是BGR通道。
    img_pil = PIL.ImageTk.PhotoImage(img_pil)

    labelDisplayPic2 = tkinter.Label(root, image=img_pil)  # 生成標(biāo)簽并將標(biāo)簽添加到主窗口
    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)  # 此時(shí)image參數(shù)是RGB通道
    btnSavePic['state'] = tkinter.DISABLED


def funOpenFile():
    picName = tkinter.filedialog.askopenfilename()
    result_dict['file_path'] = picName  # 把結(jié)果放在字典里,作用相當(dāng)于 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)  # 生成標(biāo)簽并將標(biāo)簽添加到主窗口
    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"} #用來存儲(chǔ)函數(shù)中需返回的值

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='請(qǐng)選擇圖片文件:') #生成標(biāo)簽并將標(biāo)簽添加到主窗口
labelDisplayFileName.grid(row=0,column=1,columnspan=6,sticky="w")

##################################################################
# 展示希望替換的顏色色號(hào)
label1=tkinter.Label(root,text='請(qǐng)輸入希望替換的顏色色號(hào):') #生成標(biāo)簽并將標(biāo)簽添加到主窗口
label1.grid(row=1,column=0)

labelR=tkinter.Label(root,text='R:',width=5) #生成標(biāo)簽并將標(biāo)簽添加到主窗口
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) #生成標(biāo)簽并將標(biāo)簽添加到主窗口
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) #生成標(biāo)簽并將標(biāo)簽添加到主窗口
labelB.grid(row=1,column=5,sticky="w")
inputB=tkinter.Entry(root,width=5)
inputB.grid(row=1,column=6,sticky="w")

################################################################
# 展示希望新顏色色號(hào)
label2=tkinter.Label(root,text='請(qǐng)輸入希望新顏色的色號(hào):') #生成標(biāo)簽并將標(biāo)簽添加到主窗口
label2.grid(row=2,column=0)

labelR2=tkinter.Label(root,text='R:',width=5) #生成標(biāo)簽并將標(biāo)簽添加到主窗口
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) #生成標(biāo)簽并將標(biāo)簽添加到主窗口
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) #生成標(biāo)簽并將標(biāo)簽添加到主窗口
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日

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容