0

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)

original image

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)

transformed image without increasing the resolution

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)

transformed image with increased resolution

Any ideas how to solve this without resizing the image with PIL.Image.resize before tranforming it?

Axel
  • 89
  • 1
  • 2
  • 8
  • Every pixel of the original image ended up somewhere in that final image, controlled by your transform matrix - where do you expect any further pixel data to come from, to make it any bigger? If you just wanted a bigger version of your 256x256 result, you need to change the matrix accordingly - I think changing all the 256s to 1500s in `pa` would do it. – jasonharper May 24 '18 at 13:13
  • OK got it. That makes sense and also works. Thanks! – Axel May 24 '18 at 16:44
  • https://stackoverflow.com/questions/14177744/how-does-perspective-transformation-work-in-pil this will solve your problem. – Salman Ghauri Nov 14 '18 at 10:46

0 Answers0