Python 如何使用tkinter滑块更改PIL图像的对比度,但不设置它?
我试图用python中的PIL库制作一个图像编辑器,该库用tkinter显示。我正在尝试制作一个可以改变图像对比度的滑块Python 如何使用tkinter滑块更改PIL图像的对比度,但不设置它?,python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,我试图用python中的PIL库制作一个图像编辑器,该库用tkinter显示。我正在尝试制作一个可以改变图像对比度的滑块 from tkinter import * from tkinter import filedialog from PIL import Image, ImageEnhance, ImageTk def update_image(): global img, adapted_img,contrast_val contrast = ImageEnhance.
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()
但是,在我当前的代码中,图像的对比度只能增加,不能改变。例如,如果我希望图像有更多的对比度,我可以使用滑块来增加对比度。但是,这不允许我恢复对比。有没有一种方法可以让我改变图像的对比度
完整代码:
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()
对比度是一个有损的、不可逆的过程。其他函数工作正常,因为它们是自己的无损逆函数。如果要使对比度正常工作,则需要保留原始未修改版本,并仅显示修改后的版本。每次应用对比度时,它都应用于未修改的图像版本
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()
对比度是一个有损的、不可逆的过程。其他函数工作正常,因为它们是自己的无损逆函数。如果要使对比度正常工作,则需要保留原始未修改版本,并仅显示修改后的版本。每次应用对比度时,它都应用于未修改的图像版本
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()
在更改图像之前保存图像副本。在更改图像之前保存图像副本。