3 回答

TA貢獻(xiàn)1858條經(jīng)驗 獲得超8個贊
根據(jù)tiago接受的響應(yīng),這是一個小型通用函數(shù),該函數(shù)將numpy數(shù)組導(dǎo)出到與該數(shù)組具有相同分辨率的圖像:
import matplotlib.pyplot as plt
import numpy as np
def export_figure_matplotlib(arr, f_name, dpi=200, resize_fact=1, plt_show=False):
"""
Export array as figure in original resolution
:param arr: array of image to save in original resolution
:param f_name: name of file where to save figure
:param resize_fact: resize facter wrt shape of arr, in (0, np.infty)
:param dpi: dpi of your screen
:param plt_show: show plot or not
"""
fig = plt.figure(frameon=False)
fig.set_size_inches(arr.shape[1]/dpi, arr.shape[0]/dpi)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(arr)
plt.savefig(f_name, dpi=(dpi * resize_fact))
if plt_show:
plt.show()
else:
plt.close()
如tiago上次答復(fù)中所述,首先需要找到屏幕DPI,例如,可以在此處完成:http : //dpi.lv
我resize_fact在函數(shù)中添加了一個附加參數(shù),例如,您可以將圖像導(dǎo)出到原始分辨率的50%(0.5)。
添加回答
舉報