<!DOCTYPE html> <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>ImageMath Module — Pillow (PIL Fork) 5.4.1 documentation</title> <script type="text/javascript" src="../_static/js/modernizr.min.js"></script> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/language_data.js"></script> <script type="text/javascript" src="../_static/js/script.js"></script> <script type="text/javascript" src="../_static/js/theme.js"></script> <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="next" title="ImageMorph Module" href="ImageMorph.html" /> <link rel="prev" title="ImageGrab Module (macOS and Windows only)" href="ImageGrab.html" /> </head> <body class="wy-body-for-nav"> <div class="wy-grid-for-nav"> <nav data-toggle="wy-nav-shift" class="wy-nav-side"> <div class="wy-side-scroll"> <div class="wy-side-nav-search" > <a href="../index.html" class="icon icon-home"> Pillow (PIL Fork) </a> <div class="version"> 5.4.1 </div> <div role="search"> <form id="rtd-search-form" class="wy-form" action="../search.html" method="get"> <input type="text" name="q" placeholder="Search docs" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> </div> <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> <ul class="current"> <li class="toctree-l1"><a class="reference internal" href="../installation.html">Installation</a></li> <li class="toctree-l1"><a class="reference internal" href="../handbook/index.html">Handbook</a></li> <li class="toctree-l1 current"><a class="reference internal" href="index.html">Reference</a><ul class="current"> <li class="toctree-l2"><a class="reference internal" href="Image.html"><code class="docutils literal notranslate"><span class="pre">Image</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageChops.html"><code class="docutils literal notranslate"><span class="pre">ImageChops</span></code> (“Channel Operations”) Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageColor.html"><code class="docutils literal notranslate"><span class="pre">ImageColor</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageCms.html"><code class="docutils literal notranslate"><span class="pre">ImageCms</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageDraw.html"><code class="docutils literal notranslate"><span class="pre">ImageDraw</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageEnhance.html"><code class="docutils literal notranslate"><span class="pre">ImageEnhance</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageFile.html"><code class="docutils literal notranslate"><span class="pre">ImageFile</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageFilter.html"><code class="docutils literal notranslate"><span class="pre">ImageFilter</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageFont.html"><code class="docutils literal notranslate"><span class="pre">ImageFont</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageGrab.html"><code class="docutils literal notranslate"><span class="pre">ImageGrab</span></code> Module (macOS and Windows only)</a></li> <li class="toctree-l2 current"><a class="current reference internal" href="#"><code class="docutils literal notranslate"><span class="pre">ImageMath</span></code> Module</a><ul> <li class="toctree-l3"><a class="reference internal" href="#example-using-the-imagemath-module">Example: Using the <code class="docutils literal notranslate"><span class="pre">ImageMath</span></code> module</a></li> <li class="toctree-l3"><a class="reference internal" href="#expression-syntax">Expression syntax</a><ul> <li class="toctree-l4"><a class="reference internal" href="#standard-operators">Standard Operators</a></li> <li class="toctree-l4"><a class="reference internal" href="#bitwise-operators">Bitwise Operators</a></li> <li class="toctree-l4"><a class="reference internal" href="#logical-operators">Logical Operators</a></li> <li class="toctree-l4"><a class="reference internal" href="#built-in-functions">Built-in Functions</a></li> </ul> </li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="ImageMorph.html"><code class="docutils literal notranslate"><span class="pre">ImageMorph</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageOps.html"><code class="docutils literal notranslate"><span class="pre">ImageOps</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImagePalette.html"><code class="docutils literal notranslate"><span class="pre">ImagePalette</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImagePath.html"><code class="docutils literal notranslate"><span class="pre">ImagePath</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageQt.html"><code class="docutils literal notranslate"><span class="pre">ImageQt</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageSequence.html"><code class="docutils literal notranslate"><span class="pre">ImageSequence</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageStat.html"><code class="docutils literal notranslate"><span class="pre">ImageStat</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageTk.html"><code class="docutils literal notranslate"><span class="pre">ImageTk</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="ImageWin.html"><code class="docutils literal notranslate"><span class="pre">ImageWin</span></code> Module (Windows-only)</a></li> <li class="toctree-l2"><a class="reference internal" href="ExifTags.html"><code class="docutils literal notranslate"><span class="pre">ExifTags</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="TiffTags.html"><code class="docutils literal notranslate"><span class="pre">TiffTags</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="PSDraw.html"><code class="docutils literal notranslate"><span class="pre">PSDraw</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="PixelAccess.html"><code class="docutils literal notranslate"><span class="pre">PixelAccess</span></code> Class</a></li> <li class="toctree-l2"><a class="reference internal" href="PyAccess.html"><code class="docutils literal notranslate"><span class="pre">PyAccess</span></code> Module</a></li> <li class="toctree-l2"><a class="reference internal" href="../PIL.html">PIL Package (autodoc of remaining modules)</a></li> <li class="toctree-l2"><a class="reference internal" href="plugins.html">Plugin reference</a></li> <li class="toctree-l2"><a class="reference internal" href="internal_design.html">Internal Reference Docs</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="../porting.html">Porting</a></li> <li class="toctree-l1"><a class="reference internal" href="../about.html">About</a></li> <li class="toctree-l1"><a class="reference internal" href="../releasenotes/index.html">Release Notes</a></li> <li class="toctree-l1"><a class="reference internal" href="../deprecations.html">Deprecations and removals</a></li> </ul> </div> </div> </nav> <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> <nav class="wy-nav-top" aria-label="top navigation"> <i data-toggle="wy-nav-top" class="fa fa-bars"></i> <a href="../index.html">Pillow (PIL Fork)</a> </nav> <div class="wy-nav-content"> <div class="rst-content"> <div role="navigation" aria-label="breadcrumbs navigation"> <ul class="wy-breadcrumbs"> <li><a href="../index.html">Docs</a> »</li> <li><a href="index.html">Reference</a> »</li> <li><code class="docutils literal notranslate"><span class="pre">ImageMath</span></code> Module</li> <li class="wy-breadcrumbs-aside"> <a href="../_sources/reference/ImageMath.rst.txt" rel="nofollow"> View page source</a> </li> </ul> <hr/> </div> <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> <div itemprop="articleBody"> <span class="target" id="module-PIL.ImageMath"></span><div class="section" id="imagemath-module"> <h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">ImageMath</span></code> Module<a class="headerlink" href="#imagemath-module" title="Permalink to this headline">¶</a></h1> <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">ImageMath</span></code> module can be used to evaluate “image expressions”. The module provides a single eval function, which takes an expression string and one or more images.</p> <div class="section" id="example-using-the-imagemath-module"> <h2>Example: Using the <a class="reference internal" href="#module-PIL.ImageMath" title="PIL.ImageMath"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ImageMath</span></code></a> module<a class="headerlink" href="#example-using-the-imagemath-module" title="Permalink to this headline">¶</a></h2> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span><span class="p">,</span> <span class="n">ImageMath</span> <span class="n">im1</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"image1.jpg"</span><span class="p">)</span> <span class="n">im2</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"image2.jpg"</span><span class="p">)</span> <span class="n">out</span> <span class="o">=</span> <span class="n">ImageMath</span><span class="o">.</span><span class="n">eval</span><span class="p">(</span><span class="s2">"convert(min(a, b), 'L')"</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="n">im1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="n">im2</span><span class="p">)</span> <span class="n">out</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s2">"result.png"</span><span class="p">)</span> </pre></div> </div> <dl class="function"> <dt id="PIL.ImageMath.eval"> <code class="descclassname">PIL.ImageMath.</code><code class="descname">eval</code><span class="sig-paren">(</span><em>expression</em>, <em>environment</em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/PIL/ImageMath.html#eval"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#PIL.ImageMath.eval" title="Permalink to this definition">¶</a></dt> <dd><p>Evaluate expression in the given environment.</p> <p>In the current version, <a class="reference internal" href="#module-PIL.ImageMath" title="PIL.ImageMath"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ImageMath</span></code></a> only supports single-layer images. To process multi-band images, use the <a class="reference internal" href="Image.html#PIL.Image.Image.split" title="PIL.Image.Image.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code></a> method or <a class="reference internal" href="Image.html#PIL.Image.merge" title="PIL.Image.merge"><code class="xref py py-func docutils literal notranslate"><span class="pre">merge()</span></code></a> function.</p> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> <li><strong>expression</strong> – A string which uses the standard Python expression syntax. In addition to the standard operators, you can also use the functions described below.</li> <li><strong>environment</strong> – A dictionary that maps image names to Image instances. You can use one or more keyword arguments instead of a dictionary, as shown in the above example. Note that the names must be valid Python identifiers.</li> </ul> </td> </tr> <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">An image, an integer value, a floating point value, or a pixel tuple, depending on the expression.</p> </td> </tr> </tbody> </table> </dd></dl> </div> <div class="section" id="expression-syntax"> <h2>Expression syntax<a class="headerlink" href="#expression-syntax" title="Permalink to this headline">¶</a></h2> <p>Expressions are standard Python expressions, but they’re evaluated in a non-standard environment. You can use PIL methods as usual, plus the following set of operators and functions:</p> <div class="section" id="standard-operators"> <h3>Standard Operators<a class="headerlink" href="#standard-operators" title="Permalink to this headline">¶</a></h3> <p>You can use standard arithmetical operators for addition (+), subtraction (-), multiplication (*), and division (/).</p> <p>The module also supports unary minus (-), modulo (%), and power (**) operators.</p> <p>Note that all operations are done with 32-bit integers or 32-bit floating point values, as necessary. For example, if you add two 8-bit images, the result will be a 32-bit integer image. If you add a floating point constant to an 8-bit image, the result will be a 32-bit floating point image.</p> <p>You can force conversion using the <code class="xref py py-func docutils literal notranslate"><span class="pre">convert()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code>, and <code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code> functions described below.</p> </div> <div class="section" id="bitwise-operators"> <h3>Bitwise Operators<a class="headerlink" href="#bitwise-operators" title="Permalink to this headline">¶</a></h3> <p>The module also provides operations that operate on individual bits. This includes and (&), or (|), and exclusive or (^). You can also invert (~) all pixel bits.</p> <p>Note that the operands are converted to 32-bit signed integers before the bitwise operation is applied. This means that you’ll get negative values if you invert an ordinary greyscale image. You can use the and (&) operator to mask off unwanted bits.</p> <p>Bitwise operators don’t work on floating point images.</p> </div> <div class="section" id="logical-operators"> <h3>Logical Operators<a class="headerlink" href="#logical-operators" title="Permalink to this headline">¶</a></h3> <p>Logical operators like <code class="docutils literal notranslate"><span class="pre">and</span></code>, <code class="docutils literal notranslate"><span class="pre">or</span></code>, and <code class="docutils literal notranslate"><span class="pre">not</span></code> work on entire images, rather than individual pixels.</p> <p>An empty image (all pixels zero) is treated as false. All other images are treated as true.</p> <p>Note that <code class="docutils literal notranslate"><span class="pre">and</span></code> and <code class="docutils literal notranslate"><span class="pre">or</span></code> return the last evaluated operand, while not always returns a boolean value.</p> </div> <div class="section" id="built-in-functions"> <h3>Built-in Functions<a class="headerlink" href="#built-in-functions" title="Permalink to this headline">¶</a></h3> <p>These functions are applied to each individual pixel.</p> <dl class="function"> <dt id="abs"> <code class="descname">abs</code><span class="sig-paren">(</span><em>image</em><span class="sig-paren">)</span><a class="headerlink" href="#abs" title="Permalink to this definition">¶</a></dt> <dd><p>Absolute value.</p> </dd></dl> <dl class="function"> <dt id="convert"> <code class="descname">convert</code><span class="sig-paren">(</span><em>image</em>, <em>mode</em><span class="sig-paren">)</span><a class="headerlink" href="#convert" title="Permalink to this definition">¶</a></dt> <dd><p>Convert image to the given mode. The mode must be given as a string constant.</p> </dd></dl> <dl class="function"> <dt id="float"> <code class="descname">float</code><span class="sig-paren">(</span><em>image</em><span class="sig-paren">)</span><a class="headerlink" href="#float" title="Permalink to this definition">¶</a></dt> <dd><p>Convert image to 32-bit floating point. This is equivalent to convert(image, “F”).</p> </dd></dl> <dl class="function"> <dt id="int"> <code class="descname">int</code><span class="sig-paren">(</span><em>image</em><span class="sig-paren">)</span><a class="headerlink" href="#int" title="Permalink to this definition">¶</a></dt> <dd><p>Convert image to 32-bit integer. This is equivalent to convert(image, “I”).</p> <p>Note that 1-bit and 8-bit images are automatically converted to 32-bit integers if necessary to get a correct result.</p> </dd></dl> <dl class="function"> <dt id="max"> <code class="descname">max</code><span class="sig-paren">(</span><em>image1</em>, <em>image2</em><span class="sig-paren">)</span><a class="headerlink" href="#max" title="Permalink to this definition">¶</a></dt> <dd><p>Maximum value.</p> </dd></dl> <dl class="function"> <dt id="min"> <code class="descname">min</code><span class="sig-paren">(</span><em>image1</em>, <em>image2</em><span class="sig-paren">)</span><a class="headerlink" href="#min" title="Permalink to this definition">¶</a></dt> <dd><p>Minimum value.</p> </dd></dl> </div> </div> </div> </div> </div> <footer> <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> <a href="ImageMorph.html" class="btn btn-neutral float-right" title="ImageMorph Module" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a> <a href="ImageGrab.html" class="btn btn-neutral float-left" title="ImageGrab Module (macOS and Windows only)" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a> </div> <hr/> <div role="contentinfo"> <p> © Copyright 1995-2011 Fredrik Lundh, 2010-2018 Alex Clark and Contributors </p> </div> Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. </footer> </div> </div> </section> </div> <script type="text/javascript"> jQuery(function () { SphinxRtdTheme.Navigation.enable(true); }); </script> </body> </html>