Python 使用插入式PIL/TKinter
我正在尝试使用PIL和Tkinter来制作一些定制的图像处理软件。我想用鼠标在图像中选择一个感兴趣的区域,抓取那些像素值,然后将其传递给scipy/numpy进行一些数字运算,并可能通过PIL进行更多的图像编辑 到目前为止,我有一个RegionFinterest课程:Python 使用插入式PIL/TKinter,python,image-processing,tkinter,python-imaging-library,Python,Image Processing,Tkinter,Python Imaging Library,我正在尝试使用PIL和Tkinter来制作一些定制的图像处理软件。我想用鼠标在图像中选择一个感兴趣的区域,抓取那些像素值,然后将其传递给scipy/numpy进行一些数字运算,并可能通过PIL进行更多的图像编辑 到目前为止,我有一个RegionFinterest课程: class RegionOfInterest: def __init__(self,image,boundingBox): #take bounding box, draw an oval on the i
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图像对象和一个边界框(带有用于裁剪和保存图像的捕获方法)。这将由draw函数调用:
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 = []
然后通过Tkinter中的控件单击调用
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()
mouse_X=0
鼠标_Y=0
地区=[]
区域列表=[]
image=image.open('test.jpg')
image=image.convert('L')
imPix=image.load()
canvas=Tkinter.canvas(窗口,宽度=image.size[0],高度=image.size[1])
canvas.pack()
image_tk=ImageTk.PhotoImage(图像)
canvas.create_image(image.size[0]//2,image.size[1]//2,image=image_-tk)
window.bind(“,draw)
bind(“,lambda e:nextFrame(sequence\u object=sequence,event=e))
Tkinter.mainloop()
目前我最大的问题是,当我绘制矩形时(回到RegionFintereste.init()),矩形不会显示
关于如何让它工作有什么建议吗?关于学习如何更好地连接tkinter/pil的资源,可能有什么建议吗?如果您确实在您认为的位置绘制矩形,最有可能发生的情况是,它的堆叠级别较低,因此位于图像下方。尝试
lift
ing或lower
ing矩形或图像 像这样的事情怎么样:
from Tkinter import *
from PIL import Image, ImageTk
class ScrolledCanvas(Frame):
def __init__(self, master, **kwargs):
Frame.__init__(self, master, **kwargs)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.canv = Canvas(self, bd=0, highlightthickness=0)
self.hScroll = Scrollbar(self, orient='horizontal',
command=self.canv.xview)
self.hScroll.grid(row=1, column=0, sticky='we')
self.vScroll = Scrollbar(self, orient='vertical',
command=self.canv.yview)
self.vScroll.grid(row=0, column=1, sticky='ns')
self.canv.grid(row=0, column=0, sticky='nsew', padx=4, pady=4)
self.canv.configure(xscrollcommand=self.hScroll.set,
yscrollcommand=self.vScroll.set)
class MyApp(Tk):
def __init__(self):
Tk.__init__(self)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.main = ScrolledCanvas(self)
self.main.grid(row=0, column=0, sticky='nsew')
self.c = self.main.canv
self.currentImage = {}
self.load_imgfile('test.jpg')
self.c.bind('<ButtonPress-1>', self.on_mouse_down)
self.c.bind('<B1-Motion>', self.on_mouse_drag)
self.c.bind('<ButtonRelease-1>', self.on_mouse_up)
self.c.bind('<Button-3>', self.on_right_click)
def load_imgfile(self, filename):
img = Image.open(filename)
img = img.convert('L')
self.currentImage['data'] = img
photo = ImageTk.PhotoImage(img)
self.c.xview_moveto(0)
self.c.yview_moveto(0)
self.c.create_image(0, 0, image=photo, anchor='nw', tags='img')
self.c.config(scrollregion=self.c.bbox('all'))
self.currentImage['photo'] = photo
def on_mouse_down(self, event):
self.anchor = (event.widget.canvasx(event.x),
event.widget.canvasy(event.y))
self.item = None
def on_mouse_drag(self, event):
bbox = self.anchor + (event.widget.canvasx(event.x),
event.widget.canvasy(event.y))
if self.item is None:
self.item = event.widget.create_rectangle(bbox, outline="yellow")
else:
event.widget.coords(self.item, *bbox)
def on_mouse_up(self, event):
if self.item:
self.on_mouse_drag(event)
box = tuple((int(round(v)) for v in event.widget.coords(self.item)))
roi = self.currentImage['data'].crop(box) # region of interest
values = roi.getdata() # <----------------------- pixel values
print roi.size, len(values)
#print list(values)
def on_right_click(self, event):
found = event.widget.find_all()
for iid in found:
if event.widget.type(iid) == 'rectangle':
event.widget.delete(iid)
app = MyApp()
app.mainloop()
从Tkinter导入*
从PIL导入图像,ImageTk
类滚动画布(框架):
定义初始(自我、主控、**kwargs):
帧。初始帧(自身、主帧、**kwargs)
self.grid_rowconfigure(0,权重=1)
self.grid\u column配置(0,权重=1)
self.canv=Canvas(self,bd=0,highlightthickness=0)
self.hScroll=滚动条(self,orient='horizontal',
command=self.canv.xview)
self.hScroll.grid(行=1,列=0,sticky='we')
self.vScroll=滚动条(self,orient='vertical',
command=self.canv.yview)
self.vScroll.grid(行=0,列=1,sticky='ns')
self.canv.grid(行=0,列=0,粘滞=nsew',padx=4,pady=4)
self.canv.configure(xscrollcommand=self.hScroll.set,
yscrollcommand=self.vScroll.set)
MyApp类(Tk):
定义初始化(自):
Tk.\uuuuuu初始(自我)
self.grid_rowconfigure(0,权重=1)
self.grid\u column配置(0,权重=1)
self.main=滚动画布(self)
self.main.grid(行=0,列=0,sticky='nsew')
self.c=self.main.canv
self.currentImage={}
self.load\u imgfile('test.jpg'))
self.c.bind(“”,self.on\u mouse\u down)
self.c.bind(“”,self.on_鼠标拖动)
self.c.bind(“”,self.on\u mouse\u up)
self.c.bind(“”,self.on_右键单击)
def load_imgfile(自身,文件名):
img=Image.open(文件名)
img=img.convert('L')
self.currentImage['data']=img
photo=ImageTk.PhotoImage(img)
self.c.xview_moveto(0)
self.c.yview_moveto(0)
self.c.create_image(0,0,image=photo,anchor='nw',tags='img')
self.c.config(scrollregion=self.c.bbox('all'))
self.currentImage['photo']=照片
鼠标按下时的def(自身,事件):
self.anchor=(event.widget.canvasx(event.x),
event.widget.canvasy(event.y))
self.item=None
鼠标拖动def(自身,事件):
bbox=self.anchor+(event.widget.canvasx(event.x),
event.widget.canvasy(event.y))
如果self.item为无:
self.item=event.widget.create_矩形(bbox,outline=“黄色”)
其他:
event.widget.coords(self.item,*bbox)
鼠标上的def(自身,事件):
如果是self.item:
在鼠标上拖动(事件)
box=event.widget.coords(self.item))中v的元组((int(round(v)))
roi=self.currentImage['data'].crop(框)#感兴趣区域
values=roi.getdata()#