how to set the dpi while using 'imwrite' to save a matrix into an image of 'jpg' format?

16 views (last 30 days)
A is a matrix of 1000*1000*3
I want to export A as a 'jpg' image with dpi=240
imwrite(A,'filename.jpg');
the default dpi value is 96, how to change it into 240?

Accepted Answer

Image Analyst
Image Analyst on 13 May 2012
That is not one of the parameters that you can set with MATLAB's imwrite() function.
  4 Comments
David Young
David Young on 11 Jul 2023
It seems silly that you can't set the resolution parameter for jpegs when you can for tiffs. It surely would be the work of moments to put it into the imwrite code - it's just a number to go into the file header.

Sign in to comment.

More Answers (1)

DGM
DGM on 11 Jul 2023
You use external tools. If you have exiftool, use that.
% take a clean image
inpict = imread('peppers.png');
% write it to a degraded 4:2:0 JPG at 75% quality
fname = 'crustBelongsOnPies.jpg';
imwrite(inpict,fname)
% set x and y resolution tags and units using external tools
resolution = 240;
comstr = sprintf(['exiftool %s '...
'-xresolution=%d '...
'-yresolution=%d '...
'-resolutionunit=inches -v2'],...
fname,resolution,resolution)
system(comstr,'-echo')
% inspect to see that the tags have been set
imfinfo(fname)

Categories

Find more on Image Processing Toolbox 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!