![](/img/trans.png)
[英]Unable to store numpy.ndarray' object has no attribute 'save'
[英]Store a numpy.ndarray as an image, and then save the pixel values
我目前正在尋找可以將numpy.ndarray
作為圖像存儲,然后保存該圖像,並將圖像的像素值提取到numpy.ndarray
。 帶有像素值的numpy.ndarray的尺寸應與用於創建繪圖的numpy.ndarray的尺寸相同。
我嘗試過這樣的事情:
def make_plot_store_data(name, data):
plt.figure()
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.savefig(name+".png")
plt.close()
convert = plt.get_cmap(cm.jet)
numpy_output_interweawed = convert(data.T)
為什么這么亂呢?
這是采用512x512 ndarray
的一種方法,將其顯示為圖像,將其存儲為圖像對象,保存圖像文件,並生成與原始形狀相同的規格化像素陣列。
import numpy as np
# sample numpy array of an image
from skimage import data
camera = data.camera()
print(camera.shape) # (512, 512)
# array sample
print(camera[0:5, 0:5])
[[156 157 160 159 158]
[156 157 159 158 158]
[158 157 156 156 157]
[160 157 154 154 156]
[158 157 156 156 157]]
# display numpy array as image, save as object
img = plt.imshow(camera)
# save image to file
plt.savefig('camera.png')
# normalize img object pixel values between 0 and 1
normed_pixels = img.norm(camera)
# normed_pixels array has same shape as original
print(normed_pixels.shape) # (512, 512)
# sample data from normed_pixels numpy array
print(normed_pixels[0:5,0:5])
[[ 0.61176473 0.6156863 0.627451 0.62352943 0.61960787]
[ 0.61176473 0.6156863 0.62352943 0.61960787 0.61960787]
[ 0.61960787 0.6156863 0.61176473 0.61176473 0.6156863 ]
[ 0.627451 0.6156863 0.60392159 0.60392159 0.61176473]
[ 0.61960787 0.6156863 0.61176473 0.61176473 0.6156863 ]]
除了標准的pyplot
方法外,您可能還考慮研究skimage
模塊。 那里有很多圖像處理方法,它們都是為了與numpy
一起玩而設計的。 希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.