<li><strong>Objects are rendered using <codeclass="python hljs"><spanclass="hljs-string">'format(<el>, <options>)'</span></code>.</strong></li>
<li><strong>Objects are rendered using <codeclass="python hljs"><spanclass="hljs-string">'format(<el>, "<options>")'</span></code>.</strong></li>
<li><strong>Options can be generated dynamically: <codeclass="python hljs"><spanclass="hljs-string">f'<spanclass="hljs-subst">{<el>:{<str/int>}</span>[…]}'</span></code>.</strong></li>
<li><strong>Adding <codeclass="python hljs"><spanclass="hljs-string">'='</span></code> to the expression prepends it to the output: <codeclass="python hljs"><spanclass="hljs-string">f'<spanclass="hljs-subst">{<spanclass="hljs-number">1</span>+<spanclass="hljs-number">1</span>=}</span>'</span></code> returns <codeclass="python hljs"><spanclass="hljs-string">'1+1=2'</span></code>.</strong></li>
<li><strong>Adding <codeclass="python hljs"><spanclass="hljs-string">'!r'</span></code> to the expression converts object to string by calling its <ahref="#class">repr()</a> method.</strong></li>
</ul><div><h3id="fileobject">File Object</h3><pre><codeclass="python language-python hljs"><file>.seek(<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Moves to the start of the file.</span>
<file>.seek(offset) <spanclass="hljs-comment"># Moves 'offset' chars/bytes from the start.</span>
<file>.seek(<spanclass="hljs-number">0</span>, <spanclass="hljs-number">2</span>) <spanclass="hljs-comment"># Moves to the end of the file.</span>
<bin_file>.seek(±offset, <anchor>)<spanclass="hljs-comment"># Anchor: 0 start, 1 current position, 2 end.</span>
<li><strong>Use <codeclass="python hljs"><spanclass="hljs-string">'render_template(filename, <kwargs>)'</span></code> to render file located in templates dir.</strong></li>
<li><strong>To return an error code use <codeclass="python hljs"><spanclass="hljs-string">'abort(<int>)'</span></code> and to redirect use <codeclass="python hljs"><spanclass="hljs-string">'redirect(<url>)'</span></code>.</strong></li>
<li><strong>To return an error code use <codeclass="python hljs"><spanclass="hljs-string">'abort(<int>)'</span></code> and to redirect use <codeclass="python hljs"><spanclass="hljs-string">'redirect("<url>")'</span></code>.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'request.args[<str>]'</span></code> returns parameter from the query string (URL part after '?').</strong></li>
@ -2264,18 +2264,18 @@ right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</
<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> = <Image>.convert(<spanclass="hljs-string">'<mode>'</span>) <spanclass="hljs-comment"># Converts image to the new mode (see Modes).</span>
<Image>.save(<path>) <spanclass="hljs-comment"># Selects format based on extension (PNG/JPG…).</span>
<Image>.show() <spanclass="hljs-comment"># Opens image in the default preview app.</span>
<pre><codeclass="python language-python hljs"><Image> = <Image>.filter(<Filter>) <spanclass="hljs-comment"># Use ImageFilter.<name>(<args>) for Filter.</span>
<Image> = <Enhance>.enhance(<float>) <spanclass="hljs-comment"># Use ImageEnhance.<name>(<Image>) for Enhance.</span>
</code></pre>
<pre><codeclass="python language-python hljs"><array> = np.array(<Image>) <spanclass="hljs-comment"># Creates a 2d/3d NumPy array from the image.</span>
<Image> = Image.fromarray(np.uint8(<array>)) <spanclass="hljs-comment"># Use `<array>.clip(0, 255)` to clip values.</span>
@ -2283,7 +2283,7 @@ right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</
<div><h3id="modes-1">Modes</h3><ul>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'L'</span></code> - Lightness (greyscale image). 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 (true color image). 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 (i.e. forth int) makes pixel more transparent.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (i.e. forth int) makes pixels more transparent.</strong></li>
<li><strong><codeclass="python hljs"><spanclass="hljs-string">'HSV'</span></code> - Hue, saturation, value. Three ints representing color in HSV 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
@ -2931,7 +2931,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active