PIL/tkinter - image viewer application

Post here if you need help with creating a Graphical User Interface in Python.

PIL/tkinter - image viewer application

Postby Chilcone » Sun Jan 05, 2014 12:38 pm

Hi ! Coding my simple image viewer, where I have an image and can modify its brightness and contrast by scales, I came to problem with Enhance class that I can't solve. It doesn't give me any error, but it doesn't work as I want. I want simply the scale to adjust brightness when I move with it. I haven't implemented second Enhance class, just want to make the first Enhance class and scale work on the image.
Appreciate your help :)

Code: Select all
import tkinter as tk
from PIL import Image, ImageTk, ImageEnhance

class ImageViewer(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master, background="green")

        # for now, don't use images.
        self.im = Image.open("plant.jpg") #choose your picture
        self.tkim = ImageTk.PhotoImage(self.im)

        # these three widgets make up our main layout
        label = tk.Label(self, image=self.tkim, text="label")
        e = Enhance(self,self.im, ImageEnhance.Brightness)
        e1 = Enhance1(self, self.im)

        label.pack(side="bottom", fill="both", expand=True)
        e.pack(side="left", fill="both", expand=True)
        e1.pack(side="right", fill="both", expand=True)

class Enhance(tk.Frame):
    def __init__(self, master,image, enhancer):
        tk.Frame.__init__(self, master)
        self.image = image
        self.tkim = ImageTk.PhotoImage(image.mode, image.size)
        self.enhancer = enhancer(image)

        s = tk.Scale(self, label="Brightness", orient=tk.VERTICAL,from_=3.0, to=-1.0, resolution=0.01,command=self.update_enhance)
        s.pack(side = "left", fill = "both", expand = True)

    def update_enhance(self, value):
        self.value = eval(value)

    # width, height, and color are only temporary, they
    # make it easy to see the frames before they have
    # any content

class Enhance1(tk.Frame):
    def __init__(self, master, image):
       self.image = image

# width, height, and color are only temporary, they
# make it easy to see the frames before they have
# any content
       tk.Frame.__init__(self, master, background="blue", width=100, height=100)

if __name__ == "__main__":
    root = tk.Tk()
    ImageViewer(root).pack(fill="both", expand=True)
Last edited by Yoriz on Sun Jan 05, 2014 12:56 pm, edited 1 time in total.
Reason: first post lock
Posts: 6
Joined: Sun Jan 05, 2014 12:33 pm

Re: PIL/tkinter - image viewer application

Postby wuf » Sat Jan 18, 2014 11:48 am

Hi Chilcone

Hier one solution for your Image-Viewer:
Code: Select all
import tkinter as tk
from PIL import Image, ImageTk, ImageEnhance

FRAME_HEADER_FONT = ('Helevtica', 10, 'bold', 'underline')
FRAME_FONT_COLOR = 'steelblue'

class ImageViewer(object):
    def __init__(self, title):
        self.win = tk.Tk()
        self.win.protocol("WM_DELETE_WINDOW", self.close)

        self.main_frame = tk.Frame(self.win)
        self.main_frame.pack(fill='both', expand=True)

        scale_frame = tk.LabelFrame(self.main_frame, text="Brightness:",
            font=FRAME_HEADER_FONT,fg=FRAME_FONT_COLOR, bd=0, padx=4,
            pady=4, labelanchor='n')
        scale_frame.pack(side='left', fill='y', padx=4, pady=4)
        image_frame = tk.LabelFrame(self.main_frame, text="Image:",
            font=FRAME_HEADER_FONT, fg=FRAME_FONT_COLOR, bd=0, padx=4,
            pady=4, labelanchor='n')
        image_frame.pack(side='left', fill='both', expand=True,
            padx=4, pady=4)

        scale = tk.Scale(scale_frame, orient=tk.VERTICAL, from_=3.0,
            to=0.0, resolution=0.01, command=self.update_enhance)
        scale.pack(side = "left", fill = "both", expand = True)

        self.image = Image.open("plant.jpg")
        self.brightness_enhancer = ImageEnhance.Brightness(self.image)
        self.tk_image = ImageTk.PhotoImage('RGBA', self.image.size)

        image_label = tk.Label(image_frame, image=self.tk_image)
        image_label.pack(fill='both', expand=True)

    def update_enhance(self, value):
        enhanced_image = self.brightness_enhancer.enhance(float(value))
    def run(self):
    def close(self):
        print("Do something before shutdown")
ImageViewer("Image Viewer").run()

wuf ;)
Posts: 37
Joined: Fri Feb 08, 2013 6:42 am

Re: PIL/tkinter - image viewer application

Postby Chilcone » Sat Jan 18, 2014 8:56 pm

Hello wuf
That works, but I would like to know, how can I update the image with two scales, one for brightness and another for contrast. I've tried to create second update_enhance function and second scale, but when I modify the image with brightness scale and then with contrast scale image is always as a new, so I think I will need to somehow save the image after I modify it with one scale and then use the second scale to update that saved image, but just can't find a way to do it.
Thanks for help :)
Posts: 6
Joined: Sun Jan 05, 2014 12:33 pm

Return to GUI

Who is online

Users browsing this forum: No registered users and 3 guests