def CropImage(img): width,height = img.size try: leftMargin = min([list(x).index(False) for x in img if False in list(x)]) except ValueError: leftMargin = 0 try: rightMargin = min([RevList(list(x)).index(False) for x in img if False in list(x)]) except ValueError: rightMargin =0 try: topMargin = min([list(x).index(False) for x in numpy.transpose(img) if False in list(x)]) except ValueError: topMargin = 0 try: bottomMargin = min([RevList(list(x)).index(False) for x in numpy.transpose(img) if False in list(x)]) except ValueError: bottomMargin =0 imgGray = pymorph.gray(img) return Image.fromarray(imgGray).crop((leftMargin,topMargin,width-rightMargin,height-bottomMargin))
def ThinImage(imgPath): img = Image.open(imgPath).convert('L') imgArray = numpy.asarray(img) imgBinary = pymorph.neg(pymorph.binary(imgArray)) img = pymorph.thin(imgBinary) iPrune = pymorph.thin(img,pymorph.endpoints('homotopic'),10) Image.fromarray(pymorph.gray(pymorph.neg(iPrune))).save('thinned.png','PNG') print "Saving"
def thinning(img, iters=-1, theta=45): """ thinning morphological operation """ # convert to binary assert len(img.shape) == 2 bin_img = pymorph.binary(img) result = pymorph.thin(bin_img, n=iters, theta=theta) return pymorph.gray(result)
def skelm(img, mask=None): """skeleton operation """ assert len(img.shape) == 2 bin_img = pymorph.binary(img) if mask == None: result = pymorph.skelm(bin_img) else: result = pymorph.skelm(bin_img, mask) return pymorph.gray(result)