<Image> = Image.open(<path>) <spanclass="hljs-comment"># Identifies format based on file contents.</span>
<Image> = <Image>.convert(<spanclass="hljs-string">'<mode>'</span>) <spanclass="hljs-comment"># Converts image to the new mode.</span>
<Image>.save(<path>) <spanclass="hljs-comment"># Selects format based on the path extension.</span>
<Image>.show() <spanclass="hljs-comment"># Opens image in default preview app.</span>
<Image> = Image.open(<path>) <spanclass="hljs-comment"># Identifies format based on file contents.</span>
<Image> = <Image>.convert(<spanclass="hljs-string">'<mode>'</span>) <spanclass="hljs-comment"># Converts image to the new mode.</span>
<Image>.save(<path>) <spanclass="hljs-comment"># Selects format based on the path extension.</span>
<Image>.show() <spanclass="hljs-comment"># Opens image in default preview app.</span>
</code></pre>
<pre><codeclass="python language-python hljs"><int/tuple> = <Image>.getpixel((x, y)) <spanclass="hljs-comment"># Returns a pixel.</span>
<Image>.putpixel((x, y), <int/tuple>) <spanclass="hljs-comment"># Writes a pixel to the image.</span>
<ImagingCore> = <Image>.getdata() <spanclass="hljs-comment"># Returns a flattened sequence of pixels.</span>
<Image>.putdata(<list/ImagingCore>) <spanclass="hljs-comment"># Writes a flattened sequence of pixels.</span>
<Image>.paste(<Image>, (x, y)) <spanclass="hljs-comment"># Writes passed image to the image.</span>
<pre><codeclass="python language-python hljs"><int/tuple> = <Image>.getpixel((x, y)) <spanclass="hljs-comment"># Returns a pixel.</span>
<Image>.putpixel((x, y), <int/tuple>) <spanclass="hljs-comment"># Writes a pixel to the image.</span>
<ImagingCore> = <Image>.getdata() <spanclass="hljs-comment"># Returns a flattened sequence of pixels.</span>
<Image>.putdata(<list/ImagingCore>) <spanclass="hljs-comment"># Writes a flattened sequence of pixels.</span>
<Image>.paste(<Image>, (x, y)) <spanclass="hljs-comment"># Writes passed image to the image.</span>
</code></pre>
<pre><codeclass="python language-python hljs"><Image> = <Image>.resize((width, height)) <spanclass="hljs-comment"># Use <Image>.width/height for original sizes.</span>
<pre><codeclass="python language-python hljs"><Image> = <Image>.resize((width, height)) <spanclass="hljs-comment"># Use <Image>.width/height for original sizes.</span>
<pre><codeclass="python language-python hljs"><array> = np.array(<Image>) <spanclass="hljs-comment"># Creates NumPy array from the image.</span>
<Image> = Image.fromarray(np.uint8(<array>)) <spanclass="hljs-comment"># Use <array>.clip(0, 255) to clip the values.</span>
<pre><codeclass="python language-python hljs"><array> = np.array(<Image>) <spanclass="hljs-comment"># Creates NumPy array from the image.</span>
<Image> = Image.fromarray(np.uint8(<array>)) <spanclass="hljs-comment"># Use <array>.clip(0, 255) to clip the values.</span>
</code></pre>
<div><h3id="modes-1">Modes</h3><ul>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'1'</span></code> - 1-bit pixels, black and white, stored with one pixel per byte.</strong></li>
<div><h3id="imagedraw">Image Draw</h3><pre><codeclass="python language-python hljs"><ImageDraw> = ImageDraw.Draw(<Image>) <spanclass="hljs-comment"># Object for adding 2D graphics to the image.</span>
<ImageDraw>.point((x, y)) <spanclass="hljs-comment"># Draws a point. Truncates floats into ints.</span>
<ImageDraw>.line((x1, y1, x2, y2 [, ...])) <spanclass="hljs-comment"># To get anti-aliasing use Image's resize().</span>
<ImageDraw>.arc((x1, y1, x2, y2), deg1, deg2) <spanclass="hljs-comment"># Always draws in clockwise direction.</span>
<ImageDraw>.rectangle((x1, y1, x2, y2)) <spanclass="hljs-comment"># To rotate use Image's rotate() and paste().</span>
<ImageDraw>.polygon((x1, y1, x2, y2, ...)) <spanclass="hljs-comment"># Last point gets connected to the first.</span>
<ImageDraw>.ellipse((x1, y1, x2, y2)) <spanclass="hljs-comment"># To rotate use Image's rotate() and paste().</span>
<div><h3id="imagedraw">Image Draw</h3><pre><codeclass="python language-python hljs"><ImageDraw> = ImageDraw.Draw(<Image>) <spanclass="hljs-comment"># Object for adding 2D graphics to the image.</span>
<ImageDraw>.point((x, y)) <spanclass="hljs-comment"># Draws a point. Truncates floats into ints.</span>
<ImageDraw>.line((x1, y1, x2, y2 [, ...])) <spanclass="hljs-comment"># To get anti-aliasing use Image's resize().</span>
<ImageDraw>.arc((x1, y1, x2, y2), deg1, deg2) <spanclass="hljs-comment"># Always draws in clockwise direction.</span>
<ImageDraw>.rectangle((x1, y1, x2, y2)) <spanclass="hljs-comment"># To rotate use Image's rotate() and paste().</span>
<ImageDraw>.polygon((x1, y1, x2, y2, ...)) <spanclass="hljs-comment"># Last point gets connected to the first.</span>
<ImageDraw>.ellipse((x1, y1, x2, y2)) <spanclass="hljs-comment"># To rotate use Image's rotate() and paste().</span>