0

I want to open the image with matplotlib , then embed it into matplotlib plot and then display in Tkinter widget. I know how to display matplotlib graph in Tkinter, but don't know how to do it with the image. Here my code:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from numpy import array, arange, sin, pi

root = Tk()
root_panel = Frame(root)
root_panel.pack(side="bottom", fill="both", expand="yes")

btn_panel = Frame(root_panel, height=35)
btn_panel.pack(side='top', fill="both", expand="yes")

img_arr = mpimg.imread('img/pic.jpg')
imgplot = plt.imshow(img_arr)

#here is the example of how I embed matplotlib graph in Tkinter,
#basically, I want to do the same with the image (imgplot)
f = Figure()
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
a.plot(t, s)

canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side="top", fill="both", expand=1)
canvas._tkcanvas.pack(side="top", fill="both", expand=1)

root.mainloop()
2
  • 1
    Does replacing a.plot(t, s) by a.imshow(img_arr) do what you want? Commented Nov 27, 2017 at 19:12
  • yes, sorry for my stupidity:D Commented Nov 27, 2017 at 19:13

2 Answers 2

4

To display the picture in the matplotlib figure embeded inside the Tkinter window, all there is to do is replace a.plot(t, s) by a.imshow(img_arr) in the code given in the question.

Sign up to request clarification or add additional context in comments.

Comments

1
canvas.show() 

gives an error : AttributeError: 'FigureCanvasTkAgg' object has no attribute 'show'

Instead of canvas.show() write canvas.draw() .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.