4

I have loaded a 100x100 rgb image in a numpy array. I then converted it to a 30000x1 numpy array to pass through a machine learning model. The output of this model is also a 30000x1 numpy array. How do I convert this array back to a numpy array of 100x100 3-tuples so I can print the generated rgb image?

If the initial array is [r1 g1 b1],[r2 g2 b2],...,[], it is unrolled to [r1 g1 b1 r2 g2 b2 ...]. I need it back in the form [r1 g1 b1],[r2 g2 b2],...,[].

What I used to load the image as array:

im=img.resize((height,width), Image.ANTIALIAS);
im=np.array(im);
im=im.ravel();

I have tried .reshape((100,100,3)) and I'm getting a black output image. The machine learning model is correct and it is not the reason for getting a black output.

3
  • Your reshape seems correct (try it with the initial array). How are you converting to RGB, maybe the problem is there? Commented Aug 4, 2016 at 18:56
  • I'm not converting the image to RGB. I'm loading the RGB image into the img array and reshaping it into 100x100 and storing it in im Commented Aug 5, 2016 at 2:57
  • I was referring to the output. From your question: "I have tried .reshape((100,100,3)) and I'm getting a black output image. " -- hence my question, how are you converting back to RGB image? Have you tried to reshape and display your original array, without the machine learning part in between? Commented Aug 5, 2016 at 9:11

1 Answer 1

3

Try reshape((3, 100, 100))

a = np.random.random((3, 2, 2))
# array([[[ 0.28623689,  0.96406455],
#         [ 0.55002183,  0.73325715]],
#
#        [[ 0.44293834,  0.08118479],
#         [ 0.28732176,  0.94749812]],
#
#        [[ 0.40169829,  0.0265604 ],
#         [ 0.07904701,  0.19342463]]])
x = np.ravel()
# array([ 0.28623689,  0.96406455,  0.55002183,  0.73325715,  0.44293834,
#         0.08118479,  0.28732176,  0.94749812,  0.40169829,  0.0265604 ,
#         0.07904701,  0.19342463])
print(x.reshape((2, 2, 3)))
# array([[[ 0.28623689,  0.96406455,  0.55002183],
#         [ 0.73325715,  0.44293834,  0.08118479]],

#        [[ 0.28732176,  0.94749812,  0.40169829],
#         [ 0.0265604 ,  0.07904701,  0.19342463]]])
print(x.reshape((3, 2, 2)))
# array([[[ 0.28623689,  0.96406455],
#         [ 0.55002183,  0.73325715]],
#
#        [[ 0.44293834,  0.08118479],
#         [ 0.28732176,  0.94749812]],
#
#        [[ 0.40169829,  0.0265604 ],
#         [ 0.07904701,  0.19342463]]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.