解決plt.savefig()保存到本地的圖片上下左右會有白邊
plt.savefig()保存到本地的圖片上下左右會有白邊
plt.imshow(datalistall[i]) plt.axis('off') # plt.gca().xaxis.set_major_locator(plt.NullLocator()) # plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.margins(0, 0) plt.savefig('自己改一下要保存的地址', bbox_inches='tight', dpi=300, pad_inches=0.0) plt.show()
測試下來,如果僅僅bbox_inches=‘tight’,最后保存的圖片僅僅把白邊變窄了。還要加上pad_inches=0.0。
中間注釋掉的兩句沒有什么影響。
plt.show()一定要寫在plt.savefig后面,不然的話會保存成一片空白。
plt.savefig() 圖片去除旁邊的空白區(qū)域、并且使用CV2讀取和candy 識別
在作圖時需要將輸出的圖片緊密排布,還要去掉坐標軸,同時設置輸出圖片大小。
但是發(fā)現(xiàn)matplotlib使用plt.savefig()保存的圖片
周圍有一圈空白。那么如何去掉該空白呢?
首先,關閉坐標軸顯示:
plt.axis('off')
但是,這樣只是關閉顯示而已,透明的坐標軸仍然會占據(jù)左下角位置,導致輸出的圖片偏右。要想完全去掉坐標軸,需要改為以下代碼:
plt.axis('off') fig = plt.gcf() fig.set_size_inches(7.0/3,7.0/3) #dpi = 300, output = 700*700 pixels plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) plt.margins(0,0) fig.savefig(out_png_path, format='png', transparent=True, dpi=300, pad_inches = 0)
即可完成去掉空白。
注:如果不采用 subplot_adjust + margin(0,0),而是在fig.savefig()的參數(shù)中添加bbox_inches = ‘tight’,也可以達到
去除空白的效果; 但是,這樣會導致對圖片輸出大小的設置失效。
import h5py import matplotlib.pyplot as plt import numpy as np import cv2 # train_imgs = h5py.File("./datafuse/test_images.hdf5", 'r') img = train_imgs['input_DEM'][0][...] plt.axis('off') fig = plt.gcf() fig.set_size_inches(7.0/3,7.0/3) plt.gca().xaxis.set_major_locator(plt.NullLocator()) plt.gca().yaxis.set_major_locator(plt.NullLocator()) plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) plt.margins(0,0) plt.imshow(img, cmap='gray') plt.savefig('DEM.png', pad_inches = 0) plt.show() img = cv2.imread('DEM.png', 0) ?# 原圖為彩色圖,可將第二個參數(shù)變?yōu)?,為灰度圖 # # plt.imshow(img, cmap='gray') # plt.show() edges = cv2.Canny(img, 100, 200) plt.subplot(121), plt.imshow(img, cmap='gray') plt.title('raw'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(edges, cmap='gray') plt.title('Canny detecetion'), plt.xticks([]), plt.yticks([]) plt.show()
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python+django+mysql開發(fā)實戰(zhàn)(附demo)
本文主要介紹了python+django+mysql開發(fā)實戰(zhàn)(附demo),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01python 實現(xiàn)讀取一個excel多個sheet表并合并的方法
今天小編就為大家分享一篇python 實現(xiàn)讀取一個excel多個sheet表并合并的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包
今天小編就為大家分享一篇關于Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04python 實現(xiàn)的發(fā)送郵件模板【普通郵件、帶附件、帶圖片郵件】
這篇文章主要介紹了python 實現(xiàn)的發(fā)送郵件模板,包含Python發(fā)送普通郵件、帶附件及帶圖片郵件相關實現(xiàn)技巧,需要的朋友可以參考下2019-07-07詳解如何使用pandas進行時間序列數(shù)據(jù)的周期轉換
時間序列數(shù)據(jù)是數(shù)據(jù)分析中經(jīng)常遇到的類型,為了更多的挖掘出數(shù)據(jù)內(nèi)部的信息,我們常常依據(jù)原始數(shù)據(jù)中的時間周期,將其轉換成不同跨度的周期,下面以模擬的K線數(shù)據(jù)為例,演示如何使用pandas來進行周期轉換,感興趣的朋友可以參考下2024-05-05Scrapy模擬登錄趕集網(wǎng)的實現(xiàn)代碼
這篇文章主要介紹了Scrapy模擬登錄趕集網(wǎng)的實現(xiàn)代碼,本文通過代碼圖文相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07