Sam K
Sam K

Reputation: 349

Matrix Multiplication outputting incorrect image

I have an image matrix which I am scaling down and then scaling back to its original values.

The image is first read into an array of size (150,200,3).

image_data = ndimage.imread(image_file,mode='RGB').astype(float)

Next I am scaling the pixel values down and then back to their original values. Also, I cast the float array back to an integer array.

  image_data = (image_data - (255.0 / 2)) / 255.0
  image_data = (image_data * 255.0) + (255.0 / 2)
  image_data = image_data.astype(int)

Now I save the image in the file initial.jpg.

  image0 = PILImage.fromarray(image_data,mode='RGB')
  image0.save('Tests/Initial.jpg')

The saved Image looks like this...

enter image description here

However, If I remove the matrix multiplication and casting (the middle three lines of code). I save an image that looks like this. This is the correct file.

enter image description here

I have verified the matrices and the modified matrices are identical to the original so I am confused why the images would not be identical as well.

Upvotes: 0

Views: 71

Answers (1)

Sam K
Sam K

Reputation: 349

It is found that image arrays can only be saved properly if they are of type uint8. So change

image_data = image_data.astype(int)

to

image_data = image_data.astype(np.uint8)

Upvotes: 0

Related Questions