Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import sys
  2. from PIL import Image
  3. import numpy
  4.  
  5. def find_coeffs(pa, pb):
  6. matrix = []
  7. for p1, p2 in zip(pa, pb):
  8. matrix.append([p1[0], p1[1], 1, 0, 0, 0, -p2[0]*p1[0], -p2[0]*p1[1]])
  9. matrix.append([0, 0, 0, p1[0], p1[1], 1, -p2[1]*p1[0], -p2[1]*p1[1]])
  10.  
  11. A = numpy.matrix(matrix, dtype=numpy.float)
  12. B = numpy.array(pb).reshape(8)
  13.  
  14. res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B)
  15. return numpy.array(res).reshape(8)
  16.  
  17. img = Image.open(sys.argv[1])
  18. width, height = img.size
  19. m = -0.5
  20.  
  21. coeffs = find_coeffs(
  22. [(0, 0), (width, 0), (width, height), (0, height)],
  23. [(width/4.8, 0), (width - (width/4.8), 0), (width, height), (0, height)])
  24.  
  25. img.transform((width, height), Image.PERSPECTIVE, coeffs,
  26. Image.BICUBIC).save(sys.argv[3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement