{"id":298,"date":"2009-11-09T15:35:10","date_gmt":"2009-11-09T20:35:10","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/11\/09\/writing-tiff-files-with-given-width-and-resolution\/"},"modified":"2019-10-29T08:03:50","modified_gmt":"2019-10-29T12:03:50","slug":"writing-tiff-files-with-given-width-and-resolution","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/11\/09\/writing-tiff-files-with-given-width-and-resolution\/","title":{"rendered":"Writing TIFF files with given width and resolution"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>I received the following question recently:<\/p>\r\n   <p><i>I'd like to be able to establish an image size that will be recognized by PDFLATEX when I compile my document. Often, I size\r\n         an image in MATLAB only to have it occupy more than a page after compiling in PDFLATEX. I know I can use imresize, but I'd\r\n         like to resize a PNG so that it is exact 2 inches wide, so I can get some consistency of sizing in my document.<\/i><\/p>\r\n   <p>It occurred to me that this is a common use case in publishing articles, books, etc: \"I need this image to print exactly 3\r\n      inches wide in my document so it fits nicely in the column.\"  The document might be LaTeX as above, or a Word file, or something\r\n      else.\r\n   <\/p>\r\n   <p>In publishing, TIFF is usually the format of choice.  The MATLAB function <tt>imwrite<\/tt> can include extra information in the TIFF file to control how wide an image will be printed when included in a document application.\r\n       This extra information is provided in the form of the <tt>'Resolution'<\/tt> parameter, which gives pixels per inch.  (In this context the term dots per inch is also used.)\r\n   <\/p>\r\n   <p>I wrote about how to use the <tt>'Resolution'<\/tt> parameter <a href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/03\/03\/help-my-publisher-wants-a-300-dpi-tiff\/\">way back in 2006<\/a>, but at the time I didn't explain how to achieve a certain desired printed width.\r\n   <\/p>\r\n   <p>When publishers specify a certain resolution in pixels per inch (dots per inch), <i>and<\/i> the image should be printed at a certain width, then generally the image has to be resized to meet both criteria.  That is,\r\n      the number of image pixels may have to be changed.\r\n   <\/p>\r\n   <p>To help users prepare such image files I wrote the MATLAB function <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25595-imwritesize-write-image-file-with-specified-width-and-resolution\"><tt>imwritesize<\/tt><\/a>, which you can find on the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25595-imwritesize-write-image-file-with-specified-width-and-resolution\">MATLAB Central File Exchange<\/a>.\r\n   <\/p>\r\n   <p>In the most simple usage, <tt>imwritesize<\/tt> will create a TIFF file or PNG file for you so that the image will have the desired width in a document application.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rgb = imread(<span style=\"color: #A020F0\">'peppers.png'<\/span>);\r\nsize(rgb)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n   384   512     3\r\n\r\n<\/pre><p>Notice the original image has 512 pixels per row.<\/p>\r\n   <p>Now write the image out using <tt>imwritesize<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imwritesize(rgb, <span style=\"color: #A020F0\">'peppers_3in.tif'<\/span>, 3);<\/pre><p>The extension of the output filename determines the image file format.  To write out a PNG file instead of TIFF, just use\r\n      the extension '.png'.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imwritesize(rgb, <span style=\"color: #A020F0\">'peppers_3in.png'<\/span>, 3);<\/pre><p>With this usage, the number of pixels in the image is not changed. <tt>imwritesize<\/tt> just saves the image into the file with the right resolution parameter to achieve the desired width;\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">info = imfinfo(<span style=\"color: #A020F0\">'peppers_3in.tif'<\/span>);\r\nimage_pixels_per_row = info.Width<\/pre><pre style=\"font-style:oblique\">\r\nimage_pixels_per_row =\r\n\r\n   512\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">document_width_in_inches = image_pixels_per_row \/ info.XResolution<\/pre><pre style=\"font-style:oblique\">\r\ndocument_width_in_inches =\r\n\r\n   2.994152046783626\r\n\r\n<\/pre><p>The width is not exactly 3 inches because the resolution value in a TIFF file is restricted to be an integer number of pixels\r\n      per inch.\r\n   <\/p>\r\n   <p>Now suppose you want the document width of the image to be 3 inches <b>and<\/b> the document image resolution to be 300 dpi?  Then you specify 300 as an additional argument to <tt>imwritesize<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imwritesize(rgb, <span style=\"color: #A020F0\">'peppers_3in_at_300dpi.tif'<\/span>, 3, 300);<\/pre><p>With this usage, the number of image pixels is changed by calling <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/imresize.html\"><tt>imresize<\/tt><\/a>, an Image Processing Toolbox function.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">info2 = imfinfo(<span style=\"color: #A020F0\">'peppers_3in_at_300dpi.tif'<\/span>);\r\nimage_pixels_per_row = info2.Width<\/pre><pre style=\"font-style:oblique\">\r\nimage_pixels_per_row =\r\n\r\n   900\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">document_width_in_inches = image_pixels_per_row \/ info2.XResolution<\/pre><pre style=\"font-style:oblique\">\r\ndocument_width_in_inches =\r\n\r\n     3\r\n\r\n<\/pre><p>I apologize to those of you living in metric land.  I wanted to keep the interface simple so I did not include units options.\r\n       You could perform a metric conversion in the call to <tt>imwritesize<\/tt>, like this:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imwritesize(rgb, <span style=\"color: #A020F0\">'peppers_7cm.tif'<\/span>, 7\/2.54)<\/pre><p>Or you can modify the code in <tt>imwritesize<\/tt> to suit yourself.  I tried to keep the code very straightforward so that users could learn from it.\r\n   <\/p>\r\n   <p>I hope you find this MATLAB Central File Exchange contribution useful.  If you want to use this function and you already have\r\n      MATLAB R2009b, you should consider taking this chance to experiment with the new <a href=\"https:\/\/blogs.mathworks.com\/community\/2009\/09\/21\/the-front-page-of-the-file-exchange-your-desktop\/\">MATLAB Desktop \/ File Exchange integration<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f665d66061c94faaa4c41c7c8bb926af() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='f665d66061c94faaa4c41c7c8bb926af ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f665d66061c94faaa4c41c7c8bb926af';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = '';\r\n        copyright = 'Copyright 2009 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_f665d66061c94faaa4c41c7c8bb926af()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.9<br><\/p>\r\n<\/div>\r\n<!--\r\nf665d66061c94faaa4c41c7c8bb926af ##### SOURCE BEGIN #####\r\n%%\r\n% I received the following question recently:\r\n%\r\n% _I'd like to be able to establish an image size that will be recognized by\r\n% PDFLATEX when I compile my document. Often, I size an image in MATLAB only to\r\n% have it occupy more than a page after compiling in PDFLATEX. I know I can use\r\n% imresize, but I'd like to resize a PNG so that it is exact 2 inches wide, so\r\n% I can get some consistency of sizing in my document._\r\n%\r\n% It occurred to me that this is a common use case in publishing articles,\r\n% books, etc: \"I need this image to print exactly 3 inches wide in my document\r\n% so it fits nicely in the column.\"  The document might be LaTeX as above, or a\r\n% Word file, or something else.\r\n%\r\n% In publishing, TIFF is usually the format of choice.  The MATLAB function\r\n% |imwrite| can include extra information in the TIFF file to control how wide\r\n% an image will be printed when included in a document application.  This extra\r\n% information is provided in the form of the |'Resolution'| parameter, which\r\n% gives pixels per inch.  (In this context the term dots per inch is also used.)\r\n% \r\n% I wrote about how to use the |'Resolution'| parameter \r\n% <https:\/\/blogs.mathworks.com\/steve\/2006\/03\/03\/help-my-publisher-wants-a-300-dpi-tiff\/ \r\n% way back in 2006>, but at the time I didn't explain how to achieve a certain\r\n% desired printed width.\r\n%\r\n% When publishers specify a certain resolution in pixels per inch (dots per\r\n% inch), _and_ the image should be printed at a certain width, then generally\r\n% the image has to be resized to meet both criteria.  That is, the number of\r\n% image pixels may have to be changed.\r\n%\r\n% To help users prepare such image files I wrote the MATLAB function\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25595-imwritesize-write-image-file-with-specified-width-and-resolution \r\n% |imwritesize|>, which you can find on the \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/25595-imwritesize-write-image-file-with-specified-width-and-resolution \r\n% MATLAB Central File Exchange>.\r\n%\r\n% In the most simple usage, |imwritesize| will create a TIFF file or PNG file\r\n% for you so that the image will have the desired width in a document\r\n% application.\r\n\r\nrgb = imread('peppers.png');\r\nsize(rgb)\r\n\r\n%%\r\n% Notice the original image has 512 pixels per row.\r\n%\r\n% Now write the image out using |imwritesize|:\r\n\r\nimwritesize(rgb, 'peppers_3in.tif', 3);\r\n\r\n%%\r\n% The extension of the output filename determines the image file format.  To\r\n% write out a PNG file instead of TIFF, just use the extension '.png'.\r\n\r\nimwritesize(rgb, 'peppers_3in.png', 3);\r\n\r\n%%\r\n% With this usage, the number of pixels in the image is not changed.\r\n% |imwritesize| just saves the image into the file with the right resolution\r\n% parameter to achieve the desired width;\r\n\r\ninfo = imfinfo('peppers_3in.tif');\r\nimage_pixels_per_row = info.Width\r\n\r\n%%\r\ndocument_width_in_inches = image_pixels_per_row \/ info.XResolution\r\n\r\n%%\r\n% The width is not exactly 3 inches because the resolution value in a TIFF file\r\n% is restricted to be an integer number of pixels per inch.\r\n%\r\n% Now suppose you want the document width of the image to be 3 inches *and* the\r\n% document image resolution to be 300 dpi?  Then you specify 300 as an\r\n% additional argument to |imwritesize|:\r\n\r\nimwritesize(rgb, 'peppers_3in_at_300dpi.tif', 3, 300);\r\n\r\n%%\r\n% With this usage, the number of image pixels is changed by calling \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/imresize.html \r\n% |imresize|>, an Image Processing Toolbox function.\r\n\r\n%%\r\ninfo2 = imfinfo('peppers_3in_at_300dpi.tif');\r\nimage_pixels_per_row = info2.Width\r\n\r\n%%\r\ndocument_width_in_inches = image_pixels_per_row \/ info2.XResolution\r\n\r\n%%\r\n% I apologize to those of you living in metric land.  I wanted to keep the\r\n% interface simple so I did not include units options.  You could perform a\r\n% metric conversion in the call to |imwritesize|, like this:\r\n\r\nimwritesize(rgb, 'peppers_7cm.tif', 7\/2.54)\r\n\r\n%%\r\n% Or you can modify the code in |imwritesize| to suit yourself.  I tried to keep\r\n% the code very straightforward so that users could learn from it.\r\n%\r\n% I hope you find this MATLAB Central File Exchange contribution useful.  If you\r\n% want to use this function and you already have MATLAB R2009b, you should\r\n% consider taking this chance to experiment with the new \r\n% <https:\/\/blogs.mathworks.com\/community\/2009\/09\/21\/the-front-page-of-the-file-exchange-your-desktop\/ \r\n% MATLAB Desktop \/ File Exchange integration>.\r\n\r\n##### SOURCE END ##### f665d66061c94faaa4c41c7c8bb926af\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   I received the following question recently:\r\n   I'd like to be able to establish an image size that will be recognized by PDFLATEX when I compile my document. Often, I size\r\n         an image in... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/11\/09\/writing-tiff-files-with-given-width-and-resolution\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[332,76,190],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/298"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=298"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/298\/revisions"}],"predecessor-version":[{"id":3659,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/298\/revisions\/3659"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}