{"id":1131,"date":"2014-09-10T09:55:11","date_gmt":"2014-09-10T13:55:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1131"},"modified":"2019-11-01T11:18:57","modified_gmt":"2019-11-01T15:18:57","slug":"image-file-information-using-imfinfo","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2014\/09\/10\/image-file-information-using-imfinfo\/","title":{"rendered":"Image file information using imfinfo"},"content":{"rendered":"<div class=\"content\"><!--introduction--><!--\/introduction--><p>How can you find out what's in an image file before you read in the data? The answer is the function <tt>imfinfo<\/tt>. Today I want to explore the basics of using this function.<\/p><p>The function <tt>imfinfo<\/tt> returns a struct. The fields of the struct contain information about the image file.<\/p><p>Here's an example.<\/p><pre class=\"codeinput\">imfinfo(<span class=\"string\">'peppers.png'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nans = \r\n\r\n                  Filename: '\/Applications\/MATLAB_R2014a.app\/toolbox\/matla...'\r\n               FileModDate: '02-Apr-2013 15:55:52'\r\n                  FileSize: 287677\r\n                    Format: 'png'\r\n             FormatVersion: []\r\n                     Width: 512\r\n                    Height: 384\r\n                  BitDepth: 24\r\n                 ColorType: 'truecolor'\r\n           FormatSignature: [137 80 78 71 13 10 26 10]\r\n                  Colormap: []\r\n                 Histogram: []\r\n             InterlaceType: 'none'\r\n              Transparency: 'none'\r\n    SimpleTransparencyData: []\r\n           BackgroundColor: []\r\n           RenderingIntent: []\r\n            Chromaticities: []\r\n                     Gamma: []\r\n               XResolution: []\r\n               YResolution: []\r\n            ResolutionUnit: []\r\n                   XOffset: []\r\n                   YOffset: []\r\n                OffsetUnit: []\r\n           SignificantBits: []\r\n              ImageModTime: '16 Jul 2002 16:46:41 +0000'\r\n                     Title: []\r\n                    Author: []\r\n               Description: 'Zesty peppers'\r\n                 Copyright: 'Copyright The MathWorks, Inc.'\r\n              CreationTime: []\r\n                  Software: []\r\n                Disclaimer: []\r\n                   Warning: []\r\n                    Source: []\r\n                   Comment: []\r\n                 OtherText: []\r\n\r\n<\/pre><p>The fields returned by <tt>imfinfo<\/tt> aren't the same for all image files. Here's the output for a TIFF file.<\/p><pre class=\"codeinput\">imfinfo(<span class=\"string\">'forest.tif'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nans = \r\n\r\n                     Filename: '\/Applications\/MATLAB_R2014a.app\/toolbox\/im...'\r\n                  FileModDate: '25-Sep-2013 20:12:00'\r\n                     FileSize: 124888\r\n                       Format: 'tif'\r\n                FormatVersion: []\r\n                        Width: 447\r\n                       Height: 301\r\n                     BitDepth: 8\r\n                    ColorType: 'indexed'\r\n              FormatSignature: [73 73 42 0]\r\n                    ByteOrder: 'little-endian'\r\n               NewSubFileType: 0\r\n                BitsPerSample: 8\r\n                  Compression: 'PackBits'\r\n    PhotometricInterpretation: 'RGB Palette'\r\n                 StripOffsets: [1x17 double]\r\n              SamplesPerPixel: 1\r\n                 RowsPerStrip: 18\r\n              StripByteCounts: [1x17 double]\r\n                  XResolution: 72\r\n                  YResolution: 72\r\n               ResolutionUnit: 'Inch'\r\n                     Colormap: [256x3 double]\r\n          PlanarConfiguration: 'Chunky'\r\n                    TileWidth: []\r\n                   TileLength: []\r\n                  TileOffsets: []\r\n               TileByteCounts: []\r\n                  Orientation: 1\r\n                    FillOrder: 1\r\n             GrayResponseUnit: 0.0100\r\n               MaxSampleValue: 255\r\n               MinSampleValue: 0\r\n                 Thresholding: 1\r\n                       Offset: 122964\r\n             ImageDescription: 'Carmanah Ancient Forest, British Columbia,...'\r\n\r\n<\/pre><p>Only the first nine fields of the returned struct are always the same:<\/p><div><ul><li><tt>Filename<\/tt><\/li><li><tt>FileModDate<\/tt><\/li><li><tt>FileSize<\/tt><\/li><li><tt>Format<\/tt><\/li><li><tt>FormatVersion<\/tt><\/li><li><tt>Width<\/tt><\/li><li><tt>Height<\/tt><\/li><li><tt>BitDepth<\/tt><\/li><li><tt>ColorType<\/tt><\/li><\/ul><\/div><p><tt>FileModDate<\/tt> is the file modification date (as reported by the operating system). <tt>FileSize<\/tt> is the size of the file in bytes. The peppers.png file takes up 287,677 bytes on disk.<\/p><p><tt>Format<\/tt> is an abbreviation indicating the type of image file (PNG, TIFF, JPEG, etc.). Although frequently the filename extension indicates the image format type, this isn't always the case. The <tt>Format<\/tt> field shows you the format type whether or not the filename extension indicates it.<\/p><p><tt>FormatVersion<\/tt> has turned out to be less useful than I thought would be back in the mid-1990s. You generally don't need to pay attention to it.<\/p><p><tt>Width<\/tt> is the number of image columns, and <tt>Height<\/tt> is the number of rows.<\/p><p>If I were designing <tt>imfinfo<\/tt> from scratch today, I'd probably call the next field <tt>BitsPerPixel<\/tt> instead of <tt>BitDepth<\/tt>. I might also add a field called <tt>SamplesPerPixel<\/tt>. Anyway, images such as peppers.png used to be frequently called \"24-bit images\" because each pixel is represented by three samples (one number for red, one for green, and one for blue), and each sample is represented by 8 bits. 3*8 is 24, hence 24-bit image.<\/p><p>The file forest.tif, on the other hand, uses only one sample for each pixel, and each sample is represented by 8 bits, so forest.tif is sometimes called an \"8-bit image,\" and its <tt>BitDepth<\/tt> is 8.<\/p><p><tt>ColorType<\/tt> tells us how the pixel values are interpreted as colors on the screen. The most common values for <tt>ColorType<\/tt> are <tt>'grayscale'<\/tt>, <tt>'indexed'<\/tt>, and <tt>'truecolor'<\/tt>. In a grayscale image, each pixel value represents a shade of gray. In an indexed image, each pixel value is really a lookup index used to get the pixel color from a colormap (also called a palette). Truecolor (in this context) originally referred to a 24-bit image with three samples per pixels, and with the samples representing red, green, and blue. In MATLAB, though, we use the term to refer to any RGB image represented by three samples per pixel, regardless of the bit depth.<\/p><p>Here's an example of using the information returned by <tt>imfinfo<\/tt> to compute the compression ratio for a JPEG file. Let's use ngc6543a.jpg, the first truecolor sample image shipped with MATLAB.<\/p><pre class=\"codeinput\">info = imfinfo(<span class=\"string\">'ngc6543a.jpg'<\/span>);\r\nfile_bytes = info.FileSize;\r\nimage_bytes = info.Width * info.Height * info.BitDepth \/ 8;\r\n<\/pre><p>I divided by 8 in the line above because <tt>BitDepth<\/tt> is in bits instead of bytes.<\/p><pre class=\"codeinput\">compression_ratio = image_bytes \/ file_bytes\r\n<\/pre><pre class=\"codeoutput\">\r\ncompression_ratio =\r\n\r\n   42.7210\r\n\r\n<\/pre><p>So what about all those other fields after the first nine? Well, they vary depending on the file format and the specific information contained in the file. For example, TIFF files optionally contain horizontal and vertical resolution information.<\/p><pre class=\"codeinput\">info = imfinfo(<span class=\"string\">'forest.tif'<\/span>);\r\ninfo.XResolution\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n    72\r\n\r\n<\/pre><pre class=\"codeinput\">info.YResolution\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n    72\r\n\r\n<\/pre><pre class=\"codeinput\">info.ResolutionUnit\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\nInch\r\n\r\n<\/pre><p>Most image file formats allow you to store text descriptions in them.<\/p><pre class=\"codeinput\">info = imfinfo(<span class=\"string\">'peppers.png'<\/span>);\r\ninfo.Description\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\nZesty peppers\r\n\r\n<\/pre><p>These days, most cameras store a lot more information in image files than the older sample images that are in MATLAB and the Image Processing Toolbox. This information is also available using <tt>imfinfo<\/tt>.<\/p><pre class=\"codeinput\">imshow(<span class=\"string\">'IMG_1046.JPG'<\/span>,<span class=\"string\">'InitialMagnification'<\/span>,20)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/imfinfo_examples_01.png\" alt=\"\"> <pre class=\"codeinput\">info = imfinfo(<span class=\"string\">'IMG_1046.JPG'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\ninfo = \r\n\r\n            Filename: '\/Users\/steve\/Dropbox\/MATLAB\/Blogs\/IMG_1046.JPG'\r\n         FileModDate: '28-Jun-2014 22:07:32'\r\n            FileSize: 1595137\r\n              Format: 'jpg'\r\n       FormatVersion: ''\r\n               Width: 2816\r\n              Height: 1880\r\n            BitDepth: 24\r\n           ColorType: 'truecolor'\r\n     FormatSignature: ''\r\n     NumberOfSamples: 3\r\n        CodingMethod: 'Huffman'\r\n       CodingProcess: 'Sequential'\r\n             Comment: {}\r\n    ImageDescription: '                               '\r\n                Make: 'Canon'\r\n               Model: 'Canon PowerShot S95'\r\n         Orientation: 1\r\n         XResolution: 180\r\n         YResolution: 180\r\n      ResolutionUnit: 'Inch'\r\n            DateTime: '2014:06:28 19:07:32'\r\n    YCbCrPositioning: 'Co-sited'\r\n       DigitalCamera: [1x1 struct]\r\n       ExifThumbnail: [1x1 struct]\r\n\r\n<\/pre><p>That extra information is in the <tt>DigitalCamera<\/tt> field.<\/p><pre class=\"codeinput\">info.DigitalCamera\r\n<\/pre><pre class=\"codeoutput\">\r\nans = \r\n\r\n                ExposureTime: 0.0040\r\n                     FNumber: 4.9000\r\n             ISOSpeedRatings: 80\r\n                 ExifVersion: [48 50 51 48]\r\n            DateTimeOriginal: '2014:06:28 19:07:32'\r\n           DateTimeDigitized: '2014:06:28 19:07:32'\r\n     ComponentsConfiguration: 'YCbCr'\r\n      CompressedBitsPerPixel: 3\r\n           ShutterSpeedValue: 7.9688\r\n               ApertureValue: 4.5938\r\n           ExposureBiasValue: 0\r\n            MaxApertureValue: 4.5938\r\n                MeteringMode: 'Pattern'\r\n                       Flash: 'Flash did not fire, no strobe return detect...'\r\n                 FocalLength: 22.5000\r\n                   MakerNote: [1x2764 double]\r\n                 UserComment: [1x264 double]\r\n             FlashpixVersion: [48 49 48 48]\r\n                  ColorSpace: 'sRGB'\r\n            CPixelXDimension: 2816\r\n            CPixelYDimension: 1880\r\n         InteroperabilityIFD: [1x1 struct]\r\n       FocalPlaneXResolution: 9.6438e+03\r\n       FocalPlaneYResolution: 9.6575e+03\r\n    FocalPlaneResolutionUnit: 2\r\n               SensingMethod: 'One-chip color area sensor'\r\n                  FileSource: 'DSC'\r\n              CustomRendered: 'Normal process'\r\n                ExposureMode: 'Auto exposure'\r\n                WhiteBalance: 'Auto white balance'\r\n            DigitalZoomRatio: 1\r\n            SceneCaptureType: 'Standard'\r\n                 UnknownTags: [1x1 struct]\r\n\r\n<\/pre><p>Do you have questions about <tt>imfinfo<\/tt>? Do you ever use it for writing batch processing scripts? I'd be interested to hear about it, so please leave a comment below.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_726b3ce4748444fc810b39d8d10f8524() {\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='726b3ce4748444fc810b39d8d10f8524 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 726b3ce4748444fc810b39d8d10f8524';\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        copyright = 'Copyright 2014 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_726b3ce4748444fc810b39d8d10f8524()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><\/div><!--\r\n726b3ce4748444fc810b39d8d10f8524 ##### SOURCE BEGIN #####\r\n\r\n%% \r\n% How can you find out what's in an image file before you read in the data?\r\n% The answer is the function |imfinfo|. Today I want to explore the basics\r\n% of using this function.\r\n%\r\n% The function |imfinfo| returns a struct. The fields of the struct contain\r\n% information about the image file.\r\n%\r\n% Here's an example.\r\n\r\nimfinfo('peppers.png')\r\n\r\n%%\r\n% The fields returned by |imfinfo| aren't the same for all image files.\r\n% Here's the output for a TIFF file.\r\n\r\nimfinfo('forest.tif')\r\n\r\n%%\r\n% Only the first nine fields of the returned struct are always the same:\r\n%\r\n% * |Filename|\r\n% * |FileModDate|\r\n% * |FileSize|\r\n% * |Format|\r\n% * |FormatVersion|\r\n% * |Width|\r\n% * |Height|\r\n% * |BitDepth|\r\n% * |ColorType|\r\n%\r\n% |FileModDate| is the file modification date (as reported by the operating\r\n% system). |FileSize| is the size of the file in bytes. The peppers.png\r\n% file takes up 287,677 bytes on disk.\r\n%\r\n% |Format| is an abbreviation indicating the type of image file (PNG, TIFF,\r\n% JPEG, etc.). Although frequently the filename extension indicates the\r\n% image format type, this isn't always the case. The |Format| field shows\r\n% you the format type whether or not the filename extension indicates it.\r\n%\r\n% |FormatVersion| has turned out to be less useful than I thought would be\r\n% back in the mid-1990s. You generally don't need to pay attention to it.\r\n%\r\n% |Width| is the number of image columns, and |Height| is the number of\r\n% rows.\r\n%\r\n% If I were designing |imfinfo| from scratch today, I'd probably call the\r\n% next field |BitsPerPixel| instead of |BitDepth|. I might also add a field\r\n% called |SamplesPerPixel|. Anyway, images such as peppers.png used to be\r\n% frequently called \"24-bit images\" because each pixel is represented by\r\n% three samples (one number for red, one for green, and one for blue), and\r\n% each sample is represented by 8 bits. 3*8 is 24, hence 24-bit image.\r\n%\r\n% The file forest.tif, on the other hand, uses only one sample for each\r\n% pixel, and each sample is represented by 8 bits, so forest.tif is\r\n% sometimes called an \"8-bit image,\" and its |BitDepth| is 8.\r\n%\r\n% |ColorType| tells us how the pixel values are interpreted as colors on\r\n% the screen. The most common values for |ColorType| are |'grayscale'|,\r\n% |'indexed'|, and |'truecolor'|. In a grayscale image, each pixel value\r\n% represents a shade of gray. In an indexed image, each pixel value is\r\n% really a lookup index used to get the pixel color from a colormap (also\r\n% called a palette). Truecolor (in this context) originally referred to a\r\n% 24-bit image with three samples per pixels, and with the samples\r\n% representing red, green, and blue. In MATLAB, though, we use the term to\r\n% refer to any RGB image represented by three samples per pixel, regardless\r\n% of the bit depth.\r\n%\r\n% Here's an example of using the information returned by |imfinfo| to\r\n% compute the compression ratio for a JPEG file. Let's use ngc6543a.jpg,\r\n% the first truecolor sample image shipped with MATLAB.\r\n\r\ninfo = imfinfo('ngc6543a.jpg');\r\nfile_bytes = info.FileSize;\r\nimage_bytes = info.Width * info.Height * info.BitDepth \/ 8;\r\n\r\n%%\r\n% I divided by 8 in the line above because |BitDepth| is in bits instead of\r\n% bytes.\r\n\r\ncompression_ratio = image_bytes \/ file_bytes\r\n\r\n%%\r\n% So what about all those other fields after the first nine? Well, they\r\n% vary depending on the file format and the specific information contained\r\n% in the file. For example, TIFF files optionally contain horizontal and\r\n% vertical resolution information.\r\n\r\ninfo = imfinfo('forest.tif');\r\ninfo.XResolution\r\n\r\n%%\r\ninfo.YResolution\r\n\r\n%%\r\ninfo.ResolutionUnit\r\n\r\n%%\r\n% Most image file formats allow you to store text descriptions in them.\r\n\r\ninfo = imfinfo('peppers.png');\r\ninfo.Description\r\n\r\n%%\r\n% These days, most cameras store a lot more information in image files than\r\n% the older sample images that are in MATLAB and the Image Processing\r\n% Toolbox. This information is also available using |imfinfo|.\r\n\r\nimshow('IMG_1046.JPG','InitialMagnification',20)\r\n\r\n%%\r\ninfo = imfinfo('IMG_1046.JPG')\r\n\r\n%%\r\n% That extra information is in the |DigitalCamera| field.\r\n\r\ninfo.DigitalCamera\r\n\r\n%%\r\n% Do you have questions about |imfinfo|? Do you ever use it for writing\r\n% batch processing scripts? I'd be interested to hear about it, so please\r\n% leave a comment below.\r\n##### SOURCE END ##### 726b3ce4748444fc810b39d8d10f8524\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/imfinfo_examples_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>How can you find out what's in an image file before you read in the data? The answer is the function imfinfo. Today I want to explore the basics of using this function.The function imfinfo returns a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2014\/09\/10\/image-file-information-using-imfinfo\/\">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,36],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1131"}],"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=1131"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1131\/revisions"}],"predecessor-version":[{"id":1133,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1131\/revisions\/1133"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}