From 06bc3ec94fc9454adcf0c40f44b8fa9e76ba6d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= 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 {:.<10} # '......' {:0} # '' ``` -* **Objects are rendered using `'format(, )'`.** +* **Objects are rendered using `'format(, "")'`.** * **Options can be generated dynamically: `f'{:{}[…]}'`.** * **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('-', '--', type=) # Option (defa p.add_argument('', type=, nargs=1) # Mandatory first argument. p.add_argument('', type=, nargs='+') # Mandatory remaining args. p.add_argument('', type=, nargs='?/*') # Optional argument/s. - = p.parse_args() # Exits on parsing error. - = . # Returns `()`. +args = p.parse_args() # Exits on parsing error. + = args. # Returns `()`. ``` * **Use `'help='` to set argument description that will be displayed in help message.** @@ -1591,7 +1591,7 @@ Open .seek(0) # Moves to the start of the file. .seek(offset) # Moves 'offset' chars/bytes from the start. .seek(0, 2) # Moves to the end of the file. -.seek(±offset, ) # Anchor: 0 start, 1 current position, 2 end. +.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('

{{title}}

', title=sport) ``` * **Use `'render_template(filename, )'` to render file located in templates dir.** -* **To return an error code use `'abort()'` and to redirect use `'redirect()'`.** +* **To return an error code use `'abort()'` and to redirect use `'redirect("")'`.** * **`'request.args[]'` returns parameter from the query string (URL part after '?').** * **`'session[] = '` stores session data. Needs `'app.secret_key = '`.** @@ -2771,22 +2771,22 @@ from PIL import Image ```python = Image.new('', (width, height)) # Creates new image. Also `color=`. = Image.open() # Identifies format based on file's contents. - = .convert('') # Converts image to the new mode. + = .convert('') # Converts image to the new mode (see Modes). .save() # Selects format based on extension (PNG/JPG…). .show() # Opens image in the default preview app. ``` ```python - = .getpixel((x, y)) # Returns pixel's value (its color). -.putpixel((x, y), ) # Updates pixel's value. - = .getdata() # Returns a flattened view of pixel values. -.putdata() # Updates pixels with a copy of the sequence. + = .getpixel((x, y)) # Returns pixel's value (its color). + = .getdata() # Returns a flattened view of pixel values. +.putpixel((x, y), ) # Updates pixel's value. Clips passed int/s. +.putdata() # Updates pixels with a copy of the sequence. .paste(, (x, y)) # Draws passed image at the specified location. ``` ```python - = .filter() # ` = ImageFilter.()` - = .enhance() # ` = ImageEnhance.()` + = .filter() # Use ImageFilter.() for Filter. + = .enhance() # Use ImageEnhance.() 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 @@
- +
@@ -380,7 +380,7 @@ Point(x=1, y=2
    -
  • 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() method.
  • @@ -1314,8 +1314,8 @@ p.add_argument('-<short_name>', '<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>)`.
      @@ -1349,7 +1349,7 @@ p.add_argument('<name>', type=<type>

    File Object

    <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.
     
    @@ -2108,7 +2108,7 @@ app.run(host=None, port='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>'.
@@ -2264,18 +2264,18 @@ right = [[0.1, 0.1<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. -
<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.
 
-
<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.
 
<array> = np.array(<Image>)                     # Creates a 2d/3d NumPy array from the image.
 <Image> = Image.fromarray(np.uint8(<array>))    # Use `<array>.clip(0, 255)` to clip values.
@@ -2283,7 +2283,7 @@ right = [[0.1,  0.1

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.

Examples

Creates a PNG image of a rainbow gradient:

WIDTH, HEIGHT = 100, 100
 n_pixels = WIDTH * HEIGHT
@@ -2931,7 +2931,7 @@ $ deactivate                # Deactivates the active