Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)

Solution 1:

Cast the image to np.uint8 after scaling [0, 255] range will dismiss this warning. It seems like a feature in matplotlib, as discussed in this issue.

plt.imshow((out * 255).astype(np.uint8))

Solution 2:

Instead of plt.imshow(out), use plt.imshow(out.astype('uint8')). That's it!

Solution 3:

If you want to show it, you can use img/255.

Or

np.array(img,np.int32)

The reason is that if the color intensity is a float, then matplotlib expects it to range from 0 to 1. If an int, then it expects 0 to 255. So you can either force all the numbers to int or scale them all by 1/255.

Solution 4:

As the warning is saying, it is clipping the data in between (0,1).... I tried above methods the warning was gone but my images were having missing pixel values. So I tried following steps for my numpy array Image (64, 64, 3). Step 1: Check the min and max values of array by

maxValue = np.amax(Image)
minValue = np.amin(Image)

For my case min values of images were negative and max value positive, but all were in between about (-1, 1.5) so Step 2: I simply clipped the data before imshow by

Image = np.clip(Image, 0, 1)
plt.imshow(Image)

Note if your pixel values are ranged something like (-84, 317) etc. Then you may use following steps

Image = Image/np.amax(Image)
Image = np.clip(Image, 0, 1)
plt.imshow(Image)