opencv - How to crop an image into polygon shape in python -
i'm trying make gui helps me cropping faces , foldering them according moods such angry, sad, happy, etc. code looks working fine when crop image saves rest of image. don't know how can accomplish that!! cropping fuctions:
def on_mouse(self, event, x, y, buttons, user_param): # mouse callback gets called every mouse event (i.e. moving, clicking, etc.) if self.done: # nothing more return if event == cv2.event_mousemove: # want able draw line-in-progress, update current mouse position self.current = (x, y) elif event == cv2.event_lbuttondown: # left click means adding point @ current position list of points print("adding point #%d position(%d,%d)" % (len(self.points), x, y)) self.points.append((x, y)) elif event == cv2.event_rbuttondown: # right click means we're done print("completing polygon %d points." % len(self.points)) self.done = true def run(self, image): # let's create our working window , set mouse callback handle events cv2.namedwindow(self.window_name, flags=cv2.window_autosize) cv2.imshow(self.window_name, image) cv2.waitkey(1) cv2.setmousecallback(self.window_name, self.on_mouse) while(not self.done): # our drawing loop, continuously draw new images # , show them in named window if (len(self.points) > 0): # draw current polygon segments cv2.polylines(image, np.array([self.points]), false, final_line_color, 1) # , show current segment cv2.line(image, self.points[-1], self.current, working_line_color) # update window cv2.imshow(self.window_name, image) # , wait 50ms before next iteration (this pump window messages meanwhile) if cv2.waitkey(50) == 27: # esc hit self.done = true # user finised entering polygon points, let's make final drawing # of filled polygon if (len(self.points) > 0): cv2.fillpoly(image, np.array([self.points]), final_line_color) # , show cv2.imshow(self.window_name, image) # waiting user press key cv2.waitkey() cv2.destroywindow(self.window_name) return image
i added whole code here if need
edit : when changed lines in open function:
filename = unicode(filename.toutf8(), encoding="utf-8") img = cv2.imread(filename)
to this:
im = image.open(filename).convert("rgba") imarray = numpy.asarray(im) real_image = imageviewer.run(imarray)
it gaves me errors: connected pydev debugger (build 162.1967.10) corrupt jpeg data: premature end of data segment traceback (most recent call last): file "c:/users/asus/desktop/cgg/gui/template2.py", line 156, in open im = image.open(filename).convert("rgba") file "c:\python27\lib\site-packages\pil\image.py", line 1956, in open prefix = fp.read(16) attributeerror: 'qstring' object has no attribute 'read'
i file name code lines way
filename = qtgui.qfiledialog.getopenfilename(self, "open file", qtcore.qdir.currentpath()) if filename: image = qtgui.qimage(filename) if image.isnull(): qtgui.qmessagebox.information(self, "image viewer", "cannot load %s." % filename) return
Comments
Post a Comment