imforge.crop module

imforge.crop.crop(image, polygon, fillcolor=None, cut_out=False, clip=False)

Crop image according to given (convex) polygon.

Note

All edges of the polygon are included in the cropped image. This is not the same behaviour as PIL.Image.Image.crop() when using a straight rectangular crop box because the right and bottom coordinates are not included in the cropped image.

Parameters
  • image (Union[PIL.Image.Image,numpy.ndarray]) – the (pillow) image to crop

  • polygon (list[tuple[int,int]]) – the list of points coordinates of the polygon to crop

  • fillcolor – the color to use for filling area outside of polygon

  • cut_out (bool) – if False (the default), the fillcolor is only used for filling areas outside of original image when doing crop. Il True, then all areas outside of polygon are filled with fillcolor.

  • clip (bool) – if False (the default), the cropped image will include the whole polygon, even if some of its coordinates are outside of the original image, which can lead to a large image with areas filled with fillcolor on edges of cropped image. If True, the polygon is first clipped to the original image box, thus leading to the minimal cropped image containing all the visible parts of polygon in original image.

Returns

the cropped image, in same format as input image

Return type

Union[PIL.Image.Image,numpy.ndarray]

imforge.crop.crop_cv2(image, polygon, fillcolor=None, cut_out=False, clip=False)

Crop image according to given (convex) polygon.

Note

All edges of the polygon are included in the cropped image. This is not the same behaviour as PIL.Image.Image.crop() when using a straight rectangular crop box because the right and bottom coordinates are not included in the cropped image.

Parameters
  • image (numpy.ndarray) – the (opencv) image to crop

  • polygon (list[tuple[int,int]]) – the list of points coordinates of the polygon to crop

  • fillcolor – the color to use for filling area outside of polygon

  • cut_out (bool) – if False (the default), the fillcolor is only used for filling areas outside of original image when doing crop. Il True, then all areas outside of polygon are filled with fillcolor.

  • clip (bool) – if False (the default), the cropped image will include the whole polygon, even if some of its coordinates are outside of the original image, which can lead to a large image with areas filled with fillcolor on edges of cropped image. If True, the polygon is first clipped to the original image box, thus leading to the minimal cropped image containing all the visible parts of polygon in original image.

Returns

the cropped image

Return type

numpy.ndarray

imforge.crop.crop_pil(image, polygon, fillcolor=None, cut_out=False, clip=False)

Crop image according to given (convex) polygon.

Note

All edges of the polygon are included in the cropped image. This is not the same behaviour as PIL.Image.Image.crop() when using a straight rectangular crop box because the right and bottom coordinates are not included in the cropped image.

Parameters
  • image (PIL.Image.Image) – the (pillow) image to crop

  • polygon (list[tuple[int,int]]) – the list of points coordinates of the polygon to crop

  • fillcolor – the color to use for filling area outside of polygon

  • cut_out (bool) – if False (the default), the fillcolor is only used for filling areas outside of original image when doing crop. Il True, then all areas outside of polygon are filled with fillcolor.

  • clip (bool) – if False (the default), the cropped image will include the whole polygon, even if some of its coordinates are outside of the original image, which can lead to a large image with areas filled with fillcolor on edges of cropped image. If True, the polygon is first clipped to the original image box, thus leading to the minimal cropped image containing all the visible parts of polygon in original image.

Returns

the cropped image

Return type

PIL.Image.Image