def extractimg(im, poly): path= PIL.ImagePath.Path(poly) bb= path.getbbox() bbsize= (abs(bb[2]-bb[0]), abs(bb[3]-bb[1])) tx= PIL.ImageTransform.ExtentTransform(path.getbbox()) newimg= im.transform(bbsize, tx) # update path to match 'newimg' path.map(lambda x,y:(x-bb[0], y-bb[1])) # now clear pixels outside poly img= PIL.Image.new("RGBA", bbsize) img.paste(newimg, createmask(path)) return img
def extractimg(im, poly): path = PIL.ImagePath.Path(poly) bb = path.getbbox() bbsize = (abs(bb[2] - bb[0]), abs(bb[3] - bb[1])) tx = PIL.ImageTransform.ExtentTransform(path.getbbox()) newimg = im.transform(bbsize, tx) # update path to match 'newimg' path.map(lambda x, y: (x - bb[0], y - bb[1])) # now clear pixels outside poly img = PIL.Image.new("RGBA", bbsize) img.paste(newimg, createmask(path)) return img
def createmask(path): bb= path.getbbox() bbsize= (abs(bb[2]-bb[0]), abs(bb[3]-bb[1])) img= PIL.Image.new("1", bbsize) draw= PIL.ImageDraw.Draw(img) draw.polygon(path, fill=1) return img
def createmask(path): bb = path.getbbox() bbsize = (abs(bb[2] - bb[0]), abs(bb[3] - bb[1])) img = PIL.Image.new("1", bbsize) draw = PIL.ImageDraw.Draw(img) draw.polygon(path, fill=1) return img