Matlab繪圖基礎——用print函數批量保存圖片到文件(Print figure or save to file)


Matlab繪圖基礎——用print函數批量保存圖片到文件(Print figure or save to file)

 

一、用法解析

1.1. 分辨率-rnumber

1.2.  輸出圖片的“格式”formats

二、用法示例

2.1. 設置輸出圖片的“圖像縱橫比”

2.2. Batch Processing(圖片保存“批處理”)filename

1.2. 輸出圖片的“格式”formats

一、用法解析

print(figure_handle,'formats','-rnumber','filename') 

%將圖形保存為formats格式,分辨率為600的(默認為72),最好指定的分辨率大一點,否則保存圖形的效果較差.

1.1. 分辨率-rnumber

Use -rnumber to specify the resolution of the generated output.

To set the resolution(分辨率) of the output file for a built-in MATLAB format, use the -r switch.

l  For example, -r300 sets the output resolution to 300 dots per inch(每英寸300個點)

The –r switch is also supported for Windows Enhanced Metafiles(增強型圖元文件), JPEG, TIFF and PNG files, but is not supported for Ghostscript raster formats(柵格文件).

l  For more information, see Printing and Exporting without a DisplayandResolution Considerations.

—— 來自matlab幫助“print”頁中

1.2.  輸出圖片的“格式”formats

請參考本文末尾

二、用法示例

% Save the figure with the handle h to a PostScript file named Figure1, which can be printed later.

H = figure;   % 指定圖片打印figure_handles

% 若沒有figure_handle,則默認print當前顯示圖片

plot(1:4,5:8)

print(h,'-dpng','-r200','Figure1')      % 這三行代碼就夠用了

2.1. 設置輸出圖片的“圖像縱橫比”

When you set the axes Position to [0 0 1 1] so that it fills the entire figure, the aspect ratio is not preserved when you print because MATLAB printing software adjusts the figure size when printing according to the figure's PaperPositionproperty. To preserve the image aspect ratio(圖像縱橫比) when printing, set the figure's 'PaperPositionMode' to 'auto' from the command line.

set(gcf,'PaperPositionMode','auto')

% Setting the current figure's (gcf) PaperPositionMode to auto enables

% you to resize the figure window and print it at the size you see on the screen.

舉例

surf(peaks);shading interp           % 畫圖,shading使圖像美觀

axis off                                 % 不顯示坐標軸

set(gcf,'PaperPositionMode','auto'); % 設置圖像縱橫比

print('-dpng','-r200','a');           % 保存圖片,名為a

——來自matlab幫助 Printing Images

2.2. Batch Processing(圖片保存“批處理”)filename

You can use the function form of print to pass variables containing file names. For example, this for loop uses file names stored in a cell array to create a series of graphs and prints each one with a different file name:

fnames = {'file1', 'file2', 'file3'};

for k=1:length(fnames)

    surf(peaks);shading interp

    print('-dtiff','-r600',fnames{k}) % fnames is a cell of string arrays so each element is a string

end

————來自matlab幫助“print”頁末

注:如果你不能調整輸出分辨率和文件格式,可能是"Printing and Exporting without a Display"問題,具體查看幫助“print”頁

 

選項 位圖格式 對應的文件擴展名
'-djpeg' JPEG 24 位 .jpg
'-dpng' PNG 24 位 .png
'-dtiff' TIFF 24 位(壓縮) .tif
'-dtiffn' TIFF 24 位(未壓縮) .tif
'-dmeta' 增強型圖元文件(僅限 Windows) .emf
'-dbmpmono' BMP 單色 .bmp
'-dbmp' BMP 24 位 .bmp
'-dbmp16m' BMP 24 位 .bmp
'-dbmp256' BMP 8 位(256 色,使用固定顏色圖) .bmp
'-dhdf' HDF 24 位 .hdf
'-dpbm' PBM(普通格式)1 位 .pbm
'-dpbmraw' PBM(原始格式)1 位 .pbm
'-dpcxmono' PCX 1 位 .pcx
'-dpcx24b' PCX 24 位彩色(三個 8 位平面) .pcx
'-dpcx256' PCX 8 位新彩色(256 色) .pcx
'-dpcx16' PCX 舊彩色(EGA/VGA 16 色) .pcx
'-dpgm' PGM(普通格式) .pgm
'-dpgmraw' PGM(原始格式) .pgm
'-dppm' PPM(普通格式) .ppm
'-dppmraw' PPM(原始格式) .ppm
選項 向量圖形格式 對應的文件擴展名
'-dpdf' 整頁可移植文檔格式 (PDF) 顏色 .pdf
'-deps' PostScript (EPS) 3 級黑白 .eps
'-depsc' 封裝的 PostScript (EPS) 3 級彩色 .eps
'-deps2' 封裝的 PostScript (EPS) 2 級黑白 .eps
'-depsc2' 封裝的 PostScript (EPS) 2 級彩色 .eps
'-dmeta' 增強型圖元文件(僅限 Windows®) .emf
'-dsvg' SVG(可伸縮向量圖) .svg
'-dps' 全頁 PostScript (PS) 3 級黑白 .ps
'-dpsc' 全頁 PostScript (PS) 3 級彩色 .ps
'-dps2' 全頁 PostScript (PS) 2 級黑白 .ps
'-dpsc2' 全頁 PostScript (PS) 2 級彩色 .ps

 

引自:司徒鮮生

博客地址:Matlab繪圖基礎——用print函數批量保存圖片到文件(Print figure or save to file) - 司徒鮮生 - 博客園 (cnblogs.com)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM