如何在图片上同时添加多种效果,例如对比度和亮度 [英] How to add multiple effects to a picture such as contrast and brightness at the same time

查看:116
本文介绍了如何在图片上同时添加多种效果,例如对比度和亮度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python创建了一个简单的程序来更改图片的对比度,亮度颜色等.首先,我仅添加了一种效果,并且效果很好,我设法将其与Scaler链接在一起.然后,我尝试同时添加了与定标器相关联的多种效果,但是当我尝试在图片上添加多种效果(例如,同时使用对比度和亮度)时,屏幕变灰,或者什么也没发生图片.

I created a simple program using python to change picture Contrast,Brightness color and etc. First I just added one effect, and it worked well, I managed to link it with a Scaler. Then I tried to add multiple effect at once which I've also linked with scalers, but when I try to add multiple effects on the picture (e.g Contrast and brightness at the same time) I got a grey Screen, or just nothing happened to the picture.

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()

推荐答案

欢迎您! 首先,您不必为所有按钮,文本小部件等使用config-您只需在创建小部件时将所有这些选项作为参数即可,例如

Welcome to SO! First of all, you don't have to use config for all your Buttons, Text widgets and so on - you can simply give all those options as arguments when you're creating a widget, e.g.

textCon = Text(window2, width=8, height=1, state=DISABLED, background="#EBEBEB", font="Roboto")

这使您的代码更短,更简单,更快. 您在onClickDisplay中所做的事情由于简单的原因而无法正常工作.您正在使用and(这是一个布尔运算符)来尝试使多个事情同时发生-这与and无关.这就是我要重写您的代码的方式:

This makes your code shorter, simpler and faster. What you're doing in onClickDisplay does not work for a simple reason. You are using and, which is a boolean operator, to try to make multiple things happen at once - which is not what and is about. This is how i would rewrite your code:

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只是将小部件一个接一个地放置

By defining a class, you can work around having to use global variables. Opening two windows is not necessary in your case, as you can just add the button for choosing the image file to your other window. I would recommend using place() instead of pack(), as it will allow you to define exact x and y coordinates for your different widgets inside the window, which will make it look more structured - pack simply places widgets one after another.

这篇关于如何在图片上同时添加多种效果,例如对比度和亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆