您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)如何獲取matplotlib.pyplot畫圖后的二進(jìn)制流的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
有些時(shí)候,我們需要畫圖后的二進(jìn)制數(shù)據(jù)流,matplotlib沒有提供相關(guān)的api,通過源碼查看與百度,得到下面此方法
import matplotlib.pyplot as plt import numpy as np import io x=np.arange(10) y=x #plt.plot(x,y) #canvas = plt.get_current_fig_manager().canvas #canvas.draw() fig=plt.figure() plt.plot(x,y) canvas=fig.canvas #上面這段代碼和上面注釋掉的代碼效果一樣 #方法1 buffer = io.BytesIO() canvas.print_png(buffer) data=buffer.getvalue() buffer.close() #方法2 buf, size = canvas.print_to_buffer() image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1) buffer=io.BytesIO() image.save(buffer,'PNG') data=buffer.getvalue() buffer.close() with open('hhh.png',mode='wb') as f: f.write(data) #f=open('hh.png',mode='wb') #f.write(data) #f.close()
如果我們想把二進(jìn)制的圖片轉(zhuǎn)成數(shù)組也是可以的,
buffer=io.BytesIO() buffer.write(data) img=Image.open(buffer) img = np.asarray(img)
感謝各位的閱讀!關(guān)于“如何獲取matplotlib.pyplot畫圖后的二進(jìn)制流”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。