whitebear
whitebear

Reputation: 12423

show image by matplotlib from numpy array

I have numpy array which shape is (512,512,3)

It is 512 x 512 image.

I want to show image and save as png with matplotlib

How can I do this???

How should I convert??

[[[ 87  48  39]
  [107  43  29]
  [101  40  28]
  ...
  [115 107 100]
  [115 106 100]
  [115 107 102]]

 [[ 94  44  30]
  [106  38  20]
  [ 97  38  23]
  ...
  [114 109 103]
  [113 108 103]
  [114 106  98]]

 [[ 87  41  30]
  [ 96  40  32]
  [ 92  38  37]
  ...
  [114 110 105]
  [114 110 105]
  [116 109  98]]

 ...

 [[123 112 112]
  [140 120 121]
  [135 120 119]
  ...
  [215 191 218]
  [221 195 223]
  [217 196 214]]

 [[127 116 119]
  [134 115 115]
  [138 123 124]
  ...
  [217 195 220]
  [220 199 221]
  [215 193 208]]

 [[125 118 117]
  [127 115 116]
  [131 121 123]
  ...
  [215 199 220]
  [216 198 217]
  [202 179 198]]]

Upvotes: 2

Views: 4729

Answers (2)

Aditya
Aditya

Reputation: 2520

You can try the below snip,

from PIL import Image
import numpy as np
# data is your array

img = Image.fromarray(data, 'RGB')
img.save('my.png')
img.show()

Upvotes: 2

Naphat Amundsen
Naphat Amundsen

Reputation: 1623

plt.imshow(array)
plt.savefig('filename.png')

Upvotes: 2

Related Questions