|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>September 27, 2024</aside> |
| 57 | + <aside>October 2, 2024</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
380 | 380 | </code></pre></div>
|
381 | 381 |
|
382 | 382 | <ul>
|
383 |
| -<li><strong>Objects are rendered using <code class="python hljs"><span class="hljs-string">'format(<el>, <options>)'</span></code>.</strong></li> |
| 383 | +<li><strong>Objects are rendered using <code class="python hljs"><span class="hljs-string">'format(<el>, "<options>")'</span></code>.</strong></li> |
384 | 384 | <li><strong>Options can be generated dynamically: <code class="python hljs"><span class="hljs-string">f'<span class="hljs-subst">{<el>:{<str/int>}</span>[…]}'</span></code>.</strong></li>
|
385 | 385 | <li><strong>Adding <code class="python hljs"><span class="hljs-string">'='</span></code> to the expression prepends it to the output: <code class="python hljs"><span class="hljs-string">f'<span class="hljs-subst">{<span class="hljs-number">1</span>+<span class="hljs-number">1</span>=}</span>'</span></code> returns <code class="python hljs"><span class="hljs-string">'1+1=2'</span></code>.</strong></li>
|
386 | 386 | <li><strong>Adding <code class="python hljs"><span class="hljs-string">'!r'</span></code> to the expression converts object to string by calling its <a href="#class">repr()</a> method.</strong></li>
|
|
1314 | 1314 | p.add_argument(<span class="hljs-string">'<name>'</span>, type=<type>, nargs=<span class="hljs-number">1</span>) <span class="hljs-comment"># Mandatory first argument.</span>
|
1315 | 1315 | p.add_argument(<span class="hljs-string">'<name>'</span>, type=<type>, nargs=<span class="hljs-string">'+'</span>) <span class="hljs-comment"># Mandatory remaining args.</span>
|
1316 | 1316 | p.add_argument(<span class="hljs-string">'<name>'</span>, type=<type>, nargs=<span class="hljs-string">'?/*'</span>) <span class="hljs-comment"># Optional argument/s.</span>
|
1317 |
| -<args> = p.parse_args() <span class="hljs-comment"># Exits on parsing error.</span> |
1318 |
| -<obj> = <args>.<name> <span class="hljs-comment"># Returns `<type>(<arg>)`.</span> |
| 1317 | +args = p.parse_args() <span class="hljs-comment"># Exits on parsing error.</span> |
| 1318 | +<obj> = args.<name> <span class="hljs-comment"># Returns `<type>(<arg>)`.</span> |
1319 | 1319 | </code></pre></div>
|
1320 | 1320 |
|
1321 | 1321 | <ul>
|
|
1349 | 1349 | </ul><div><h3 id="fileobject">File Object</h3><pre><code class="python language-python hljs"><file>.seek(<span class="hljs-number">0</span>) <span class="hljs-comment"># Moves to the start of the file.</span>
|
1350 | 1350 | <file>.seek(offset) <span class="hljs-comment"># Moves 'offset' chars/bytes from the start.</span>
|
1351 | 1351 | <file>.seek(<span class="hljs-number">0</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># Moves to the end of the file.</span>
|
1352 |
| -<bin_file>.seek(±offset, <anchor>) <span class="hljs-comment"># Anchor: 0 start, 1 current position, 2 end.</span> |
| 1352 | +<bin_file>.seek(±offset, origin) <span class="hljs-comment"># Origin: 0 start, 1 current position, 2 end.</span> |
1353 | 1353 | </code></pre></div></div></div>
|
1354 | 1354 |
|
1355 | 1355 |
|
@@ -2108,7 +2108,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2108 | 2108 |
|
2109 | 2109 | <ul>
|
2110 | 2110 | <li><strong>Use <code class="python hljs"><span class="hljs-string">'render_template(filename, <kwargs>)'</span></code> to render file located in templates dir.</strong></li>
|
2111 |
| -<li><strong>To return an error code use <code class="python hljs"><span class="hljs-string">'abort(<int>)'</span></code> and to redirect use <code class="python hljs"><span class="hljs-string">'redirect(<url>)'</span></code>.</strong></li> |
| 2111 | +<li><strong>To return an error code use <code class="python hljs"><span class="hljs-string">'abort(<int>)'</span></code> and to redirect use <code class="python hljs"><span class="hljs-string">'redirect("<url>")'</span></code>.</strong></li> |
2112 | 2112 | <li><strong><code class="python hljs"><span class="hljs-string">'request.args[<str>]'</span></code> returns parameter from the query string (URL part after '?').</strong></li>
|
2113 | 2113 | <li><strong><code class="python hljs"><span class="hljs-string">'session[<str>] = <obj>'</span></code> stores session data. Needs <code class="python hljs"><span class="hljs-string">'app.secret_key = <str>'</span></code>.</strong></li>
|
2114 | 2114 | </ul>
|
@@ -2264,26 +2264,26 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2264 | 2264 |
|
2265 | 2265 | <pre><code class="python language-python hljs"><Image> = Image.new(<span class="hljs-string">'<mode>'</span>, (width, height)) <span class="hljs-comment"># Creates new image. Also `color=<int/tuple>`.</span>
|
2266 | 2266 | <Image> = Image.open(<path>) <span class="hljs-comment"># Identifies format based on file's contents.</span>
|
2267 |
| -<Image> = <Image>.convert(<span class="hljs-string">'<mode>'</span>) <span class="hljs-comment"># Converts image to the new mode.</span> |
| 2267 | +<Image> = <Image>.convert(<span class="hljs-string">'<mode>'</span>) <span class="hljs-comment"># Converts image to the new mode (see Modes).</span> |
2268 | 2268 | <Image>.save(<path>) <span class="hljs-comment"># Selects format based on extension (PNG/JPG…).</span>
|
2269 | 2269 | <Image>.show() <span class="hljs-comment"># Opens image in the default preview app.</span>
|
2270 | 2270 | </code></pre>
|
2271 |
| -<pre><code class="python language-python hljs"><int/tuple> = <Image>.getpixel((x, y)) <span class="hljs-comment"># Returns pixel's value (its color).</span> |
2272 |
| -<Image>.putpixel((x, y), <int/tuple>) <span class="hljs-comment"># Updates pixel's value.</span> |
2273 |
| -<ImagingCore> = <Image>.getdata() <span class="hljs-comment"># Returns a flattened view of pixel values.</span> |
2274 |
| -<Image>.putdata(<list/ImagingCore>) <span class="hljs-comment"># Updates pixels with a copy of the sequence.</span> |
| 2271 | +<pre><code class="python language-python hljs"><int/tup> = <Image>.getpixel((x, y)) <span class="hljs-comment"># Returns pixel's value (its color).</span> |
| 2272 | +<ImgCore> = <Image>.getdata() <span class="hljs-comment"># Returns a flattened view of pixel values.</span> |
| 2273 | +<Image>.putpixel((x, y), <int/tuple>) <span class="hljs-comment"># Updates pixel's value. Clips passed int/s.</span> |
| 2274 | +<Image>.putdata(<list/ImgCore>) <span class="hljs-comment"># Updates pixels with a copy of the sequence.</span> |
2275 | 2275 | <Image>.paste(<Image>, (x, y)) <span class="hljs-comment"># Draws passed image at the specified location.</span>
|
2276 | 2276 | </code></pre>
|
2277 |
| -<pre><code class="python language-python hljs"><Image> = <Image>.filter(<Filter>) <span class="hljs-comment"># `<Filter> = ImageFilter.<name>(<args>)`</span> |
2278 |
| -<Image> = <Enhance>.enhance(<float>) <span class="hljs-comment"># `<Enhance> = ImageEnhance.<name>(<Image>)`</span> |
| 2277 | +<pre><code class="python language-python hljs"><Image> = <Image>.filter(<Filter>) <span class="hljs-comment"># Use ImageFilter.<name>(<args>) for Filter.</span> |
| 2278 | +<Image> = <Enhance>.enhance(<float>) <span class="hljs-comment"># Use ImageEnhance.<name>(<Image>) for Enhance.</span> |
2279 | 2279 | </code></pre>
|
2280 | 2280 | <pre><code class="python language-python hljs"><array> = np.array(<Image>) <span class="hljs-comment"># Creates a 2d/3d NumPy array from the image.</span>
|
2281 | 2281 | <Image> = Image.fromarray(np.uint8(<array>)) <span class="hljs-comment"># Use `<array>.clip(0, 255)` to clip values.</span>
|
2282 | 2282 | </code></pre>
|
2283 | 2283 | <div><h3 id="modes-1">Modes</h3><ul>
|
2284 | 2284 | <li><strong><code class="python hljs"><span class="hljs-string">'L'</span></code> - Lightness (greyscale image). Each pixel is an int between 0 and 255.</strong></li>
|
2285 | 2285 | <li><strong><code class="python hljs"><span class="hljs-string">'RGB'</span></code> - Red, green, blue (true color image). Each pixel is a tuple of three ints.</strong></li>
|
2286 |
| -<li><strong><code class="python hljs"><span class="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.</strong></li> |
| 2286 | +<li><strong><code class="python hljs"><span class="hljs-string">'RGBA'</span></code> - RGB with alpha. Low alpha (i.e. forth int) makes pixels more transparent.</strong></li> |
2287 | 2287 | <li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - Hue, saturation, value. Three ints representing color in HSV color space.</strong></li>
|
2288 | 2288 | </ul><div><h3 id="examples">Examples</h3><div><h4 id="createsapngimageofarainbowgradient">Creates a PNG image of a rainbow gradient:</h4><pre><code class="python language-python hljs">WIDTH, HEIGHT = <span class="hljs-number">100</span>, <span class="hljs-number">100</span>
|
2289 | 2289 | n_pixels = WIDTH * HEIGHT
|
@@ -2931,7 +2931,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2931 | 2931 |
|
2932 | 2932 |
|
2933 | 2933 | <footer>
|
2934 |
| - <aside>September 27, 2024</aside> |
| 2934 | + <aside>October 2, 2024</aside> |
2935 | 2935 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2936 | 2936 | </footer>
|
2937 | 2937 |
|
|
0 commit comments