Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PIL import Image
- import numpy
- def find_coeffs(pa, pb):
- matrix = []
- for p1, p2 in zip(pa, pb):
- matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
- matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])
- A = numpy.matrix(matrix, dtype=numpy.float)
- B = numpy.array(pb).reshape(8)
- res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B)
- return numpy.array(res).reshape(8)
- img = Image.open(sys.argv[1])
- width, height = img.size
- m = -0.5
- coeffs = find_coeffs(
- [(0, 0), (width, 0), (width, height), (0, height)],
- [(width/4.8, 0), (width - (width/4.8), 0), (width, height), (0, height)])
- img.transform((width, height), Image.PERSPECTIVE, coeffs,
- Image.BICUBIC).save(sys.argv[3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement