有没有办法从图像中返回实际的 x,y 坐标? [英] Is there way to return actual x,y-coordinates from the image?

查看:37
本文介绍了有没有办法从图像中返回实际的 x,y 坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我亲爱的py专家.我在 python(Tkinter 库)中编写了一个 GUI 程序,它显示一张图片,以显示我使用 Canvas 小部件的图像.该程序确实加载了一张图片(它的宽度/高度总是大于 Canvas 的宽度/高度),当我在 Canvas 上单击鼠标时,它返回 ax,y - 坐标(和颜色灰度值),但它始终是 Canvas 的坐标,不是图像的.例如 - 图像大小为 x=5040 和 y=3360,Canvas 小部件大小为 x=500 和 y=400,当我点击 Canvas 时,我永远不会得到值,即使我滚动图像,X- 超过 500轴和 Y 轴分别为 400.我做错了什么,如何从图像中获取正确的信息?谢谢.

解决方案

作为 上的文档画布说明:

<块引用>

坐标系

Canvas 小部件使用两个坐标系;窗口坐标系(左上角为 (0, 0))和画布坐标系,用于指定绘制项目的位置.通过滚动画布,您可以指定在窗口中显示画布坐标系的哪一部分.

scrollregion 选项用于限制画布的滚动操作.要设置它,您通常可以使用以下内容:

canvas.config(scrollregion=canvas.bbox(ALL))

<块引用>

要将窗口坐标转换为画布坐标,请使用canvasxcanvasy 方法:

def 回调(事件):画布 = event.widgetx = canvas.canvasx(event.x)y = canvas.canvasy(event.y)打印 canvas.find_closest(x, y)

请注意,这将为您提供画布坐标,这些坐标不一定与图像坐标相同——例如,如果您在画布内的图像周围留下边框,您必须手动更正.

my dear py-experts. I wrote a GUI program in python (Tkinter library) which displays a picture, to display an image i used the Canvas widget. The program does load a picture (its width/height always bigger than Canvas width/height) and when i make a mouse click on the Canvas it returns a x,y - coordinates (and color grayscale value), but it always a coordinates of Canvas, not an image ones. For example - image sizes are x=5040 and y=3360 and Canvas widget sizes are x=500 and y=400 and when i making a click on Canvas i never get values,even if i scrolling image, more than 500 for X-axis and 400 for Y-axis respectively. What i did wrong, how to get right information from the image? Thnx.

解决方案

As the docs on Canvas explain:

Coordinate Systems

The Canvas widget uses two coordinate systems; the window coordinate system (with (0, 0) in the upper left corner), and a canvas coordinate system which specify where the items are drawn. By scrolling the canvas, you can specify which part of the canvas coordinate system to show in the window.

The scrollregion option is used to limit scrolling operations for the canvas. To set this, you can usually use something like:

canvas.config(scrollregion=canvas.bbox(ALL))

To convert from window coordinates to canvas coordinates, use the canvasx and canvasy methods:

def callback(event):
    canvas = event.widget
    x = canvas.canvasx(event.x)
    y = canvas.canvasy(event.y)
    print canvas.find_closest(x, y)

Note that this will give you canvas coordinates, which are not necessarily the same as image coordinate—if you, e.g., left a border around the image within the canvas, you will have to correct for that manually.

这篇关于有没有办法从图像中返回实际的 x,y 坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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