0

I am new to tkinter. I found the following codes on stack overflow.

1: Magnifying glass

from PIL import Image, ImageTk

class LoadImage:
    def __init__(self,root):
        frame = Frame(root)
        self.canvas = Canvas(frame,width=900,height=900)
        self.canvas.pack()
        frame.pack()
        self.orig_img = Image.open('mappy.jpg').convert('RGB')
        self.img = ImageTk.PhotoImage(self.orig_img)
        self.canvas.create_image(0,0,image=self.img, anchor="nw")

        self.zoomcycle = 0
        self.zimg_id = None

        root.bind("<MouseWheel>",self.zoomer)
        self.canvas.bind("<Motion>",self.crop)

    def zoomer(self,event):
        if (event.delta > 0):
            if self.zoomcycle != 4: self.zoomcycle += 1
        elif (event.delta < 0):
            if self.zoomcycle != 0: self.zoomcycle -= 1
        self.crop(event)

    def crop(self,event):
        if self.zimg_id: self.canvas.delete(self.zimg_id)
        if (self.zoomcycle) != 0:
            x,y = event.x, event.y
            if self.zoomcycle == 1:
                tmp = self.orig_img.crop((x-45,y-30,x+45,y+30))
            elif self.zoomcycle == 2:
                tmp = self.orig_img.crop((x-30,y-20,x+30,y+20))
            elif self.zoomcycle == 3:
                tmp = self.orig_img.crop((x-15,y-10,x+15,y+10))
            elif self.zoomcycle == 4:
                tmp = self.orig_img.crop((x-6,y-4,x+6,y+4))
            size = 300,200
            self.zimg = ImageTk.PhotoImage(tmp.resize(size))
            self.zimg_id = self.canvas.create_image(event.x,event.y,image=self.zimg)

if __name__ == '__main__':
    root = Tk()
    root.title("Crop Test")
    App = LoadImage(root)
    root.mainloop()

2: Image that resizes with tkinter window:

from PIL import Image, ImageTk

class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.columnconfigure(0,weight=1)
        self.rowconfigure(0,weight=1)
        self.original = Image.open('example.png')
        self.image = ImageTk.PhotoImage(self.original)
        self.display = Canvas(self, bd=0, highlightthickness=0)
        self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG")
        self.display.grid(row=0, sticky=W+E+N+S)
        self.pack(fill=BOTH, expand=1)
        self.bind("<Configure>", self.resize)

    def resize(self, event):
        size = (event.width, event.height)
        resized = self.original.resize(size,Image.ANTIALIAS)
        self.image = ImageTk.PhotoImage(resized)
        self.display.delete("IMG")
        self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG")

root = Tk()
app = App(root)
app.mainloop()
root.destroy()

I was wondering if it is possible to combine the two codes? I have been trying for a while but I have not been able to do so. Any help and explanation would be greatly appreciated. Thanks in advance!

I found the codes on Tkinter & PIL Resize an image to fit a label and Adding Zooming in and out with a Tkinter Canvas Widget?

swj
  • 1
  • Yes it is possible. What have you tried exactly? The two codes are very similar, you need to merge the `App` and `LoadImage` classes, and rename the elements so that they have the same name (self.canvas <-> self.display, self.img <-> self.image...). – j_4321 Jul 28 '20 at 08:18
  • Hi! I basically copy and pasted the ```def resize(self, event) ```portion over to the magnifying glass code and I copied everything under the ```def__init__(self,root):``` Do I have to change the ```Frame.__init__(self)``` to ```frame = Frame(root)``` or something? I don't really know what is the difference. – swj Jul 28 '20 at 11:11
  • You should read about classes and OOP then. `App` inherits from `Frame`, so it is a variant of a tkinter frame while `LoadImage` isn't, the frame is an attribute instead. You need to pick one of the two structures and stick to it. `LoadImage`'s structure without inheritance is probably easier for you to understand as a beginner with OOP. – j_4321 Jul 28 '20 at 11:16
  • Also, you should edit your post to add the content of your comment inside the question to show more clearly what you tried. – j_4321 Jul 28 '20 at 11:18
  • Ok, I'll look into it, thank you! – swj Jul 29 '20 at 00:07

0 Answers0