我有一个方法,它使用 matplotlib.pyplot.ginput(( 从轴收集单个单击的位置,然后将图像放置在该位置的同一图中现有的轴底层数组中。
视觉对象仅在第二次调用 ginput(( 后更新,这意味着与用户的单击相比,其响应滞后,并且只有在他们选择下一个位置时才会显示他们刚刚放置的图像。这是有问题的,因为放置的随机图像可能会影响他们下一步要单击的位置。
调用方法如下所示,尽管此处绘制的是轴而不是添加图像:
%matplotlib
from matplotlib import pyplot
import numpy
import random
dimensions = [10,10]
visual_width = 1366
visual_height = 768
print("Established screen dimensions as "+str(visual_width)+","+str(visual_height))
fig, axarr = pyplot.subplots(dimensions[1],dimensions[0]
, num="Live game visualised"
, figsize=(visual_width, visual_height)
, clear=True)
fig.set_size_inches(visual_width, visual_height, forward=False)
overax = fig.add_subplot(111)
overax.axis("off")
#remove axes for initial rendering
for horizontal in range(0, dimensions[0]):
for vertical in range(0, dimensions[1]):
axarr[vertical, horizontal].axis('off')
text = fig.text(0,0, "", va="bottom", ha="left")
# test repeated interactions with figure, through successively adding tiles to the play area
playing = True
while playing:
click = pyplot.ginput()
if not click:
break
horizontal = int(click[0][0]*dimensions[0])
vertical = dimensions[1] - int(click[0][1]*dimensions[1])
print(str(horizontal)+","+str(vertical))
text.set_text(str(horizontal)+","+str(vertical))
axarr[vertical-1, horizontal].axis("on")
我的 python 3 脚本在 jupyter notebook 中,但我使用默认的 %matplotlib 后端弹出视觉效果。我在 Ubuntu 上的 Chrome 上工作。
这是一个笨拙且不令人满意的解决方案,但我的解决方法是提示双击,并接受对 ginput 的第一次调用是将存储到分配给第二次调用的变量的数据:
%matplotlib
from matplotlib import pyplot
import numpy
import random
dimensions = [10,10]
visual_width = 1366
visual_height = 768
print("Established screen dimensions as "+str(visual_width)+","+str(visual_height))
fig, axarr = pyplot.subplots(dimensions[1],dimensions[0]
, num="Live game visualised"
, figsize=(visual_width, visual_height)
, clear=True)
fig.set_size_inches(visual_width, visual_height, forward=False)
overax = fig.add_subplot(111)
overax.axis("off")
#remove axes for initial rendering
for horizontal in range(0, dimensions[0]):
for vertical in range(0, dimensions[1]):
axarr[vertical, horizontal].axis('off')
text = fig.text(0,0, "", va="bottom", ha="left")
# test repeated interactions with figure, through successively adding tiles to the play area
playing = True
while playing:
pyplot.ginput()
click = pyplot.ginput()
if not click:
break
horizontal = int(click[0][0]*dimensions[0])
vertical = dimensions[1] - int(click[0][1]*dimensions[1])
print(str(horizontal)+","+str(vertical))
text.set_text(str(horizontal)+","+str(vertical))
axarr[vertical-1, horizontal].axis("on")
text.set_text("Double click to make your next move")
我认为我的问题与您的问题相似。我的正在放大ginput
使用我自己的方法选择的图像区域。
这是我使用plt.ion
和plt.draw
的解决方案,在我的脚本中运行良好。删除image super resolution
方法。
#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
import argparse
import sys
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"-i", "--image_name", type=str, default="test.jpg", help="Input image path"
)
args = parser.parse_known_args(sys.argv[1:])[0]
plt.ion() # works in `.py` scripts
imm = plt.imread(args.image_name)
fig = plt.figure()
plt.imshow(imm)
plt.tight_layout()
plt.show()
imms = [imm]
while True:
aa = plt.ginput(2, timeout=-1)
aa = np.array(aa)
left, right = int(aa[:, 0].min()), int(aa[:, 0].max())
top, bottom = int(aa[:, 1].min()), int(aa[:, 1].max())
if right - left < 1 or bottom - top < 1: # Double click to return
if len(imms) > 1:
imms.pop(-1)
else:
break
else:
sub_image = imms[-1][top:bottom, left:right, :]
imms.append(sub_image)
plt.imshow(imms[-1])
plt.draw() # works in `ipython` environment
在我的测试中:
- 添加没有最后一个
plt.draw
的plt.ion
仍然可以在.py
脚本中使用。 - 添加最后一个
plt.draw
而不plt.ion
ipython
环境中工作。