Eric H.
Eric H.

Reputation: 2232

How to distort an image with python PIL using an inner quad without cropping out of the quad

I want to distort an image using python PIL and pointing 4 points inside (not on the borders), that should become a rectangle in the resulting image. I also want to keep the full image outside this quad. The full image should be distorted, not cropped.

Upvotes: 0

Views: 43

Answers (1)

Eric H.
Eric H.

Reputation: 2232

The solution is to enlarge the inner quad by adding/substracting a constant to the x,y of the corners in order to include the whole original image. No matter if negative values.

from PIL import Image
QUADRI = (94,208, 87,650, 777,648, 767,203)     # (x,y) of the 4 corners of the inner quad
x1,y1,x2,y2,x3,y3,x4,y4 = QUADRI
k=300
x1-=k; y1-=k
x2-=k; y2+=k
x3+=k; y3+=k
x4+=k; y4-=k
im = Image.open("photo.jpg")
im1 = im.transform(im.size, Image.QUAD, (x1,y1,x2,y2,x3,y3,x4,y4))      
im1.show()

Upvotes: 0

Related Questions