Is There A Way To Convert Pyplot.imshow() Object To Numpy Array?
Solution 1:
Unless you really need the marker ticks and such,
im._rgba_cache
gives you direct access to the MxNx4 numpy
array that is the color mapped data.
If you just want the color mapped data, you can by pass imshow
entirely and directly color-map the data your self (see guide for picking your color map)
my_cm = maplotlib.cm.get_cmap('Reds')
normed_data = (data - np.min(data)) / (np.max(data) - np.min(data))
mapped_data = my_cm(normed_data)
which will give you back a MxNx4 array mapped between 0
and 1
,
mapped_datau8 = (255 * my_cm(normed_data)).astype('uint8')
or
mapped_data = my_cm(normed_data, bytes=True)
will convert it to unsigned ints.
matplotlib
includes a range of normalization code, see here.
get_cmap
doc and colormap gallery
edit: fixed oversight pointed out at https://stackoverflow.com/a/14880947/380231
Solution 2:
Ar you sure you want to convert the return value of the method or the whole plot?
For the latter, you should try:
- Save the plot to a StringIO-buffer image using
savefig
- Load the image from this buffer, using PIL or opencv
- Convert it to a numpy array
See sample below:
import numpy as np
import matplotlib.pyplotas plt
importPILfrom cStringIO importStringIO
plt.imshow(np.random.random((20,20)))
buffer_ = StringIO()
plt.savefig(buffer_, format = "png")
buffer_.seek(0)
image = PIL.Image.open(buffer_)
ar = np.asarray(image)
buffer_.close()
Look into savefig
-*args
and **kwargs
for more options, e.g., dpi, background color, transparency, padding etc.
If you jsut want the color coded image, without axes, labels, etc., I'd still do the same, just use
plt.subplots_adjust(0,0,1,1)
to extend the axes over the whole figure. Take care of the aspect
of you plot, otherwise mpl might shrink your axes again.
Solution 3:
Updated answer inspired from @Thorsten Kranz
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import io
plt.imshow(np.random.random((20,20)))
with io.BytesIO() as buffer:
plt.savefig(buffer, format = "png")
buffer.seek(0)
image = Image.open(buffer)
ar = np.asarray(image)
print(ar.shape)
Solution 4:
here I found a much more sample to convert an array to rgba, I wanted to convert the spectrogram to rgb images with the magma cmap, and here's what I did;
import matplotlib.cm as cm
sm = cm.ScalarMappable(cmap='magma')
sm.set_clim(spect.min(), spect.max())
im = sm.to_rgba(spect)
plt.imshow(im)
plt.title(im.shape)
Post a Comment for "Is There A Way To Convert Pyplot.imshow() Object To Numpy Array?"