Below is en2.py code-

    # The Python Imaging Library
    # $Id$
    # this demo script creates four windows containing an image and a slider.
    # drag the slider to modify the image.
    #

    from Tkinter import *
    from PIL import Image, ImageTk, ImageEnhance
    import sys

     # enhancer widget

    class Enhance(Frame):
        def __init__(self, master, name, enhancer, lo, hi):
            Frame.__init__(self, master)

            # set up the image

            self.tkim = ImageTk.PhotoImage(image.mode, image.size)
            self.enhancer = enhancer(image)
            #mid = (lo + hi)/2
            self.update('1.0') # normalize

            # image window
            Label(self, image=self.tkim).grid()

            # scale
            s = Scale(self, label=name, orient=HORIZONTAL,
                      from_=lo, to=hi, resolution=0.01,
                      command=self.update)
            #if image == "ip_img.jpg" or image == "not_img.jpg":
             #   s.config(state=DISABLED)
            s.set(self.value)
            s.grid(row=0,column=0)

        def update(self, value):
            self.value = eval(value)
            tkim.paste(self.enhancer.enhance(self.value))
  • Now in another code below, I have to write the code that resizes en2.image or another ImageTk.PhotoImage instance that contains a copy of en2.image.

    ####zoom in photoimage instance of en2.image code here

    Label(top, image=en2.tkim).grid(row=0, column=10, columnspan=20, rowspan=20,padx=10, sticky=W+E+N+S)

    im = Image.open("zoomin.jpg")
    image = im.resize((80,30),Image.ANTIALIAS)
    photo = ImageTk.PhotoImage(image)
    b=Button(root, image=photo,command=zoomin)
    b.grid(row=5,column=0)

    root.mainloop()

    I've googled and some documents show that there is a zoom method that can be used with photoimage instance but it gives attribute error on the interpreter.
    I tried to change the height and width explicitly using en2.image.height=900 (say), but even thats not working, it does not raise an error, but at the same time, it does not alter the height.

Please tell me a way so that I can resize en2.image.

Dani AI

Generated

Key points that often trip people up here:

  • ImageTk.PhotoImage does not implement .zoom() or .subsample(). Those belong to tkinter.PhotoImage and only support integer scaling. With PIL/Pillow you must resize the underlying Image and then create a new ImageTk.PhotoImage.
  • Setting attributes like .width/.height on a PhotoImage does nothing. You need to compute a new size and recreate the image.
  • Always keep a Python reference to the PhotoImage you set on a widget (for example, label.image = tkimg) or Tk will garbage-collect it and the display will not update.

Example of a safe zoom function using PIL/Pillow and updating a Label:

# src: the original PIL.Image you want to display
# label: the Label currently showing an ImageTk.PhotoImage built from src
def zoom_label(label, src, scale):
    w, h = src.size
    resample = getattr(Image, "LANCZOS", Image.ANTIALIAS)  # Pillow >=3.4 vs old PIL
    zoomed = src.resize((int(w * scale), int(h * scale)), resample)
    tkimg = ImageTk.PhotoImage(zoomed)
    label.configure(image=tkimg)
    label.image = tkimg  # keep a reference

If you are enhancing first, apply the enhancer to the original Image, then resize the result each time to avoid cumulative quality loss. Also avoid eval when reading Scale values; use float(value).

Official references: the Pillow docs for Image.resize and Tk’s photo image manual (which documents zoom/subsample for PhotoImage) at tcl.tk.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.