修改图片颜色小工具V1.3

工具更健壮,更新日志后续再补充,源码已复制更新至V1.3!

Changelog

2023年1月20日13:57:37

  1. 增加去背景功能,可以保存成透明背景的png格式。
  2. 排版的代码从grid改为place方式。
  3. 增加menubar,帮助。
  4. 整理部分代码结构,减少了函数传参,采用字典方式。

功能介绍

开始界面.png
取色后自动输入.png
预览变成新窗口.png

选择一张图片,然后输入希望改变哪种颜色的色号,然后输入希望变成新颜色的色号。然后点击提交,就可以完成图片预览和保存功能。此功能和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()


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,692评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,482评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,995评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,223评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,245评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,208评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,091评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,929评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,346评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,570评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,739评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,437评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,037评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,677评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,833评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,760评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,647评论 2 354

推荐阅读更多精彩内容