Steve on Image Processing with MATLAB

Image processing concepts, algorithms, and MATLAB

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.




Published with MATLAB® 7.9

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.