Color mismatch when combining R, G, and B components in Python

In the following code, I first convert the image to a Numpy array:

import numpy as np
from PIL import Image

sceneImage = Image.open('peppers.tif')
arrImage = np.array(sceneImage) # 256x256x3 array

Now I define a new array and put in it arrImageaccording to the following code:

import matplotlib.pyplot as plt
final_image = np.zeros((N,N,3))
final_image[...,0] = arrImage[...,0]
final_image[...,1] = arrImage[...,1]
final_image[...,2] = arrImage[...,2]
plt.imshow(final_image)
plt.show()

In other words, I have a copy of the separately inserted components R, G and B (although this can be done much easier, I do it on purpose).

Now the problem is that fianl_imagethey arrImagedo not display the same image for me. The forms are as follows:

arrImage: enter image description here

final_image: enter image description here

What is the problem that these 2 are not the same?

+4
source share
1 answer

docs

dtype: ,

, , numpy.int8. - numpy.float64.

dtype np.zeros i.e np.uint8:

 sceneImage = Image.open('peppers.tif')
arrImage = np.array(sceneImage)  # 256x256x3 array
final_image = np.zeros((N, N,  3),dtype=np.uint8)
final_image[..., 0] = arrImage[..., 0]
final_image[..., 1] = arrImage[..., 1]
final_image[..., 2] = arrImage[..., 2]

img = Image.fromarray(final_image, 'RGB')
img.save('whatever.png')
+2

Source: https://habr.com/ru/post/1607692/


All Articles