在Matplotlib图中获取点坐标

时间:2013-06-19 14:15:30

标签: python matplotlib ipython spyder

我在Spyder环境中使用Ipython解释器。 我想编写一个脚本来裁剪同一维度的文件夹中的所有图像。 我使用Matplotlib绘制* .jpg图像。我希望能够使用鼠标指向图片上的两个角,这两个角对应于新图片的左上角和右下角。

我写了类似的代码

import os
import scipy.ndimage as nd
import matplotlib.pyplot as plt

plt.close('all')

class Conoscopia:
    def __init__(self, cono, nome):
        self.x1 = 0.0
        self.y1 = 0.0
        self.x2 = 0.0
        self.y2 = 0.0
        self.cono = cono
        self.nome = nome

    def connect(self):
        self.cid = self.cono.canvas.mpl_connect('button_press_event', self.onclick)

    def onclick(self, event):
        if self.x1==0.0 and self.y1==0.0:
            self.x1=event.xdata
            self.y1=event.ydata
            print "x1 = ", event.xdata, " y1 = ", event.xdata
        elif self.x2==0.0 and self.y2==0.0:
            self.x2=event.xdata
            self.y2=event.ydata
            print "x2 = ", event.xdata, " y2 = ", event.xdata     

    def return_coo(self):
        return self.x1, self.x2, self.y1, self.y2

    def disegna(self):
        plt.imshow(self.nome)
        plt.show()       

plt.ioff()

files = os.listdir(os.getcwd())

lista_nomi=[]
for nome in files:
    if nome[len(nome)-4:len(nome)] == '.JPG' or nome[len(nome)-4:len(nome)] == '.jpg':
        lista_nomi.append(nome)

foto = nd.imread(lista_nomi[0])

id_f = plt.figure()

cn = Conoscopia(id_f, foto)
cn.connect()
cn.disegna()

x1, y1, x2, y2 = cn.return_coo()

我的问题是整个代码一次运行,最后x1,y1,x2和y2的值对应初始化值。 然后我可以点击图片来获取四个值,如果我在解释器x1, y1, x2, y2 = cn.return_coo()中运行它们包含正确的值。 但是我可以做些什么来直接在我的代码中检索它们并能够使用它们呢? 我理想的是,一旦两次点击完成,窗口关闭,我可以使用坐标(x1,y1)和(x2,y2)进行其余的编程。

我试图在函数return_coo()上添加一些额外的等待循环或循环,直到坐标与初始化值不同,但这阻止了我的计算机并且我从未显示过pricture。

谢谢!

0 个答案:

没有答案