如何在 Tkinter 中反转导入图像的 y 轴? [英] How to reverse the y-axis of the imported image in Tkinter?

查看:32
本文介绍了如何在 Tkinter 中反转导入图像的 y 轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于重复标记:我完全知道在 matplotlib 中有一些类似的问题,例如 这个.我的问题是关于 Tkinter 而不是 matplotlib.

For duplicate-markers: I am fully aware that there are some similar questions in matplotlib, such as this one. My question is about Tkinter not matplotlib.

我现在正在将 Lena 导入 python 并在她的帽子上画一个绿点

I am now importing Lena into python and drawing a green dot at her 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>

(请忽略红点)

我现在希望绿点 (50, 50) 仍然在 Lena 的帽子上,但我也希望绿点绘制在左下角而不是左上角.

I now want the green dot (50, 50) to be still on Lena's hat, but I also want the green dot to be drawn at the left bottom corner instead of the left top corner.

我期待这样的事情:

正如所见,我设法在 matplotlib 中轻松地做到了这一点,并添加了一行:

As seen, I managed to do this easily in matplotlib with one additional line:

axes.invert_yaxis()

问题

我现在正在 Tkinter 的画布上绘图.我怎样才能达到同样的效果?

Question

I am now drawing on the canvas in Tkinter. How may I achieve the same effect?

更新

Lena 仅用于说明我的目的.在我真正的问题中,我没有在 Tkinter 中导入任何东西.我只是在空白画布上作画.我不愿意修改我的数据,我只想把图纸倒过来.就像我的Lena插图一样,坐标仍然是(50, 50).不同的是现在它在左下角而不是上角.

Lena is merely for illustration of my purpose. In my real problem, I am not importing anything in Tkinter. I merely draw on a empty canvas. I am reluctant to modify my data, I only wish to have the drawing upside down. Like in my Lena illustration, the coordinate is still (50, 50). The difference is that now it is in the left bottom instead of top corner.

推荐答案

在我看来,使用如下代码会很简单:

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()

这将返回以下内容:

基本上我所做的就是获取画布高度 (250),然后从中减去之前的 Y 值 (50),从而返回 Y 的倒数 (200).不完全是内置函数,但实际的翻转部分非常简单.希望这就是您要找的东西...祝您好运!

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!

这篇关于如何在 Tkinter 中反转导入图像的 y 轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆