I use the image transformation function of PIL according to How does perspective transformation work in PIL?, which works fine. The only thing which does not work is increasing the resolution of the resulting image.
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
imgData=plt.imread('img.jpg')
print imgData.shape
img=Image.fromarray(imgData)
plt.imshow(img)
the transformation without increasing the resolution works fine
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 = np.matrix(matrix, dtype=np.float)
B = np.array(pb).reshape(8)
res = np.linalg.solve(A, B)
return np.array(res).reshape(8)
coeffs = find_coeffs(
[(0, 0), (256, 0), (256, 256), (0, 256)],
[(0, 0), (256, 0), (100, 200), (10, 200)])
imgNew=img.transform((256, 256), Image.PERSPECTIVE, coeffs,
Image.BICUBIC)
plt.imshow(imgNew)
But if I want to increase the resolution there is some strange behaviour of PIL - the image is not reszized to the resulting canvas size
imgNew2=img.transform((1500, 1500), Image.PERSPECTIVE, coeffs,
Image.BICUBIC)
imgNew2.save('imgNew2.jpg')
plt.imshow(imgNew2)
Any ideas how to solve this without resizing the image with PIL.Image.resize before tranforming it?