【问题标题】:How to change contrast of PIL image with a tkinter slider but not set it?如何使用 tkinter 滑块更改 PIL 图像的对比度但不设置它?
【发布时间】:2020-07-21 00:45:31
【问题描述】:

我正在尝试使用 python 中的 PIL 库制作一个图像编辑器,并使用 tkinter 显示。我正在尝试制作一个可以改变图像对比度的滑块。

但是,使用我当前的代码,图像的对比度只能增加而不能改变。例如,如果我希望图像有更多的对比度,我可以使用滑块来增加它。但是,这并不能让我恢复对比度。有没有办法让我改变图像的对比度?

完整代码:

from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageEnhance, ImageTk


def update_image():
    global img, adapted_img
    adapted_img = ImageTk.PhotoImage(img)
    image_container.create_image(0, 0, image=adapted_img, anchor=NW)


def open_image():
    global img
    try:
        img = Image.open(
            filedialog.askopenfilename(title="Select file", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*"))))
        save_button.config(bg=default_color)
        flip_horizontal_button.config(bg=default_color)
        flip_vertical_button.config(bg=default_color)
        contrast_slider.config(bg=default_color)
        update_image()
    except:
        pass


def flip_horizontal():
    global img
    if img:
        img = img.transpose(Image.FLIP_LEFT_RIGHT)
        update_image()


def flip_vertical():
    global img
    if img:
        img = img.transpose(Image.FLIP_TOP_BOTTOM)
        update_image()


def save():
    global img
    if img:
        ext = StringVar()
        name = filedialog.asksaveasfilename(initialfile="Untitled", title="Select file", typevariable=ext, filetypes=(
            ('JPEG', ('*.jpg', '*.jpeg', '*.jpe')), ('PNG', '*.png'), ('GIF', '*.gif')))
        if name:
            img.save(name + "." + ext.get().lower())  # splice the string and the extension.


def change_contrast(var):
    global img, contrast
    if img:
        contraster = ImageEnhance.Contrast(img)
        contrast = var
        img = contraster.enhance(int(var))
        update_image()


root = Tk()
root.title("Image Editor")
root.geometry('600x500')
default_color = root.cget('bg')

img = None
contrast = 1

open_button = Button(text='Open Image', font=('Arial', 20), command=open_image)
flip_horizontal_button = Button(text='Flip Horizontal', font=('Arial', 10), command=flip_horizontal, bg="gray",
                                width=15)
flip_vertical_button = Button(text='Flip Vertical', font=('Arial', 10), command=flip_vertical, bg="gray", width=15)
contrast_slider = Scale(from_=0, to=100, orient=HORIZONTAL, bg="gray", command=change_contrast)
save_button = Button(text='Save', font=('Arial', 20), command=save, bg="gray")
image_container = Canvas(root, borderwidth=5, relief="groove", width=300, height=300)

image_container.pack(fill="both", expand="yes", anchor='nw', side=BOTTOM)
open_button.pack(anchor='nw', side=LEFT)
save_button.pack(anchor='nw', side=LEFT)
contrast_slider.pack(anchor='w', side=LEFT)
flip_horizontal_button.pack(anchor='w')
flip_vertical_button.pack(anchor='w')

root.mainloop()

【问题讨论】:

  • 在更改之前保存图像的副本。

标签: python tkinter python-imaging-library


【解决方案1】:

对比度是一个有损、不可逆的过程。您的其他函数可以正常工作,因为它们是它们自己的无损逆函数。如果您想要对比工作,您需要保留原始未修改并仅显示修改后的版本。每次应用对比度时,它都会应用于图像的未修改版本。

from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageEnhance, ImageTk


def update_image():
    global img, adapted_img,contrast_val
    contrast = ImageEnhance.Contrast(img)
    imgMod = contrast.enhance((contrast_val-50)/25. +0.5)

    adapted_img = ImageTk.PhotoImage(imgMod)
    image_container.create_image(0, 0, image=adapted_img, anchor=NW)


def open_image():
    global img
    try:
        img = Image.open(
            filedialog.askopenfilename(title="Select file", filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*"))))
        save_button.config(bg=default_color)
        flip_horizontal_button.config(bg=default_color)
        flip_vertical_button.config(bg=default_color)
        contrast_slider.config(bg=default_color)
        update_image()
    except:
        pass


def flip_horizontal():
    global img
    if img:
        img = img.transpose(Image.FLIP_LEFT_RIGHT)
        update_image()


def flip_vertical():
    global img
    if img:
        img = img.transpose(Image.FLIP_TOP_BOTTOM)
        update_image()


def save():
    global img
    if img:
        ext = StringVar()
        name = filedialog.asksaveasfilename(initialfile="Untitled", title="Select file", typevariable=ext, filetypes=(
            ('JPEG', ('*.jpg', '*.jpeg', '*.jpe')), ('PNG', '*.png'), ('GIF', '*.gif')))
        if name:
            img.save(name + "." + ext.get().lower())  # splice the string and the extension.


def change_contrast(var):
    global img, contrast_val
    contrast_val =int(var)
    update_image()


root = Tk()
root.title("Image Editor")
root.geometry('600x500')
default_color = root.cget('bg')

img = None
contrast_val = 50

open_button = Button(text='Open Image', font=('Arial', 20), command=open_image)
flip_horizontal_button = Button(text='Flip Horizontal', font=('Arial', 10), command=flip_horizontal, bg="gray",
                                width=15)
flip_vertical_button = Button(text='Flip Vertical', font=('Arial', 10), command=flip_vertical, bg="gray", width=15)
contrast_slider = Scale(from_=0, to=100, orient=HORIZONTAL, bg="gray", command=change_contrast)
save_button = Button(text='Save', font=('Arial', 20), command=save, bg="gray")
image_container = Canvas(root, borderwidth=5, relief="groove", width=300, height=300)

image_container.pack(fill="both", expand="yes", anchor='nw', side=BOTTOM)
open_button.pack(anchor='nw', side=LEFT)
save_button.pack(anchor='nw', side=LEFT)
contrast_slider.pack(anchor='w', side=LEFT)
flip_horizontal_button.pack(anchor='w')
flip_vertical_button.pack(anchor='w')

root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2014-06-19
    相关资源
    最近更新 更多