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)

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

    def update_enhance(self, value):
        self.value = eval(value)
        self.tkim.paste(self.enhancer.enhance(self.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)
    root.mainloop()
Last edited by Yoriz on Sun Jan 05, 2014 12:56 pm, edited 1 time in total.
Reason: first post lock
Chilcone
 
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

APP_WIN_XPOS = 0
APP_WIN_YPOS = 0
APP_WIN_WIDTH = 400
APP_WIN_HEIGHT = 400
FRAME_HEADER_FONT = ('Helevtica', 10, 'bold', 'underline')
FRAME_FONT_COLOR = 'steelblue'

class ImageViewer(object):
   
    def __init__(self, title):
        self.win = tk.Tk()
        self.win.title(title)
        self.win.geometry('{0}x{1}+{2}+{3}'.format(APP_WIN_WIDTH,
            APP_WIN_HEIGHT, APP_WIN_XPOS, APP_WIN_YPOS))
        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)
       
        self.tk_image.paste(self.image)
        scale.set(1.5)

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

wuf ;)
wuf
 
Posts: 45
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 :)
Chilcone
 
Posts: 6
Joined: Sun Jan 05, 2014 12:33 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 5 guests