3 Star 0 Fork 0

lzq1357 / Modifying pictures

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
SplitFrame.py 8.94 KB
一键复制 编辑 原始数据 按行查看 历史
lzq1357 提交于 2021-08-01 17:37 . 背景替换修改
"""
背景分离:截图获取的图片,进行背景透明处理。(程序开发中,制作图标使用)
"""
from tkinter import *
from tkinter import colorchooser
import UI
from VariableFrame import VariableFrame
#
class SplitFrame(VariableFrame):
TYPE_FORE = 0
TYPE_BACK = 1
type = None
selectColor = (0, 0, 0)
newColor = None
selectColorText = None
selectColorLb = None
newColorBtn = None
colorDiff = 10
def __init__(self, canv, master=None):
VariableFrame.__init__(self, canv, master)
self.initView()
def setType(self, type):
self.type.set(type)
def transparentize(self):
bg_color= (255, 255, 255, 0) #(0, 0, 0, 0)
if self.type.get() == self.TYPE_BACK:
self.replaceColor(bg_color)
else:
self.replaceColor(bg_color, True)
pass
def onReplaceBtnClicked(self):
new_color = self.hex2rgba(self.newColor.get())
self.replaceColor(new_color, self.type.get() == self.TYPE_FORE)
pass
def replaceColor(self, new_color, contrary=False):
img = self.canv.img
selectColor = self.selectColor
old_seq = list(img.getdata())
if img.mode != "RGBA":
img = img.convert("RGBA")
img_seq = list(img.getdata())
diff = self.colorDiff
similar = True
for i in range(len(img_seq)):
similar = True
if isinstance(selectColor, tuple):
for j in range(len(selectColor)):
if abs(old_seq[i][j]-selectColor[j])>diff:
similar = False
break
pass
else:
similar = abs(img_seq[i]-selectColor)<=diff
if (not contrary) and similar:
img_seq[i] = new_color
elif contrary and (not similar):
img_seq[i] = new_color
pass
img.putdata(img_seq)
self.canv.refresh(img)
pass
def chooseColor(self):
colorDialog = colorchooser.Chooser(self.master)
rgb, str = colorDialog.show()
if str is None:
self.newColor.set("#ffffff00")
else:
self.newColor.set(str + "ff")
self.newColorBtn["bg"] = self.newColor.get()[0:7]
pass
def afterNewColorChanged(self, newColor):
self.newColorBtn["bg"] = newColor.get()[0:7]
pass
def onClickCanvas(self, event):
x0, y0 = self.canv.getCoord()
ex, ey = self.canv.realCoord(event.x, event.y)
x = int((ex - x0)/self.canv.scale)
y = int((ey - y0)/self.canv.scale)
self.selectColor = self.canv.img.getpixel((x, y))
hexColor = self.rgba2hex(self.selectColor)
self.selectColorLb["bg"] = hexColor[0:7]
self.selectColorText.set(hexColor)
pass
def rgba2hex(self, rgba):
color = '#'
if isinstance(rgba, tuple):
color += str( hex(rgba[0]) )[-2:].replace('x', '0').upper()
color += str(hex(rgba[1]))[-2:].replace('x', '0').upper()
color += str(hex(rgba[2]))[-2:].replace('x', '0').upper()
if len(rgba) == 4:
color += str(hex(rgba[3]))[-2:].replace('x', '0').upper()
else:
color += str( hex(rgba) )[-2:].replace('x', '0').upper()
color += str( hex(rgba) )[-2:].replace('x', '0').upper()
color += str( hex(rgba) )[-2:].replace('x', '0').upper()
return color
def hex2rgba(self, hex):
r = int("0x"+hex[1:3], 16)
g = int("0x"+hex[3:5], 16)
b = int("0x"+hex[5:7], 16)
if len(hex) < 9 :
a = 255
else :
a = int("0x"+hex[7:9], 16)
return (r,g,b,a)
pass
def show(self):
super(SplitFrame, self).show()
self.canv.bind("<Button-1>", self.onClickCanvas)
def setDiff(self, diffScale):
self.colorDiff = int(diffScale)
def initView(self):
bcolor = UI.bg
fcolor = UI.fg
w = UI.rightWidth-12
h = 25
f = UI.font
fy = 5 #first y
dy = h+8 #distance y
fx = 0
sx = 130 #second x
fontSize = UI.font[1]
i = 0
nameLb = Label(self, text='点击图片选中',
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=int(w / 2), height=h,
font=f) # 仅支持英语与数字
nameLb.place(x=fx, y=fy + i * dy)
self.selectColorLb = Label(self,
bg=self.rgba2hex(self.selectColor),
bitmap=UI.viewBmp, compound='left',
width=h-3, height=h-3)
self.selectColorLb.place(x=sx, y=fy + i * dy)
self.selectColorText = StringVar()
self.selectColorText.set("#000000")
selectColorEntry = Entry(self,
width=int(w/2 - h),
font=f,
textvariable=self.selectColorText)
selectColorEntry.place(x=sx+h+5, y=fy + i * dy)
i+=1
splitBtn = Label(self, text="选中近似颜色",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=int(w/2), height=h,
font=f)
splitBtn.place(x=fx, y=fy + i * dy)
self.diffScale = Scale(self,
from_=0, # 设置最小值
to=50, # 设置最大值
resolution=1, # 设置步距值
orient=HORIZONTAL, # 设置水平方向
borderwidth=0,
bg=bcolor, fg=fcolor,
command=self.setDiff)
self.diffScale.set(self.colorDiff)
self.diffScale.place(x=sx, y=fy + i * dy)
i += 2
typeLb = Label(self, text="选中颜色作为",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=w, height=h,
font=f)
typeLb.place(x=fx, y=fy + i * dy)
self.type = IntVar()
self.type.set(self.TYPE_BACK)
self.type.set(self.TYPE_BACK)
i += 1
fontRbtn = Radiobutton(self, text="前景",
bg=bcolor, fg=fcolor,
selectcolor=UI.radioSelectColor,
bitmap=UI.viewBmp, compound='left',
width=int(w/3), height=h,
font=f,
variable=self.type,
value=self.TYPE_FORE,
command=lambda: self.setType(self.TYPE_FORE))
fontRbtn.place(x=fx, y=fy + i * dy)
# i+=1
backRbtn = Radiobutton(self, text="背景",
bg=bcolor, fg=fcolor,
selectcolor=UI.radioSelectColor,
bitmap=UI.viewBmp, compound='left',
width=int(w/3), height=h,
font=f,
variable=self.type,
value=self.TYPE_BACK,
command=lambda: self.setType(self.TYPE_BACK))
backRbtn.place(x=sx, y=fy + i * dy)
i += 2
splitBtn = Button(self, text="背景透明",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=w, height=h,
font=f,
command=lambda: self.transparentize())
splitBtn.place(x=fx, y=fy + i * dy)
i+=2
self.newColor = StringVar()
self.newColor.set("#80a0d0ff")
replaceBtn = Button(self, text="替换背景颜色",
bg=bcolor, fg=fcolor,
bitmap=UI.viewBmp, compound='left',
width=int(w/2)-5, height=h,
font=f,
command=lambda: self.onReplaceBtnClicked())
replaceBtn.place(x=fx, y=fy + i * dy)
self.newColorBtn = Button(self,
bg="#8080d0",
bitmap=UI.viewBmp, compound='left',
width=h-3, height=h-3,
font=f,
command=lambda: self.chooseColor())
self.newColorBtn.place(x=sx, y=fy + i * dy)
newColorEntry = Entry(self,
width=int(w/2 - h),
font=f,
textvariable=self.newColor)
newColorEntry.place(x=sx+h+5, y=fy + i * dy)
self.newColor.trace("w", self.afterNewColorChanged(self.newColor))
pass
#
Python
1
https://gitee.com/lzq1357/Modifying-pictures.git
git@gitee.com:lzq1357/Modifying-pictures.git
lzq1357
Modifying-pictures
Modifying pictures
master

搜索帮助