<pre><codeclass="python language-python hljs"><Image> = Image.new(<spanclass="hljs-string">'<mode>'</span>, (width, height)) <spanclass="hljs-comment"># Also `color=<int/tuple/str>`.</span>
<Image> = Image.open(<path>) <spanclass="hljs-comment"># Identifies format based on file contents.</span>
<pre><codeclass="python language-python hljs"><Image> = Image.new(<spanclass="hljs-string">'<mode>'</span>, (width, height)) <spanclass="hljs-comment"># Creates new image. Also `color=<int/tuple>`.</span>
<Image> = Image.open(<path>) <spanclass="hljs-comment"># Identifies format based on file's 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 the default preview app.</span>
@ -2272,7 +2272,7 @@ right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'RGBA'</span></code> - 4x8-bit pixels, true color with transparency mask.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'HSV'</span></code> - 3x8-bit pixels, Hue, Saturation, Value color space.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'L'</span></code> - Lightness (i.e. greyscale). Each pixel is an int between 0 and 255.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'RGB'</span></code> - Red, green, blue (i.e. true color). Each pixel is a tuple of three ints.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (forth int) means more transparency.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'HSV'</span></code> - Hue, saturation, value color space.</strong></li>
</ul><div><h3id="examples">Examples</h3><div><h4id="createsapngimageofarainbowgradient">Creates a PNG image of a rainbow gradient:</h4><pre><codeclass="python language-python hljs">WIDTH, HEIGHT = <spanclass="hljs-number">100</span>, <spanclass="hljs-number">100</span>
n_pixels = WIDTH * HEIGHT
hues = (<spanclass="hljs-number">255</span> * i/n_pixels <spanclass="hljs-keyword">for</span> i <spanclass="hljs-keyword">in</span> range(n_pixels))
xxxxxxxxxx