For duplicate-markers: I am fully aware that there are some similar questions in matplotlib
, such as this one. My question is about
What you seem to be asking for is a 2D World to Viewport Transformation:
Take an area defined in "world coordinates" (say 10 metres by 10 metres) and map it to an area defined in canvas coordinates.
eg.
from tkinter import *
xmin,ymin,xmax,ymax = 0,0,10,10 # world
umin,vmin,umax,vmax = 0,480,640,0 # viewport (note: y reversed)
points = [(2,2), (4,4), (7,7), (8,8)] # some "world" points
def world_to_viewport(worldpoint):
x,y = worldpoint
u = (x - xmin)*((umax - umin)/(xmax - xmin)) + umin
v = (y - ymin)*((vmax - vmin)/(ymax - ymin)) + vmin
return u,v
def pixel_to_world(pixel):
u,v = pixel
x = (u - umin)*((xmax - xmin)/(umax - umin)) + xmin
y = (v - vmin)*((ymax - ymin)/(vmax - vmin)) + ymin
return x,y
root = Tk()
canvas = Canvas(root, width=640, height=480, bd=0, highlightthickness=0)
canvas.pack()
def on_click(event):
root.title('%s,%s' %(pixel_to_world((event.x,event.y))))
canvas.bind('<ButtonPress-1>', on_click)
r = 5
for point in points:
cx,cy = world_to_viewport(point)
canvas.create_oval(cx-r,cy-r,cx+r,cy+r,fill='red')
root.mainloop()
It seems to me that this would be simple with code like the below:
import Tkinter
#Set up a basic canvas
top = Tkinter.Tk()
canv = Tkinter.Canvas(top, bg="brown", height=250, width=300)
#Replace with what ever values you want
x = 50
y = 50
#Draw the first dot
line1 = canv.create_line(x, y, x - 2, y - 2, fill="green", width=3)
#This next line is pretty much all it takes to find the Y inverse
y = canv.winfo_reqheight() - y
#Draw the second dot
line2 = canv.create_line(x, y, x - 2, y - 2, fill="green", width = 3)
canv.pack()
top.mainloop()
This returns the following:
Basically all I did was get the canvas height (250), and subtract the previous Y value (50) from it, which returned the Y inverse (200). Not exactly a built in function, but the actual flipping part was very simple. Hope that was what you were looking for... Good luck!
An angle can be used like such:
image = Image.open("lena.jpg")
angle = 180
tkimage = ImageTk.PhotoImage(image.rotate(angle))
...
It would be possible to draw the picture, and use reversed coordinates (so when you know the size of the canvas, instead of saying 50x50
, you could use (max-50)x(max-50)
.
The question is whether axes.imshow
can handle the ImageTk.PhotoImage
. Then again, I am not entirely sure if you'd just want this on a Tkinter canvas instead, e.g.:
canvas_obj = self.canvas.create_image(250, 250, image=tkimage)