<html lang="en"> <head> <title>Loading and Saving Images - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <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">31.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> <!-- ./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-2182"></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. Grey-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 grey 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> <!-- ./image/imwrite.m --> <p><a name="doc_002dimwrite"></a> <div class="defun"> — Function File: <b>imwrite</b> (<var>img, filename, fmt, p1, v1, <small class="dots">...</small></var>)<var><a name="index-imwrite-2183"></a></var><br> — Function File: <b>imwrite</b> (<var>img, map, filename, fmt, p1, v1, <small class="dots">...</small></var>)<var><a name="index-imwrite-2184"></a></var><br> <blockquote><p>Write images in various file formats. <p>If <var>fmt</var> is missing, the file extension (if any) 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>Sets the quality of the compression. The corresponding value should be an integer between 0 and 100, with larger values meaning higher visual quality and less compression. </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_002dimfinfo.html#doc_002dimfinfo">imfinfo</a>. </p></blockquote></div> <!-- 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-2185"></a></var><br> — Built-in Function: <var>old_val</var> = <b>IMAGE_PATH</b> (<var>new_val</var>)<var><a name="index-IMAGE_005fPATH-2186"></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></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. <!-- ./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-2187"></a></var><br> — Function File: <var>info</var> = <b>imfinfo</b> (<var>url</var>)<var><a name="index-imfinfo-2188"></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>