Bella_Ciao_gr
Bella_Ciao_gr

Reputation: 107

Python-Why does imshow() yield a blank image for a non-zero array?

My goal is to display a 2D Array as a image in Python. The array doesn't contain zero elements, and therefore I would expect an image in which imshow() automatically sets the color scale according to the array values. However, when I run the code, the image is blank.

The csv file is: https://ufile.io/urk5m

import numpy as np
import matplotlib.pyplot as plt

data_ = np.loadtxt(open("new_file.csv", "rb"), delimiter=",")
plt.imshow(data_)

My result is this: https://i.sstatic.net/kPfgZ.jpg

Upvotes: 1

Views: 1673

Answers (1)

Martin
Martin

Reputation: 3385

Always remember, but really always, that images works on 8bit integers. Thats why there is 2^8 shades of gray and why most commmon number of CS colors is (2^8)^3= 16.7 mil. colors. 3 because there are 3 color channels - RGB, each having 256 shades.

Everybody is counting with it and mainly the image processing libraries.

Therefore ALWAYS make sure you pass correct matrix datatype into image processing functions:

image_8bit = np.uint8(data_)
plt.imshow(image_8bit)
plt.show()

enter image description here

Upvotes: 2

Related Questions