How to change y axis of imported image in Tkinter?

For duplicate markers: I fully understand that matplotlib has similar questions like this one . My question on Tkinter not matplotlib .

Now I import Lena into python and draw a green dot in the hat with

 In [72]: from PIL import Image ....: import matplotlib.pylab as plt ....: im = Image.open('lena.jpg') ....: fig = plt.figure() ....: axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) ....: axes.imshow(im) ....: axes.scatter(50, 50, marker='s', color='green') Out[72]: <matplotlib.collections.PathCollection at 0xb3e2ef0> 

(Please ignore the red dot)

enter image description here

Now I want the green dot (50, 50) remain on Lena's hat, but I also want the green dot to be drawn in the lower left corner instead of the upper left corner.

I expect something like this:

enter image description here

As you can see, I managed to do this easily in matplotlib with one extra line:

 axes.invert_yaxis() 

Question

Now I draw on canvas in Tkinter . How can I achieve the same effect?


Update

Lena is just to illustrate my purpose. In my real problem, I am not importing anything into Tkinter . I'm just painting an empty canvas . I am reluctant to change my data, I just want the drawing to turn upside down. As in my illustration of Lena, the coordinate is still (50, 50) . The difference is that now it is in the lower left corner, and not in the upper corner.
+7
python tkinter
source share
3 answers

It seems to me that this would be simple with the code as shown 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:

Flipped y axis

Basically all I did was get the canvas height (250) and subtract the previous Y value (50), which returned the Y-inverse (200). Not quite a built-in function, but the actual part of the coup was very simple. Hope this was what you were looking for ... Good luck!

0
source share

You can use this angle:

 image = Image.open("lena.jpg") angle = 180 tkimage = ImageTk.PhotoImage(image.rotate(angle)) ... 

One could draw a picture and use the inverse coordinates (therefore, when you know the size of the canvas, instead of saying 50x50 , you can use (max-50)x(max-50) .

The question is whether axes.imshow handle ImageTk.PhotoImage . Again, I'm not quite sure that you just want this on a Tkinter canvas, for example:

 canvas_obj = self.canvas.create_image(250, 250, image=tkimage) 
+2
source share

What you seem to be asking for is a 2D World to Viewport transform:

Take the area defined in "world coordinates" (say, 10 meters by 10 meters) and compare it with the area defined in the coordinates of the canvas.

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() 
0
source share

All Articles