我正在尝试使用PIL和Tkinter来制作一些自定义图像处理软件。我想用鼠标在图像中选择一个感兴趣的区域,抓住这些像素值,并将它传递给scipy/numpy进行一些数字运算,并可能进行一些图像编辑。在PIL/TKinter中使用绘图
到目前为止,我有一个RegionOfInterest类:
class RegionOfInterest:
def __init__(self,image,boundingBox):
#take bounding box, draw an oval on the image, save boundingBox locally
self.box = boundingBox
self.avgInt = 0
self.draw = ImageDraw.Draw(image)
self.draw.rectangle(boundingBox,outline='white')
def capture(self):
region_to_capture = image.crop(box)
region_to_capture.save('output.jpg')
,这需要PIL图像对象和boundingBox的(与裁剪并保存图像捕捉方法)。这获得由绘制函数调用:通过Tkinter的
def draw(event):
global image
global region
global listOfRegions
mouse_X = event.x
mouse_Y = event.y
region.append(mouse_X)
region.append(mouse_Y)
if len(region) == 4:
roi = RegionOfInterest(image,region)
listOfRegions.append(roi)
canvas.update()
roi.findPixels()
roi.calcIntensity()
region = []
后者又被称为控制单击
mouse_X = 0
mouse_Y = 0
region = []
listOfRegions = []
image = Image.open('test.jpg')
image = image.convert('L')
imPix = image.load()
canvas = Tkinter.Canvas(window, width=image.size[0], height=image.size[1])
canvas.pack()
image_tk = ImageTk.PhotoImage(image)
canvas.create_image(image.size[0]//2, image.size[1]//2, image=image_tk)
window.bind("<Control-Button-1>", draw)
window.bind("<Control-space>", lambda e: nextFrame(sequence_object=sequence,event=e))
Tkinter.mainloop()
此刻我最大的问题是,当我绘制矩形(归途in RegionOfIntereste。init()),矩形不显示!
有关如何使此工作的任何建议?也许有关于如何更好地连接tkinter/pil的资源建议?