#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Tkinter/PIL - image viewer application


    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. Thank you
    Here's the code :

    Code:
    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")
            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()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    530
    Rep Power
    34
    I would suggest that you open a new Toplevel with the scale when the brightness button is pushed. You would also have to have a button in the Toplevel that is pushed when the desired level is displayed on the scale. The variable 'factor' in the code below would then be set to that value.

    Some problems with your code: first, you should provide a full path to the image file. You can't assume it is always in your PYTHONPATH.

    Next, e and e1 are class instances, not Tkinter widgets, so packing them will lead to unknown results.
    Code:
            e.pack(side="left", fill="both", expand=True)
            e1.pack(side="right", fill="both", expand=True)
    It is not necessary to create a separate class to enhance the image, a function will do.
    Code:
    try:
        import Tkinter as tk     ## Python 2.x
    except ImportError:
        import tkinter as tk     ## Python 3.x
    
    from PIL import Image, ImageTk, ImageEnhance
    
    class ImageViewer():
        def __init__(self, master):
            self.fr=tk.Frame(master, background="green")
            self.fr.pack()
    
            image_path_name = "/python/Tkinter/Grayson/Examples/Chapter08/images/boat.jpg"
            self.im = Image.open(image_path_name)
    ##        self.im = Image.open("plant.jpg")
            self.tkim = ImageTk.PhotoImage(self.im)
    
            self.label = tk.Label(self.fr, image=self.tkim, text="label")
            self.label.pack(side="top", fill="both", expand=True)
    
            tk.Button(self.fr, text="Enhance Brightness", \
                      command=self.enhance_brightness).pack(side="left")
    
            tk.Button(self.fr, text="Exit", \
                      command=master.quit).pack(side="left")
    
    ##        e = Enhance(self,self.im, ImageEnhance.Brightness)
    ##        e1 = Enhance1(self, self.im)
    ##        e.pack(side="left", fill="both", expand=True)
    ##        e1.pack(side="right", fill="both", expand=True)
    
        def enhance_brightness(self):
            """ enhance the brightness to a constant value of the variable 'factor' 
            """
    
            ## display the scale.  You will have to do the rest
            scale_window = tk.Toplevel(self.fr)
            s = tk.Scale(scale_window, label="Brightness", orient=tk.VERTICAL, \
                from_=3.0, to=-1.0, resolution=0.01)
            s.pack(side = "left", fill = "both", expand = True)
    
            factor=1.5
            print "brightness", factor
            enhancer = ImageEnhance.Brightness(self.im)
            img_enhanced = enhancer.enhance(factor)
            self.new_image=ImageTk.PhotoImage(img_enhanced)
            self.label.config(image=self.new_image)
    
    master=tk.Tk()
    IV=ImageViewer(master)
    master.mainloop()
    Last edited by dwblas; January 5th, 2014 at 05:19 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Can't do it


    Hi !
    After some time, I looked at the code and tried to make it work. Unfortunately, I just can't make the scale work. I've tried to create another function to update the scale, but it's not probably necessary. I would be thankful if u gave me some hint, so you won't think that I didn't do anything.
    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo