def get_segmentation_crop(self, annotation_segmentation_id): ''' return pil image object by annotation_segmentation_id ''' sg = Annotation.segmentation_objects.get(id=annotation_segmentation_id) if sg.segmentation: mask = np.zeros([[sg.image.height, sg.image.width], count], dtype=np.uint8) # wrong order? for i, landmarks in enumerate(sg.segmentation): img = Image.new('L', (sg.image.width, sg.image.height), 0) for landmark in landmarks: ImageDraw.Draw(img).polygon(landmark, outline=0, fill=1) mask[:, :, i] = np.array(img) elif sg.mask: rle_struct = [{'counts': sg.mask, 'size': [sg.height, sg.width]}] decoded_mask = mask.decode(rle_struct) mask = np.squeeze(decoded_mask) mask_image = PImage.fromarray(mask) mat = np.reshape(mask_image, (sg.height, sg.width)) overlay = PImage.fromarray(np.uint8(mat * 255), 'L') return overlay