0

I am using TKinter and OpenCV to mutate and display images. When I use the code below the image displays in the window just fine.

canvasX = 1800
canvasY = 900

img = imread('C:/Images/Emu.jpg')
img = cvtColor(img, COLOR_BGR2RGB)
height, width, no_channels = img.shape

if __name__ == '__main__':

    window = tkinter.Tk()

    frame = Frame(window)
    frame.pack(fill=BOTH, expand=YES)

    img = scale(img, width=canvasX, height=canvasY)

    canvas = ResizingCanvas(frame, width=canvasX, height=canvasY, highlightthickness=0)
    canvas.pack(fill=BOTH, expand=YES)
    canvas.addtag_all('all')

    photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(img))

    canvas.create_image(0, 0, image=photo, anchor=NW)

    window.mainloop()

Working Image

When I take the section that draws the image and move it to its own function, then the image doesn't get drawn to the window and I am not sure why.

canvasX = 1800
canvasY = 900

img = imread('C:/Images/Emu.jpg')
img = cvtColor(img, COLOR_BGR2RGB)
height, width, no_channels = img.shape

def draw_canvas(img: Mat, frame: Frame, width: int, height: int):
    img = scale(img, width=canvasX, height=canvasY)
    canvas = ResizingCanvas(frame, width=canvasX, height=canvasY, highlightthickness=0)
    canvas.pack(fill=BOTH, expand=YES)
    canvas.addtag_all('all')

    photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(img))
    canvas.create_image(0, 0, image=photo, anchor=NW)

if __name__ == '__main__':
    window = tkinter.Tk()

    frame = Frame(window)
    frame.pack(fill=BOTH, expand=YES)

    draw_canvas(img, frame, canvasX, canvasY)

    window.mainloop()

Broken Image


I don't think that this code is related but I will supply it:

class ResizingCanvas(Canvas):
    def __init__(self, parent, **kwargs):
        Canvas.__init__(self, parent, **kwargs)
        self.bind("<Configure>", self.on_resize)
        self.height = self.winfo_reqheight()
        self.width = self.winfo_reqwidth()

    def on_resize(self, event):
        # determine the ratio of old width/height to new width/height
        canvasX = float(event.width)/self.width
        canvasY = float(event.height)/self.height
        self.width = event.width
        self.height = event.height
        # resize the canvas
        self.config(width=self.width, height=self.height)
        # rescale all the objects tagged with the "all" tag
        self.scale("all", 0, 0, canvasX, canvasY)


def scale(img: Mat, width: int = math.inf, height: int = math.inf):
    h, w, c = img.shape

    ratio = min(float(width) / float(w), float(height) / float(h))
    newWidth = round(w * ratio)
    newHeight = round(h * ratio)
    dim = (newWidth, newHeight)

    # resize image
    return resize(img, dim, interpolation=INTER_AREA).copy()
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • I'm not sure if it's relevant here, but when you reassign `img` in the function it doesn't affect the global variable with the same name. – Barmar Apr 18 '22 at 19:25
  • @Barmar yeah, that is how I was hoping that it would work. – Get Off My Lawn Apr 18 '22 at 19:29
  • But in your original code `img = scale(img, width=canvasX, height=canvasY)` *does* affect the global variable. So the two codes are not equivalent if any following code expects it to contain the scaled image. – Barmar Apr 18 '22 at 19:33
  • Doesn't python pass the value by reference? – Get Off My Lawn Apr 18 '22 at 19:34
  • Values are passed by reference, not variables. If you modify the object it will affect the original object, but assigning the variable doesn't. – Barmar Apr 18 '22 at 19:37
  • So, I removed parameters from the function(s), and have them all affect the global `img` (like in the working code), this still gives a blank output. So, I don't think the naming of the image variable is the issue here. – Get Off My Lawn Apr 18 '22 at 19:42
  • Did you add `global img` to the function, so assigning it affects the global variable? But I said I didn't think it was the cause of the problem. – Barmar Apr 18 '22 at 19:44
  • you need to make photo global, its being garbage collected once the function ends. – Art Apr 18 '22 at 23:53

0 Answers0