Python Forum
[Tkinter] Plotting Raster Data / TIFF files on tkinter canvas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Plotting Raster Data / TIFF files on tkinter canvas
#1
Hi All....I have a TIFF file which I am trying to plot on the tkinter canvas using the following code:

import tkinter

from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

root = tkinter.Tk()

fig = Figure(figsize=(5, 4), dpi=100)

canvas1 = FigureCanvasTkAgg(fig, master=root)
canvas1.draw()

toolbar = NavigationToolbar2Tk(canvas1,root)
toolbar.update()
toolbar.pack(side=tkinter.TOP, fill=tkinter.X, padx=8)

canvas1.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1, padx=10, pady=5)

canvas1._tkcanvas.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1, padx=10, pady=5)

def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas1, toolbar)


canvas1.mpl_connect("key_press_event", on_key_press)

def _load():
    import rasterio as rio
    from rasterio.plot import show
    ax = fig.add_subplot(111)
    fig.subplots_adjust(bottom=0, right=1, top=1, left=0, wspace=0, hspace=0)

#    fig.tight_layout(pad=0)

    with rio.open(r'C:\Users\Desktop\t1.tif') as src_plot:
        show(src_plot, ax=ax, cmap='gist_gray')
    plt.close()
    ax.set(title="",xticks=[], yticks=[])
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["left"].set_visible(False)
    ax.spines["bottom"].set_visible(False)
    canvas1.draw()

button = tkinter.Button(master=root, text="Load", command=_load)
button.pack(side=tkinter.BOTTOM)

root.mainloop()
Issue which I am facing is that the raster/tiff file is getting plotted on the tkinter canvas, but it is placed inside the matplotlib figure window/frame whose size is not equal to tkinter canvas size. And hence if I am panning the figure inside the canvas, it is only moving inside the window and not across the canvas (as it used to be in ARCMAP/QGIS). Can someone help me out in resolving the issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 5,772 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 8,660 May-11-2021, 10:01 PM
Last Post: MrTim
  [Tkinter] Draw a grid of Tkinter Canvas Rectangles MrTim 5 7,758 May-09-2021, 01:48 PM
Last Post: joe_momma
Thumbs Up tkinter canvas; different page sizes on different platforms? philipbergwerf 4 4,034 Mar-27-2021, 05:04 AM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 17,548 Feb-06-2021, 03:35 PM
Last Post: joe_momma
  Reading coordinates in tiff images using PyQt hobbyist 21 7,393 Dec-22-2020, 11:12 PM
Last Post: hobbyist
  how to rotate lines in tkinter canvas helpmewithpython 1 3,373 Oct-06-2020, 06:56 PM
Last Post: deanhystad
  Tkinter function to clear old canvas and start a fresh "operation" zazas321 5 9,241 Oct-01-2020, 04:16 AM
Last Post: zazas321
  question on tkinter canvas PhotoImage gr3yali3n 1 2,104 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  TKinter coordinate system (canvas) Bhoot 1 2,897 Mar-23-2020, 04:30 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020