Steve on Image Processing

November 9th, 2009

Writing TIFF files with given width and resolution

I received the following question recently:

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 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 like to resize a PNG so that it is exact 2 inches wide, so I can get some consistency of sizing in my document.

It occurred to me that this is a common use case in publishing articles, books, etc: "I need this image to print exactly 3 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 else.

In publishing, TIFF is usually the format of choice. The MATLAB function imwrite can include extra information in the TIFF file to control how wide an image will be printed when included in a document application. This extra information is provided in the form of the 'Resolution' parameter, which gives pixels per inch. (In this context the term dots per inch is also used.)

I wrote about how to use the 'Resolution' parameter way back in 2006, but at the time I didn't explain how to achieve a certain desired printed width.

When publishers specify a certain resolution in pixels per inch (dots per inch), and the image should be printed at a certain width, then generally the image has to be resized to meet both criteria. That is, the number of image pixels may have to be changed.

To help users prepare such image files I wrote the MATLAB function imwritesize, which you can find on the MATLAB Central File Exchange.

In the most simple usage, imwritesize will create a TIFF file or PNG file for you so that the image will have the desired width in a document application.

rgb = imread('peppers.png');
size(rgb)
ans =

   384   512     3

Notice the original image has 512 pixels per row.

Now write the image out using imwritesize:

imwritesize(rgb, 'peppers_3in.tif', 3);

The extension of the output filename determines the image file format. To write out a PNG file instead of TIFF, just use the extension '.png'.

imwritesize(rgb, 'peppers_3in.png', 3);

With this usage, the number of pixels in the image is not changed. imwritesize just saves the image into the file with the right resolution parameter to achieve the desired width;

info = imfinfo('peppers_3in.tif');
image_pixels_per_row = info.Width
image_pixels_per_row =

   512

document_width_in_inches = image_pixels_per_row / info.XResolution
document_width_in_inches =

   2.994152046783626

The width is not exactly 3 inches because the resolution value in a TIFF file is restricted to be an integer number of pixels per inch.

Now suppose you want the document width of the image to be 3 inches and the document image resolution to be 300 dpi? Then you specify 300 as an additional argument to imwritesize:

imwritesize(rgb, 'peppers_3in_at_300dpi.tif', 3, 300);

With this usage, the number of image pixels is changed by calling imresize, an Image Processing Toolbox function.

info2 = imfinfo('peppers_3in_at_300dpi.tif');
image_pixels_per_row = info2.Width
image_pixels_per_row =

   900

document_width_in_inches = image_pixels_per_row / info2.XResolution
document_width_in_inches =

     3

I apologize to those of you living in metric land. I wanted to keep the interface simple so I did not include units options. You could perform a metric conversion in the call to imwritesize, like this:

imwritesize(rgb, 'peppers_7cm.tif', 7/2.54)

Or you can modify the code in imwritesize to suit yourself. I tried to keep the code very straightforward so that users could learn from it.

I hope you find this MATLAB Central File Exchange contribution useful. If you want to use this function and you already have MATLAB R2009b, you should consider taking this chance to experiment with the new MATLAB Desktop / File Exchange integration.


Get the MATLAB code

Published with MATLAB® 7.9

2 Responses to “Writing TIFF files with given width and resolution”

  1. Petter replied on :

    This seems like a Latex issue. The command
    \includegraphics[width=2in]{image.png}
    will to the trick. image.png should be saved with as high resolution as is possible.

  2. Steve replied on :

    Petter—Your solution is specific to LaTeX and doesn’t help with other document or graphics applications.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Steve: Dansheng—The following code will give you the binary image corresponding to a single object. L =...
  • Dansheng Song: Hi Steve, Your posts are very helpful for Matlab users. Do you know how can I get the binary image by...
  • DP: Thanks Steve. it did work. now can easily calculate DSC.
  • Steve: DP— A & B
  • DP: Hi Steve, Dice similarity coefficient is defined to find the overlapped regions between two objects in an image....
  • Steve: Searching— axis on
  • Steve: DP—Sorry, but I’ve never heard of dice similarity coefficients.
  • searching: Hey, I found this place extremely useful. I have a question though. Although I manage to plot my points on...
  • DP: Hi Steve, thanks for your email. i could not seperate those connected objects. Instead i am trying to compare...
  • Gopesh: Excellent code..was very helpful to me to change the background color of mri images and thereby improving the...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.