<html lang="en"> <head> <title>Loading and Saving Images - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Image-Processing.html#Image-Processing" title="Image Processing"> <link rel="next" href="Displaying-Images.html#Displaying-Images" title="Displaying Images"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Loading-and-Saving-Images"></a> <p> Next: <a rel="next" accesskey="n" href="Displaying-Images.html#Displaying-Images">Displaying Images</a>, Up: <a rel="up" accesskey="u" href="Image-Processing.html#Image-Processing">Image Processing</a> <hr> </div> <h3 class="section">32.1 Loading and Saving Images</h3> <p>The first step in most image processing tasks is to load an image into Octave. This is done using the <code>imread</code> function, which uses the <code>GraphicsMagick</code> library for reading. This means a vast number of image formats is supported. The <code>imwrite</code> function is the corresponding function for writing images to the disk. <p>In summary, most image processing code will follow the structure of this code <pre class="example"> I = imread ("my_input_image.img"); J = process_my_image (I); imwrite ("my_output_image.img", J); </pre> <!-- imread scripts/image/imread.m --> <p><a name="doc_002dimread"></a> <div class="defun"> — Function File: [<var>img</var>, <var>map</var>, <var>alpha</var>] = <b>imread</b> (<var>filename</var>)<var><a name="index-imread-2887"></a></var><br> <blockquote><p>Read images from various file formats. <p>The size and numeric class of the output depends on the format of the image. A color image is returned as an MxNx3 matrix. Gray-level and black-and-white images are of size MxN. The color depth of the image determines the numeric class of the output: "uint8" or "uint16" for gray and color, and "logical" for black and white. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dimwrite.html#doc_002dimwrite">imwrite</a>, <a href="doc_002dimfinfo.html#doc_002dimfinfo">imfinfo</a>. </p></blockquote></div> <!-- imwrite scripts/image/imwrite.m --> <p><a name="doc_002dimwrite"></a> <div class="defun"> — Function File: <b>imwrite</b> (<var>img, filename</var>)<var><a name="index-imwrite-2888"></a></var><br> — Function File: <b>imwrite</b> (<var>img, filename, fmt</var>)<var><a name="index-imwrite-2889"></a></var><br> — Function File: <b>imwrite</b> (<var>img, filename, fmt, p1, v1, <small class="dots">...</small></var>)<var><a name="index-imwrite-2890"></a></var><br> — Function File: <b>imwrite</b> (<var>img, map, filename, <small class="dots">...</small></var>)<var><a name="index-imwrite-2891"></a></var><br> <blockquote><p>Write images in various file formats. <p>If <var>fmt</var> is not supplied, the file extension of <var>filename</var> is used to determine the format. <p>The parameter-value pairs (<var>p1</var>, <var>v1</var>, <small class="dots">...</small>) are optional. Currently the following options are supported for <tt>JPEG</tt> images: <dl> <dt>‘<samp><span class="samp">Quality</span></samp>’<dd>Set the quality of the compression. The value should be an integer between 0 and 100, with larger values indicating higher visual quality and lower compression. </dl> <p><strong>Supported Formats</strong> <p><table summary=""><tr align="left"><th valign="top" width="33%">Extension </th><th valign="top" width="66%">Format <br></th></tr><tr align="left"><td valign="top" width="33%">bmp </td><td valign="top" width="66%">Windows Bitmap <br></td></tr><tr align="left"><td valign="top" width="33%">gif </td><td valign="top" width="66%">Graphics Interchange Format <br></td></tr><tr align="left"><td valign="top" width="33%">jpg and jpeg </td><td valign="top" width="66%">Joint Photographic Experts Group <br></td></tr><tr align="left"><td valign="top" width="33%">pbm </td><td valign="top" width="66%">Portable Bitmap <br></td></tr><tr align="left"><td valign="top" width="33%">pcx </td><td valign="top" width="66%"> <br></td></tr><tr align="left"><td valign="top" width="33%">pgm </td><td valign="top" width="66%">Portable Graymap <br></td></tr><tr align="left"><td valign="top" width="33%">png </td><td valign="top" width="66%">Portable Network Graphics <br></td></tr><tr align="left"><td valign="top" width="33%">pnm </td><td valign="top" width="66%">Portable Anymap <br></td></tr><tr align="left"><td valign="top" width="33%">ppm </td><td valign="top" width="66%">Portable Pixmap <br></td></tr><tr align="left"><td valign="top" width="33%">ras </td><td valign="top" width="66%">Sun Raster <br></td></tr><tr align="left"><td valign="top" width="33%">tif and tiff </td><td valign="top" width="66%">Tagged Image File Format <br></td></tr><tr align="left"><td valign="top" width="33%">xwd </td><td valign="top" width="66%">X11 Dump <br></td></tr></table> <p><strong>Unsupported Formats</strong> <p><table summary=""><tr align="left"><th valign="top" width="33%">Extension </th><th valign="top" width="66%">Format <br></th></tr><tr align="left"><td valign="top" width="33%">hdf </td><td valign="top" width="66%">Hierarchical Data Format V4 <br></td></tr><tr align="left"><td valign="top" width="33%">jp2 and jpx </td><td valign="top" width="66%">Joint Photographic Experts Group 2000 <br></td></tr></table> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dimread.html#doc_002dimread">imread</a>, <a href="doc_002dimfinfo.html#doc_002dimfinfo">imfinfo</a>. </p></blockquote></div> <!-- IMAGE_PATH src/defaults.cc --> <p><a name="doc_002dIMAGE_005fPATH"></a> <div class="defun"> — Built-in Function: <var>val</var> = <b>IMAGE_PATH</b> ()<var><a name="index-IMAGE_005fPATH-2892"></a></var><br> — Built-in Function: <var>old_val</var> = <b>IMAGE_PATH</b> (<var>new_val</var>)<var><a name="index-IMAGE_005fPATH-2893"></a></var><br> — Built-in Function: <b>IMAGE_PATH</b> (<var>new_val, "local"</var>)<var><a name="index-IMAGE_005fPATH-2894"></a></var><br> <blockquote><p>Query or set the internal variable that specifies a colon separated list of directories in which to search for image files. <p>When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function. </p></blockquote></div> <p>It is possible to get information about an image file on disk, without actually reading it into Octave. This is done using the <code>imfinfo</code> function which provides read access to many of the parameters stored in the header of the image file. <!-- imfinfo scripts/image/imfinfo.m --> <p><a name="doc_002dimfinfo"></a> <div class="defun"> — Function File: <var>info</var> = <b>imfinfo</b> (<var>filename</var>)<var><a name="index-imfinfo-2895"></a></var><br> — Function File: <var>info</var> = <b>imfinfo</b> (<var>url</var>)<var><a name="index-imfinfo-2896"></a></var><br> <blockquote><p>Read image information from a file. <p><code>imfinfo</code> returns a structure containing information about the image stored in the file <var>filename</var>. The output structure contains the following fields. <dl> <dt>‘<samp><span class="samp">Filename</span></samp>’<dd>The full name of the image file. <br><dt>‘<samp><span class="samp">FileSize</span></samp>’<dd>Number of bytes of the image on disk <br><dt>‘<samp><span class="samp">FileModDate</span></samp>’<dd>Date of last modification to the file. <br><dt>‘<samp><span class="samp">Height</span></samp>’<dd>Image height in pixels. <br><dt>‘<samp><span class="samp">Width</span></samp>’<dd>Image Width in pixels. <br><dt>‘<samp><span class="samp">BitDepth</span></samp>’<dd>Number of bits per channel per pixel. <br><dt>‘<samp><span class="samp">Format</span></samp>’<dd>Image format (e.g., <code>"jpeg"</code>). <br><dt>‘<samp><span class="samp">LongFormat</span></samp>’<dd>Long form image format description. <br><dt>‘<samp><span class="samp">XResolution</span></samp>’<dd>X resolution of the image. <br><dt>‘<samp><span class="samp">YResolution</span></samp>’<dd>Y resolution of the image. <br><dt>‘<samp><span class="samp">TotalColors</span></samp>’<dd>Number of unique colors in the image. <br><dt>‘<samp><span class="samp">TileName</span></samp>’<dd>Tile name. <br><dt>‘<samp><span class="samp">AnimationDelay</span></samp>’<dd>Time in 1/100ths of a second (0 to 65535) which must expire before displaying the next image in an animated sequence. <br><dt>‘<samp><span class="samp">AnimationIterations</span></samp>’<dd>Number of iterations to loop an animation (e.g., Netscape loop extension) for. <br><dt>‘<samp><span class="samp">ByteOrder</span></samp>’<dd>Endian option for formats that support it. Is either <code>"little-endian"</code>, <code>"big-endian"</code>, or <code>"undefined"</code>. <br><dt>‘<samp><span class="samp">Gamma</span></samp>’<dd>Gamma level of the image. The same color image displayed on two different workstations may look different due to differences in the display monitor. <br><dt>‘<samp><span class="samp">Matte</span></samp>’<dd><code>true</code> if the image has transparency. <br><dt>‘<samp><span class="samp">ModulusDepth</span></samp>’<dd>Image modulus depth (minimum number of bits required to support red/green/blue components without loss of accuracy). <br><dt>‘<samp><span class="samp">Quality</span></samp>’<dd>JPEG/MIFF/PNG compression level. <br><dt>‘<samp><span class="samp">QuantizeColors</span></samp>’<dd>Preferred number of colors in the image. <br><dt>‘<samp><span class="samp">ResolutionUnits</span></samp>’<dd>Units of image resolution. Is either <code>"pixels per inch"</code>, <code>"pixels per centimeter"</code>, or <code>"undefined"</code>. <br><dt>‘<samp><span class="samp">ColorType</span></samp>’<dd>Image type. Is either <code>"grayscale"</code>, <code>"indexed"</code>, <code>"truecolor"</code>, or <code>"undefined"</code>. <br><dt>‘<samp><span class="samp">View</span></samp>’<dd>FlashPix viewing parameters. </dl> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dimread.html#doc_002dimread">imread</a>, <a href="doc_002dimwrite.html#doc_002dimwrite">imwrite</a>. </p></blockquote></div> </body></html>