PRINT 関数でJPEG,PNG,TIFF などの画像を出力する時、サイズを指定できますか?

19 views (last 30 days)
PRINT 関数で JPEG, PNG, TIFF などの画像を出力する時、サイズを指定する方法を教えてください。

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Jan 2022
Edited: MathWorks Support Team on 20 Jan 2022
MATLAB が使用する JPEG, PNG, TIFF などのドライバは、出力時のファイルサイズを指定するため、Figure の "PaperPosition" プロパティに依存しています。さらに、ドライバは、画像生成用の Figure のスナップショットを取得するため、PRINT コマンド実行時の "-r" オプションにより出力の解像度が決定されます。
例えば、Figure の "PaperPosition" プロパティが大半の PC のデフォルト値である [.25, .25, 8, 6] (単位:インチ) であるとします。このとき、幅8, 高さ 6 となります。また、PRINT 関数の "-r" オプションのデフォルト値は、 150 dpi です。よって、
print -djpeg filename
によって生成される画像は、
8*150 x 6*150 = 1200 x 900 [ピクセル]
のサイズとなります。
以下では、JPEG 画像の出力サイズを調節する 2 つの方法を紹介します。
1. PaperPosition プロパティと解像度を指定する
例えば、400 x 300 [ピクセル] の画像を出力したい場合、以下のように実行します。
plot(1:10) % Example graph\nset(gcf,'PaperUnits','inches','PaperPosition',[0 0 4 3])\nprint -djpeg filename.jpg -r100
出力される JPEG 画像は、4*100 x 3*100 = 400 x 300[ピクセル]のサイズです。
2. スクリーン上と同じサイズで出力する
以下のような関数 screen2jpeg.m を作成し、実行します。
function screen2jpeg(filename)\n%SCREEN2JPEG Generate a JPEG file of the current figure with\n% dimensions consistent with the figure's screen dimensions.\n%\n% SCREEN2JPEG('filename') saves the current figure to the\n% JPEG file "filename".\n%\n% Sean P. McCarthy\n% Copyright (c) 1984-98 by MathWorks, Inc. All Rights Reserved\n\nif nargin < 1\n error('Not enough input arguments!')\nend\n\noldscreenunits = get(gcf,'Units');\noldpaperunits = get(gcf,'PaperUnits');\noldpaperpos = get(gcf,'PaperPosition');\nset(gcf,'Units','pixels');\nscrpos = get(gcf,'Position');\nnewpos = scrpos/100;\nset(gcf,'PaperUnits','inches','PaperPosition',newpos)\nprint('-djpeg', filename, '-r100'); % 他の画像フォーマットしたい場合はここを編集\ndrawnow\nset(gcf,'Units',oldscreenunits,'PaperUnits',oldpaperunits,...\n 'PaperPosition',oldpaperpos)
(実行例)
>> screen2jpeg('myimage.jpg')
なお、画質を向上するためには、上記の PRINT コマンド実行時に、'-djepg' の代わりに '-dpng' を使用することをお勧めします。

More Answers (0)

Categories

Find more on 印刷と保存 in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!