From 06bc3ec94fc9454adcf0c40f44b8fa9e76ba6d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= <sornjure@gmail.com> Date: Wed, 2 Oct 2024 19:58:50 +0200 Subject: [PATCH] Format, Command line argumetns, Open, Web, Image --- README.md | 26 +++++++++++++------------- index.html | 30 +++++++++++++++--------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 3fa3208..ae047b5 100644 --- a/README.md +++ b/README.md @@ -412,7 +412,7 @@ Format {<el>:.<10} # '<el>......' {<el>:0} # '<el>' ``` -* **Objects are rendered using `'format(<el>, <options>)'`.** +* **Objects are rendered using `'format(<el>, "<options>")'`.** * **Options can be generated dynamically: `f'{<el>:{<str/int>}[…]}'`.** * **Adding `'='` to the expression prepends it to the output: `f'{1+1=}'` returns `'1+1=2'`.** * **Adding `'!r'` to the expression converts object to string by calling its [repr()](#class) method.** @@ -1550,8 +1550,8 @@ p.add_argument('-<short_name>', '--<name>', type=<type>) # Option (defa p.add_argument('<name>', type=<type>, nargs=1) # Mandatory first argument. p.add_argument('<name>', type=<type>, nargs='+') # Mandatory remaining args. p.add_argument('<name>', type=<type>, nargs='?/*') # Optional argument/s. -<args> = p.parse_args() # Exits on parsing error. -<obj> = <args>.<name> # Returns `<type>(<arg>)`. +args = p.parse_args() # Exits on parsing error. +<obj> = args.<name> # Returns `<type>(<arg>)`. ``` * **Use `'help=<str>'` to set argument description that will be displayed in help message.** @@ -1591,7 +1591,7 @@ Open <file>.seek(0) # Moves to the start of the file. <file>.seek(offset) # Moves 'offset' chars/bytes from the start. <file>.seek(0, 2) # Moves to the end of the file. -<bin_file>.seek(±offset, <anchor>) # Anchor: 0 start, 1 current position, 2 end. +<bin_file>.seek(±offset, origin) # Origin: 0 start, 1 current position, 2 end. ``` ```python @@ -2568,7 +2568,7 @@ def serve_html(sport): return flask.render_template_string('<h1>{{title}}</h1>', title=sport) ``` * **Use `'render_template(filename, <kwargs>)'` to render file located in templates dir.** -* **To return an error code use `'abort(<int>)'` and to redirect use `'redirect(<url>)'`.** +* **To return an error code use `'abort(<int>)'` and to redirect use `'redirect("<url>")'`.** * **`'request.args[<str>]'` returns parameter from the query string (URL part after '?').** * **`'session[<str>] = <obj>'` stores session data. Needs `'app.secret_key = <str>'`.** @@ -2771,22 +2771,22 @@ from PIL import Image ```python <Image> = Image.new('<mode>', (width, height)) # Creates new image. Also `color=<int/tuple>`. <Image> = Image.open(<path>) # Identifies format based on file's contents. -<Image> = <Image>.convert('<mode>') # Converts image to the new mode. +<Image> = <Image>.convert('<mode>') # Converts image to the new mode (see Modes). <Image>.save(<path>) # Selects format based on extension (PNG/JPG…). <Image>.show() # Opens image in the default preview app. ``` ```python -<int/tuple> = <Image>.getpixel((x, y)) # Returns pixel's value (its color). -<Image>.putpixel((x, y), <int/tuple>) # Updates pixel's value. -<ImagingCore> = <Image>.getdata() # Returns a flattened view of pixel values. -<Image>.putdata(<list/ImagingCore>) # Updates pixels with a copy of the sequence. +<int/tup> = <Image>.getpixel((x, y)) # Returns pixel's value (its color). +<ImgCore> = <Image>.getdata() # Returns a flattened view of pixel values. +<Image>.putpixel((x, y), <int/tuple>) # Updates pixel's value. Clips passed int/s. +<Image>.putdata(<list/ImgCore>) # Updates pixels with a copy of the sequence. <Image>.paste(<Image>, (x, y)) # Draws passed image at the specified location. ``` ```python -<Image> = <Image>.filter(<Filter>) # `<Filter> = ImageFilter.<name>(<args>)` -<Image> = <Enhance>.enhance(<float>) # `<Enhance> = ImageEnhance.<name>(<Image>)` +<Image> = <Image>.filter(<Filter>) # Use ImageFilter.<name>(<args>) for Filter. +<Image> = <Enhance>.enhance(<float>) # Use ImageEnhance.<name>(<Image>) for Enhance. ``` ```python @@ -2797,7 +2797,7 @@ from PIL import Image ### Modes * **`'L'` - Lightness (greyscale image). Each pixel is an int between 0 and 255.** * **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three ints.** -* **`'RGBA'` - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.** +* **`'RGBA'` - RGB with alpha. Low alpha (i.e. forth int) makes pixels more transparent.** * **`'HSV'` - Hue, saturation, value. Three ints representing color in HSV color space.** diff --git a/index.html b/index.html index 2d4c440..3328dd4 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@ <body> <header> - <aside>September 27, 2024</aside> + <aside>October 2, 2024</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </header> @@ -380,7 +380,7 @@ Point(x=<span class="hljs-number">1</span>, y=<span class="hljs-number">2</span> </code></pre></div> <ul> -<li><strong>Objects are rendered using <code class="python hljs"><span class="hljs-string">'format(<el>, <options>)'</span></code>.</strong></li> +<li><strong>Objects are rendered using <code class="python hljs"><span class="hljs-string">'format(<el>, "<options>")'</span></code>.</strong></li> <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> <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> <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,8 +1314,8 @@ p.add_argument(<span class="hljs-string">'-<short_name>'</span>, <span cla 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> 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> 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> -<args> = p.parse_args() <span class="hljs-comment"># Exits on parsing error.</span> -<obj> = <args>.<name> <span class="hljs-comment"># Returns `<type>(<arg>)`.</span> +args = p.parse_args() <span class="hljs-comment"># Exits on parsing error.</span> +<obj> = args.<name> <span class="hljs-comment"># Returns `<type>(<arg>)`.</span> </code></pre></div> <ul> @@ -1349,7 +1349,7 @@ p.add_argument(<span class="hljs-string">'<name>'</span>, type=<type> </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> <file>.seek(offset) <span class="hljs-comment"># Moves 'offset' chars/bytes from the start.</span> <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> -<bin_file>.seek(±offset, <anchor>) <span class="hljs-comment"># Anchor: 0 start, 1 current position, 2 end.</span> +<bin_file>.seek(±offset, origin) <span class="hljs-comment"># Origin: 0 start, 1 current position, 2 end.</span> </code></pre></div></div></div> @@ -2108,7 +2108,7 @@ app.run(host=<span class="hljs-keyword">None</span>, port=<span class="hljs-keyw <ul> <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> -<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> +<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> <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> <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> </ul> @@ -2264,18 +2264,18 @@ right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</ <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> <Image> = Image.open(<path>) <span class="hljs-comment"># Identifies format based on file's contents.</span> -<Image> = <Image>.convert(<span class="hljs-string">'<mode>'</span>) <span class="hljs-comment"># Converts image to the new mode.</span> +<Image> = <Image>.convert(<span class="hljs-string">'<mode>'</span>) <span class="hljs-comment"># Converts image to the new mode (see Modes).</span> <Image>.save(<path>) <span class="hljs-comment"># Selects format based on extension (PNG/JPG…).</span> <Image>.show() <span class="hljs-comment"># Opens image in the default preview app.</span> </code></pre> -<pre><code class="python language-python hljs"><int/tuple> = <Image>.getpixel((x, y)) <span class="hljs-comment"># Returns pixel's value (its color).</span> -<Image>.putpixel((x, y), <int/tuple>) <span class="hljs-comment"># Updates pixel's value.</span> -<ImagingCore> = <Image>.getdata() <span class="hljs-comment"># Returns a flattened view of pixel values.</span> -<Image>.putdata(<list/ImagingCore>) <span class="hljs-comment"># Updates pixels with a copy of the sequence.</span> +<pre><code class="python language-python hljs"><int/tup> = <Image>.getpixel((x, y)) <span class="hljs-comment"># Returns pixel's value (its color).</span> +<ImgCore> = <Image>.getdata() <span class="hljs-comment"># Returns a flattened view of pixel values.</span> +<Image>.putpixel((x, y), <int/tuple>) <span class="hljs-comment"># Updates pixel's value. Clips passed int/s.</span> +<Image>.putdata(<list/ImgCore>) <span class="hljs-comment"># Updates pixels with a copy of the sequence.</span> <Image>.paste(<Image>, (x, y)) <span class="hljs-comment"># Draws passed image at the specified location.</span> </code></pre> -<pre><code class="python language-python hljs"><Image> = <Image>.filter(<Filter>) <span class="hljs-comment"># `<Filter> = ImageFilter.<name>(<args>)`</span> -<Image> = <Enhance>.enhance(<float>) <span class="hljs-comment"># `<Enhance> = ImageEnhance.<name>(<Image>)`</span> +<pre><code class="python language-python hljs"><Image> = <Image>.filter(<Filter>) <span class="hljs-comment"># Use ImageFilter.<name>(<args>) for Filter.</span> +<Image> = <Enhance>.enhance(<float>) <span class="hljs-comment"># Use ImageEnhance.<name>(<Image>) for Enhance.</span> </code></pre> <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> <Image> = Image.fromarray(np.uint8(<array>)) <span class="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><h3 id="modes-1">Modes</h3><ul> <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> <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> -<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> +<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> <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> </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> n_pixels = WIDTH * HEIGHT @@ -2931,7 +2931,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active <footer> - <aside>September 27, 2024</aside> + <aside>October 2, 2024</aside> <a href="https://gto76.github.io" rel="author">Jure Šorn</a> </footer>