Python code not rotating and scaling image in affine transformations as expected

65 Views Asked by At

Problem:

I am trying to rotate and scale an image using the following Python code:

    """
        img = matplotlib.pyplot.imread('letterR.jpg')
        T = np.array([[0, 2, 0],
             [-2, 0, 0]
             [0, 0, 1]])
    """

img_transformed = np.empty((2000, 2000, 4), dtype=np.uint8)
for i, row in enumerate(img):
    for j, col in enumerate(row):
        pixel_data = img[i, j, :]
        input_coords = np.array([i, j, 1])
        i_out, j_out, _ = T @ input_coords
        img_transformed[i_out, j_out, :] = pixel_data

plt.figure(figsize=(5, 5))
plt.imshow(img_transformed)

letterR.jpg

However, when I execute this code, I am not getting the same output as shown in the following image:

expected output

Instead, I am getting the following output:

actual output

Error messages:

I am not receiving any error messages.

Additional information:

I have tried executing the code on different machines and I am getting the same results.

I am not sure why the code is not producing the expected output. I would appreciate any help that you can provide.

Thank you.

0

There are 0 best solutions below