获取 canvas 上某个点的颜色
Get color of a point on the canvas
是否可以从 canvas 中获取点 (x/y-coordinate) 的颜色(rgb 值和透明度)?示例:我在 canvas 上绘制了一些图形和文本,后来我想获得特定坐标处的点的颜色。
无论 canvas 在屏幕上是否可见,该解决方案都应该是独立的。而且它应该独立于操作系统工作。
我没有在网上找到任何解决方案。这就是为什么我认为这是不可能的。我说得对吗?
你是对的,tk 没有提供任何方法来获取 canvas 上特定像素的颜色。
canvas 不是非常基于像素,因此没有提供 API 来执行此操作。
但如果你已经从中得到 tkimg package installed, you can use that to do a screengrab of the canvas and then fetch the pixel value。
package require Img
# Get the data into an image
set screengrab [image create photo -format window -data $theCanvas]
# Read the pixel data out of the grabbed image
set pixeldata [$screengrab get $x $y]
# Get rid of the grabbed data once you're done
image delete $screengrab
请注意,相关坐标将是视口坐标,而不是 canvas 内部坐标:如果您滚动了 canvas,则必须根据需要进行偏移。
是否可以从 canvas 中获取点 (x/y-coordinate) 的颜色(rgb 值和透明度)?示例:我在 canvas 上绘制了一些图形和文本,后来我想获得特定坐标处的点的颜色。
无论 canvas 在屏幕上是否可见,该解决方案都应该是独立的。而且它应该独立于操作系统工作。
我没有在网上找到任何解决方案。这就是为什么我认为这是不可能的。我说得对吗?
你是对的,tk 没有提供任何方法来获取 canvas 上特定像素的颜色。
canvas 不是非常基于像素,因此没有提供 API 来执行此操作。
但如果你已经从中得到 tkimg package installed, you can use that to do a screengrab of the canvas and then fetch the pixel value。
package require Img
# Get the data into an image
set screengrab [image create photo -format window -data $theCanvas]
# Read the pixel data out of the grabbed image
set pixeldata [$screengrab get $x $y]
# Get rid of the grabbed data once you're done
image delete $screengrab
请注意,相关坐标将是视口坐标,而不是 canvas 内部坐标:如果您滚动了 canvas,则必须根据需要进行偏移。