Home > Mobile >  Crop an image in PIL using the 4 points of a rotated rectangle
Crop an image in PIL using the 4 points of a rotated rectangle

Time:04-03

I have a list of four points of a rotated rectangle in the form of:

points = [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]

I can crop in PIL using:

img.crop((x1, y1, x2, y2))

But this doesn't work with a rotated rectangle. Just to clarify I want the resulting cropped image to be rotated so the cropped area becomes a non-rotated rectangle.

I am willing to use openCV, although would like to avoid it as the conversion of an image from PIL to openCV takes time, and I will be itterating through this process about 100 times.

CodePudding user response:

If you start with this image:

enter image description here

You can do it like this using a ![enter image description here

  • Related