1

I have code for rectangle cropping ,Honestly I'm beginner to python this code was i saw on a site

I'm using PIL library

from PIL import Image
im = Image.open("lenna.png")

crop_rectangle = (50, 50, 200, 200)
cropped_im = im.crop(crop_rectangle)

cropped_im.show()

please help me to crop ellipse or circle region from a image thank you in advance

zindarod
  • 6,328
  • 3
  • 30
  • 58
Abi sb
  • 169
  • 1
  • 4
  • 15

1 Answers1

0

Cropping an image to an elliptical or circle region will produce the same results as cropping to a square, if their extents are the same. I am assuming that you also want to mask the image as well as crop?

To do this, create a blank mask PIL Image with the same extent as the original, use PIL.ImageDraw.Draw to draw a polygon onto the image. The mask image should now now have binary pixel values where "1" represents masked. Then simply set all values in the original image to a masked value (i.e. np.nan) where the mask pixel values equal 1 (e.g. original_image[mask == 1] = np.nan).

bgordon
  • 149
  • 2
  • 15