MichaelAndroidNewbie
MichaelAndroidNewbie

Reputation: 695

How to convert a 1D python list containing image data into a numpy array and display it

Ok, I asked a similar question that revolved around vrep but it was a little specific when in fact a simpler python-based question would be more useful. I will, however, leave the question there should anyone be able to provide useful information.

Here is the question; How does one take a 1 dimensional list containing image data, convert it into a numpy array and then display it?

This is what I have so far:

im = np.array(image, dtype=np.uint8)
im.resize(128,128,3) #reshape this array into an image form (e.g. rather than 49152)
mlp.imshow(im)
pylab.show(im)

Here, image is returned from a simxGetVisionSensorImage (not important if you don't know vrep stuff) and is a list. I then try to create a numpy array and read the data in, turning it from a signed 8 bit integer into an unsigned 8 bit integer. I then resize it (it is a 49152 length list corresponding to a resolution of 128x128) and attempt to display it using either matplotlib or pylab.

Here are the includes should you need them:

import numpy as np
import matplotlib.pyplot as mlp
import pylab

the matplotlib.show command does not even show a window for the image. the pylab.show command throws this error:

Traceback (most recent call last):
File "vrep_epuck.py", line 59, in <module>
pylab.show(im)
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 244, in show
return _show(*args, **kw)
File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 165, in __call__
if block:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Here is the link to the original vrep question should you want to see the whole code or see the vrep stuff:

vrep question

Upvotes: 0

Views: 2496

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339230

It would help to stay within the usual naming conventions.
Because then it would be more obvious that pyplot.show() does not take an image as argument. Thus, don't use pyplot.show(some_image_as_argument) but simply pyplot.show().

import matplotlib.pyplot as plt

image = ...
im = np.array(image, dtype=np.uint8)
im.resize(128,128,3) 

plt.imshow(im)
plt.show()

Upvotes: 3

Related Questions