Python 3.x tkinter, PIL unable to show large image, image automatically cut

210 views Asked by At

This is my first experience with tkinter package. I'm trying to show a large image (5000, 5000) on a small window using x and y scrolls to explore the whole image. By following this answer, I have implemented a ScrollabarImage:

import tkinter 
from PIL import Image, ImageTk
class ScrollableImage(tkinter.Canvas):
    def __init__(self, master=None, **kw):
        self.image = kw.pop('image', None)
        super(ScrollableImage, self).__init__(master=master, **kw)
        self['highlightthickness'] = 0
        self.propagate(0)  # wont let the scrollbars rule the size of Canvas
        self.create_image(0,0, anchor='nw', image=self.image)
        # Vertical and Horizontal scrollbars
        self.v_scroll = tkinter.Scrollbar(self, orient='vertical', width=6)
        self.h_scroll = tkinter.Scrollbar(self, orient='horizontal', width=6)
        self.v_scroll.pack(side='right', fill='y')
        self.h_scroll.pack(side='bottom', fill='x')
        # Set the scrollbars to the canvas
        self.config(xscrollcommand=self.h_scroll.set, 
                yscrollcommand=self.v_scroll.set)
        # Set canvas view to the scrollbars
        self.v_scroll.config(command=self.yview)
        self.h_scroll.config(command=self.xview)
        # Assign the region to be scrolled 
        self.config(scrollregion=self.bbox('all'))

        self.focus_set()
        self.bind_class(self, "<MouseWheel>", self.mouse_scroll)

    def mouse_scroll(self, evt):
        if evt.state == 0 :
            # self.yview_scroll(-1*(evt.delta), 'units') # For MacOS
            self.yview_scroll( int(-1*(evt.delta/120)) , 'units') # For windows
        if evt.state == 1:
            # self.xview_scroll(-1*(evt.delta), 'units') # For MacOS
            self.xview_scroll( int(-1*(evt.delta/120)) , 'units') # For windows

And in the main:

if __name__ == "__main__":
    hdul = fits.open('gputest.fits')
    data = hdul[1].data
    header = hdul[1].header

    img = Image.fromarray(data, mode='F')

    root = tkinter.Tk()
    root.geometry("1000x1000") 
    root.resizable(0, 0) #Don't allow resizing in the x or y direction

    show_image = ScrollableImage(root, image=img)
    show_image.pack()

    root.mainloop()

But the result is completely wrong: result

The problem is that the image is not complete, it had been cut. Even if the scrolls works, I can not move on the whole image, only a cutout has been displayed. Why this happens?

I would like to: expand the image to the whole window dimension, keeping the scrolls since the image is larger than the window size. I tried to fined an explanation and correct the code, but I was not able to find a solution, maybe because I'm quite new to tkinter.

0

There are 0 answers