def warpImage(background, image, parallelogram):
'''
@params:
background is unchanged image
image is image to be warped
parallelogram is the coordinates to warp the image to, starting at upper
left and going clockwise
returns a new image that is the composition of background and image
after image has been warped
'''
mapped = np.array([[parallelogram[0].x, parallelogram[1].x, parallelogram[2].x],
[parallelogram[0].y, parallelogram[1].y, parallelogram[2].y], [1,1,1]])
width, height = image.size
original = np.array([[0, width, width],[0, 0, height]])
#solve for affine matrix
solution = np.dot(original, inv(mapped))
#unroll matrix into a sequence
affine = (solution[0][0], solution[0][1], solution[0][2], solution[1][0], solution[1][1], solution[1][2])
transformed = image.transform(background.size, Image.AFFINE, affine)
white = Image.new("RGBA", (width, height), "white")
transformedMask = white.transform(background.size, Image.AFFINE, affine)
background.paste(transformed, (0,0), transformedMask)
return background
评论列表
文章目录