我使用 python 创建了一个简单的程序来更改图片对比度、亮度颜色等。首先,我只添加了一个效果,效果很好,我设法将其与缩放器链接。然后我尝试一次添加多种效果,我也与缩放器链接,但是当我尝试在图片上添加多种效果(例如同时对比度和亮度)时,我得到了一个灰色的屏幕,或者什么也没发生图片。
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from PIL import Image
from PIL import ImageEnhance
def main():
window = Tk()
window.geometry("400x290")
window.configure(background="#EBEBEB")
OpenB = ttk.Button(window, text="Import Photo")
OpenB.pack()
def onClickImport():
askimage = askopenfilename()
global img
img=Image.open(askimage)
OpenB.config(command=onClickImport)
window.mainloop()
window2 = Tk()
window2.geometry("400x290")
window2.configure(background="#EBEBEB")
DisplayButton=ttk.Button(window2,text="Show")
DisplayButton.pack()
ScalerContrast= ttk.Scale(window2, from_=1.0, to_=5.0)
ScalerContrast.pack()
ScalerBrightness = ttk.Scale(window2, from_=1.0, to_=5.0)
ScalerBrightness.pack()
ScalerColor = ttk.Scale(window2, from_=1, to_=100)
ScalerColor.pack()
ScalerSharpness = ttk.Scale(window2, from_=1, to_=100)
ScalerSharpness.pack()
textCon=Text(window2,width=8,height=1)
textCon.insert(END,"Contrast")
textCon.config(state=DISABLED)
textCon.configure(background="#EBEBEB")
textCon.configure(font="Roboto")
textCon.pack()
textBr=Text(window2,width=8,height=1)
textBr.insert(END,"Brightness")
textBr.config(state=DISABLED)
textBr.configure(background="#EBEBEB")
textBr.configure(font="Roboto")
textBr.pack()
textCor=Text(window2,width=8,height=1)
textCor.insert(END,"Color")
textCor.config(state=DISABLED)
textCor.configure(background="#EBEBEB")
textCor.configure(font="Roboto")
textCor.pack()
textSh=Text(window2,width=8,height=1)
textSh.insert(END,"Sharpness")
textSh.config(state=DISABLED)
textSh.configure(background="#EBEBEB")
textSh.configure(font="Roboto")
textSh.pack()
converter = ImageEnhance.Contrast(img)
converter1= ImageEnhance.Brightness(img)
converter2= ImageEnhance.Color(img)
converter3= ImageEnhance.Sharpness(img)
def onClickDisplay():
img2=converter.enhance(ScalerContrast.get()) and converter1.enhance(ScalerBrightness.get()) and\
converter2.enhance(ScalerColor.get()) and converter3.enhance(ScalerColor.get())
img2.show()
DisplayButton.config(command=onClickDisplay)
window2.mainloop()
if __name__=='__main__':
main()
最佳答案
欢迎来到SO!
首先,您不必为所有按钮、文本小部件等使用 config
- 您可以在创建小部件时简单地将所有这些选项作为参数提供,例如
textCon = Text(window2, width=8, height=1, state=DISABLED, background="#EBEBEB", font="Roboto")
这使您的代码更短、更简单、更快。
您在 onClickDisplay
中所做的操作不起作用,原因很简单。您正在使用 and
( bool 运算符)来尝试同时发生多个事情 - 这不是 and
的目的。这就是我重写你的代码的方式:
class CustomImageEnhancer()
def __init__(self):
def onClickImport():
askimg = fd.askopenfilename()
self.img = Image.open(askimage)
return self.img
def converted_image(img_a, contrast, brightness, color, sharpness):
contrast_converter = ImageEnhance.Contrast(img_a)
img_b = contrast_converter.enhance(contrast)
brightness_converter = ImageEnhance.Brightness(img_b)
img_c = brightness_converter.enhance(brightness)
color_converter = ImageEnhance.Color(img_c)
img_d = color_converter.enhance(color)
sharpness_converter = ImageEnhance.Sharpness(img_d)
img_final = sharpness_converter.enhance(sharpness)
return img_final
def onClickDisplay():
cont = ScalerContrast.get()
bright = ScalerBrightness.get()
col = ScalerColor.get()
sharp = ScalerSharpness.get()
img = self.img
new_img = converted_image(img, cont, bright, col, sharp)
new_img.show()
root = Tk()
OpenB = ttk.Button(root, text="Import Photo", command=onClickImport)
OpenB.pack()
DisplayButton=ttk.Button(root, text="Show", command=onClickDisplay)
ScalerContrast= ttk.Scale(root, from_=1.0, to_=5.0)
ScalerBrightness = ttk.Scale(root, from_=1.0, to_=5.0)
ScalerColor = ttk.Scale(root, from_=1, to_=100)
ScalerSharpness = ttk.Scale(root, from_=1, to_=100)
DisplayButton.pack()
ScalerContrast.pack()
ScalerBrightness.pack()
ScalerColor.pack()
ScalerSharpness.pack()
textCon=Text(root, width=8, height=1, state=DISABLED, background="#EBEBEB", font='Roboto')
textCon.insert(END, 'Contrast')
textCon.pack()
textBr=Text(root, width=8, height=1, state=DISABLED, background='#EBEBEB', font='Roboto')
textBr.insert(END, 'Brightness')
textBr.pack()
textCor=Text(root, width=8, height=1, state=DISABLED, background='#EBEBEB', font='Roboto')
textCor.insert(END, 'Color')
textCor.pack()
textSh=Text(root, width=8, height=1, state=DISABLED, background='#EBEBEB', font='Roboto')
textSh.insert(END, 'Color')
textSh.pack()
root.mainloop()
window=CustomImageEnhancer()
通过定义类,您可以解决必须使用全局变量的问题。在您的情况下,不需要打开两个窗口,因为您只需将用于选择图像文件的按钮添加到另一个窗口即可。我建议使用 place()
而不是 pack()
,因为它允许您为窗口内的不同小部件定义精确的 x 和 y 坐标,这将使它看起来更加结构化 - pack
只是将小部件一个接一个地放置。
关于python - 如何同时给图片添加对比度、亮度等多种效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53885575/