**List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be swapped with byteswap() method.**
**List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be reversed with byteswap() method.**
```python
```python
from array import array
from array import array
@ -2042,14 +2042,14 @@ from array import array
```python
```python
<array> = array('<typecode>', <coll_of_nums>) # Array from collection of numbers.
<array> = array('<typecode>', <coll_of_nums>) # Array from collection of numbers.
<array> = array('<typecode>', <bytes>) # Array from bytes object.
<array> = array('<typecode>', <bytes>) # Copies bytes to array's memory.
<array> = array('<typecode>', <array>) # Treats array as a sequence of numbers.
<array> = array('<typecode>', <array>) # Treats array as a sequence of numbers.
<array>.fromfile(<file>, n_items) # Appends items from the binary file.
<array>.fromfile(<file>, n_items) # Appends items from binary file.
```
```
```python
```python
<bytes> = bytes(<array>) # Returns a copy of array's memory.
<bytes> = bytes(<array>) # Returns a copy of array's memory.
<file>.write(<array>) # Writes array's memory to the file.
<file>.write(<array>) # Writes array's memory to binary file.
```
```
@ -2059,7 +2059,7 @@ Memory View
```python
```python
<mview> = memoryview(<bytes/bytearray/array>) # Immutable if bytes, else mutable.
<mview> = memoryview(<bytes/bytearray/array>) # Immutable if bytes, else mutable.
<obj> = <mview>[index] # Returns int/float (bytes if format is 'c').
<mview> = <mview>[<slice>] # Returns mview with rearranged elements.
<mview> = <mview>[<slice>] # Returns mview with rearranged elements.
<mview> = <mview>.cast('<typecode>') # Only works between B/b/c and other types.
<mview> = <mview>.cast('<typecode>') # Only works between B/b/c and other types.
<mview>.release() # Releases memory buffer of the base object.
<mview>.release() # Releases memory buffer of the base object.
@ -2069,7 +2069,7 @@ Memory View
<bytes> = bytes(<mview>) # Returns a new bytes object.
<bytes> = bytes(<mview>) # Returns a new bytes object.
<bytes> = <bytes>.join(<coll_of_mviews>) # Joins mviews using bytes as a separator.
<bytes> = <bytes>.join(<coll_of_mviews>) # Joins mviews using bytes as a separator.
<array> = array('<typecode>', <mview>) # Treats mview as a sequence of numbers.
<array> = array('<typecode>', <mview>) # Treats mview as a sequence of numbers.
<file>.write(<mview>) # Writes `bytes(<mview>)` to the file.
<file>.write(<mview>) # Writes `bytes(<mview>)` to binary file.
```
```
```python
```python
@ -2156,8 +2156,8 @@ with <lock>: # Enters the block by calling acq
<bool> = <Future>.cancel() # Cancels or returns False if running/finished.
<bool> = <Future>.cancel() # Cancels or returns False if running/finished.
<iter> = as_completed(<coll_of_Futures>) # `next(<iter>)` returns next completed Future.
<iter> = as_completed(<coll_of_Futures>) # `next(<iter>)` returns next completed Future.
```
```
* **Map() and as\_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As\_completed() fails if next() is called too late, even if thread finished on time.**
* **Map() and as\_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As\_completed() fails if next() is called too late, even if all threads have finished.**
* **Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.**
* **Exceptions that happen inside threads are raised when map iterator's next() or Future's result() are called. Future's exception() method returns exception object or None.**
* **ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be [pickable](#pickle), queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via `'if __name__ == "__main__": ...'`.**
* **ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be [pickable](#pickle), queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via `'if __name__ == "__main__": ...'`.**
@ -2680,7 +2680,7 @@ import numpy as np
```python
```python
<array> = np.concatenate(<list_of_arrays>, axis=0) # Links arrays along first axis (rows).
<array> = np.concatenate(<list_of_arrays>, axis=0) # Links arrays along first axis (rows).
<array> = np.row_stack/column_stack(<list_of_arrays>) # Treats 1d arrays as rows or columns.
<array> = np.vstack/column_stack(<list_of_arrays>) # Treats 1d arrays as rows or columns.
<array> = np.tile/repeat(<array>, <int/list> [, axis]) # Tiles array or repeats its elements.
<array> = np.tile/repeat(<array>, <int/list> [, axis]) # Tiles array or repeats its elements.
```
```
* **Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).**
* **Shape is a tuple of dimension sizes. A 100x50 RGB image has shape (50, 100, 3).**
@ -2772,7 +2772,7 @@ from PIL import Image
<Image> = Image.new('<mode>', (width, height)) # Creates new image. Also `color=<int/tuple>`.
<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.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.
<Image>.save(<path>) # Selects format based on extension (png/jpg…).
<Image>.save(<path>) # Selects format based on extension (PNG/JPG…).
<Image>.show() # Opens image in the default preview app.
<Image>.show() # Opens image in the default preview app.
```
```
@ -2795,10 +2795,11 @@ from PIL import Image
```
```
### Modes
### Modes
* **`'L'` - Lightness (i.e. greyscale). Each pixel is an int between 0 and 255.**
* **`'L'` - Lightness (greyscale image). Each pixel is an int between 0 and 255.**
* **`'RGB'` - Red, green, blue (i.e. true color). Each pixel is a tuple of three ints.**
* **`'RGB'` - Red, green, blue (true color image). Each pixel is a tuple of three ints.**
* **`'RGBA'` - RGB with alpha. Low alpha (forth int) means more transparency.**
* **`'RGBA'` - RGB with alpha. Low alpha (i.e. forth int) makes pixel more transparent.**
* **`'HSV'` - Hue, saturation, value color space.**
* **`'HSV'` - Hue, saturation, value. Three ints representing color in HSV color space.**
### Examples
### Examples
#### Creates a PNG image of a rainbow gradient:
#### Creates a PNG image of a rainbow gradient:
@ -2823,14 +2824,14 @@ img.show()
### Image Draw
### Image Draw
```python
```python
from PIL import ImageDraw
from PIL import ImageDraw
<ImageDraw> = ImageDraw.Draw(<Image>) # Object for adding 2D graphics to the image.
<Draw> = ImageDraw.Draw(<Image>) # Object for adding 2D graphics to the image.
<ImageDraw>.point((x, y)) # Draws a point. Truncates floats into ints.
<Draw>.point((x, y)) # Draws a point. Truncates floats into ints.
<ImageDraw>.line((x1, y1, x2, y2 [, ...])) # To get anti-aliasing use Image's resize().
<Draw>.line((x1, y1, x2, y2 [, ...])) # To get anti-aliasing use Image's resize().
<ImageDraw>.arc((x1, y1, x2, y2), deg1, deg2) # Always draws in clockwise direction.
<Draw>.arc((x1, y1, x2, y2), deg1, deg2) # Always draws in clockwise direction.
<ImageDraw>.rectangle((x1, y1, x2, y2)) # To rotate use Image's rotate() and paste().
<Draw>.rectangle((x1, y1, x2, y2)) # To rotate use Image's rotate() and paste().
<ImageDraw>.polygon((x1, y1, x2, y2, ...)) # Last point gets connected to the first.
<Draw>.polygon((x1, y1, x2, y2, ...)) # Last point gets connected to the first.
<ImageDraw>.ellipse((x1, y1, x2, y2)) # To rotate use Image's rotate() and paste().
<Draw>.ellipse((x1, y1, x2, y2)) # To rotate use Image's rotate() and paste().
<div><h3id="sqlalchemy">SqlAlchemy</h3><p><strong>Library for interacting with various DB systems via SQL, method chaining, or ORM.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install sqlalchemy</span>
<div><h3id="sqlalchemy">SQLAlchemy</h3><p><strong>Library for interacting with various DB systems via SQL, method chaining, or ORM.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install sqlalchemy</span>
<spanclass="hljs-keyword">from</span> sqlalchemy <spanclass="hljs-keyword">import</span> create_engine, text
<spanclass="hljs-keyword">from</span> sqlalchemy <spanclass="hljs-keyword">import</span> create_engine, text
<div><h2id="array"><ahref="#array"name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be swapped with byteswap() method.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> array <spanclass="hljs-keyword">import</span> array
<div><h2id="array"><ahref="#array"name="array">#</a>Array</h2><p><strong>List that can only hold numbers of a predefined type. Available types and their minimum sizes in bytes are listed above. Type sizes and byte order are always determined by the system, however bytes of each element can be reversed with byteswap() method.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">from</span> array <spanclass="hljs-keyword">import</span> array
</code></pre></div>
</code></pre></div>
<pre><codeclass="python language-python hljs"><array> = array(<spanclass="hljs-string">'<typecode>'</span>, <coll_of_nums>) <spanclass="hljs-comment"># Array from collection of numbers.</span>
<pre><codeclass="python language-python hljs"><array> = array(<spanclass="hljs-string">'<typecode>'</span>, <coll_of_nums>) <spanclass="hljs-comment"># Array from collection of numbers.</span>
<array> = array(<spanclass="hljs-string">'<typecode>'</span>, <bytes>) <spanclass="hljs-comment"># Array from bytes object.</span>
<array> = array(<spanclass="hljs-string">'<typecode>'</span>, <bytes>) <spanclass="hljs-comment"># Copies bytes to array's memory.</span>
<array> = array(<spanclass="hljs-string">'<typecode>'</span>, <array>) <spanclass="hljs-comment"># Treats array as a sequence of numbers.</span>
<array> = array(<spanclass="hljs-string">'<typecode>'</span>, <array>) <spanclass="hljs-comment"># Treats array as a sequence of numbers.</span>
<array>.fromfile(<file>, n_items) <spanclass="hljs-comment"># Appends items from the binary file.</span>
<array>.fromfile(<file>, n_items) <spanclass="hljs-comment"># Appends items from binary file.</span>
</code></pre>
</code></pre>
<pre><codeclass="python language-python hljs"><bytes> = bytes(<array>) <spanclass="hljs-comment"># Returns a copy of array's memory.</span>
<pre><codeclass="python language-python hljs"><bytes> = bytes(<array>) <spanclass="hljs-comment"># Returns a copy of array's memory.</span>
<file>.write(<array>) <spanclass="hljs-comment"># Writes array's memory to the file.</span>
<file>.write(<array>) <spanclass="hljs-comment"># Writes array's memory to binary file.</span>
</code></pre>
</code></pre>
<div><h2id="memoryview"><ahref="#memoryview"name="memoryview">#</a>Memory View</h2><p><strong>A sequence object that points to the memory of another bytes-like object. Each element can reference a single or multiple consecutive bytes, depending on format. Order and number of elements can be changed with slicing.</strong></p><pre><codeclass="python language-python hljs"><mview> = memoryview(<bytes/bytearray/array>) <spanclass="hljs-comment"># Immutable if bytes, else mutable.</span>
<div><h2id="memoryview"><ahref="#memoryview"name="memoryview">#</a>Memory View</h2><p><strong>A sequence object that points to the memory of another bytes-like object. Each element can reference a single or multiple consecutive bytes, depending on format. Order and number of elements can be changed with slicing.</strong></p><pre><codeclass="python language-python hljs"><mview> = memoryview(<bytes/bytearray/array>) <spanclass="hljs-comment"># Immutable if bytes, else mutable.</span>
<obj> = <mview>[index] <spanclass="hljs-comment"># Returns int/float (bytes if format is 'c').</span>
<mview> = <mview>[<slice>] <spanclass="hljs-comment"># Returns mview with rearranged elements.</span>
<mview> = <mview>[<slice>] <spanclass="hljs-comment"># Returns mview with rearranged elements.</span>
<mview> = <mview>.cast(<spanclass="hljs-string">'<typecode>'</span>) <spanclass="hljs-comment"># Only works between B/b/c and other types.</span>
<mview> = <mview>.cast(<spanclass="hljs-string">'<typecode>'</span>) <spanclass="hljs-comment"># Only works between B/b/c and other types.</span>
<mview>.release() <spanclass="hljs-comment"># Releases memory buffer of the base object.</span>
<mview>.release() <spanclass="hljs-comment"># Releases memory buffer of the base object.</span>
<pre><codeclass="python language-python hljs"><bytes> = bytes(<mview>) <spanclass="hljs-comment"># Returns a new bytes object.</span>
<pre><codeclass="python language-python hljs"><bytes> = bytes(<mview>) <spanclass="hljs-comment"># Returns a new bytes object.</span>
<bytes> = <bytes>.join(<coll_of_mviews>) <spanclass="hljs-comment"># Joins mviews using bytes as a separator.</span>
<bytes> = <bytes>.join(<coll_of_mviews>) <spanclass="hljs-comment"># Joins mviews using bytes as a separator.</span>
<array> = array(<spanclass="hljs-string">'<typecode>'</span>, <mview>) <spanclass="hljs-comment"># Treats mview as a sequence of numbers.</span>
<array> = array(<spanclass="hljs-string">'<typecode>'</span>, <mview>) <spanclass="hljs-comment"># Treats mview as a sequence of numbers.</span>
<file>.write(<mview>) <spanclass="hljs-comment"># Writes `bytes(<mview>)` to the file.</span>
<file>.write(<mview>) <spanclass="hljs-comment"># Writes `bytes(<mview>)` to binary file.</span>
</code></pre>
</code></pre>
<pre><codeclass="python language-python hljs"><list> = list(<mview>) <spanclass="hljs-comment"># Returns a list of ints, floats or bytes.</span>
<pre><codeclass="python language-python hljs"><list> = list(<mview>) <spanclass="hljs-comment"># Returns a list of ints, floats or bytes.</span>
<str> = str(<mview>, <spanclass="hljs-string">'utf-8'</span>) <spanclass="hljs-comment"># Treats mview as a bytes object.</span>
<str> = str(<mview>, <spanclass="hljs-string">'utf-8'</span>) <spanclass="hljs-comment"># Treats mview as a bytes object.</span>
<iter> = as_completed(<coll_of_Futures>) <spanclass="hljs-comment"># `next(<iter>)` returns next completed Future.</span>
<iter> = as_completed(<coll_of_Futures>) <spanclass="hljs-comment"># `next(<iter>)` returns next completed Future.</span>
</code></pre>
</code></pre>
<ul>
<ul>
<li><strong>Map() and as_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As_completed() fails if next() is called too late, even if thread finished on time.</strong></li>
<li><strong>Map() and as_completed() also accept 'timeout'. It causes futures.TimeoutError when next() is called/blocking. Map() times from original call and as_completed() from first call to next(). As_completed() fails if next() is called too late, even if all threads have finished.</strong></li>
<li><strong>Exceptions that happen inside threads are raised when next() is called on map's iterator or when result() is called on a Future. Its exception() method returns exception or None.</strong></li>
<li><strong>Exceptions that happen inside threads are raised when map iterator's next() or Future's result() are called. Future's exception() method returns exception object or None.</strong></li>
<li><strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be <ahref="#pickle">pickable</a>, queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via <codeclass="python hljs"><spanclass="hljs-string">'if __name__ == "__main__": ...'</span></code>.</strong></li>
<li><strong>ProcessPoolExecutor provides true parallelism but: everything sent to/from workers must be <ahref="#pickle">pickable</a>, queues must be sent using executor's 'initargs' and 'initializer' parameters, and executor should only be reachable via <codeclass="python hljs"><spanclass="hljs-string">'if __name__ == "__main__": ...'</span></code>.</strong></li>
</ul>
</ul>
<div><h2id="operator"><ahref="#operator"name="operator">#</a>Operator</h2><p><strong>Module of functions that provide the functionality of operators. Functions are ordered by operator precedence, starting with least binding.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> operator <spanclass="hljs-keyword">as</span> op
<div><h2id="operator"><ahref="#operator"name="operator">#</a>Operator</h2><p><strong>Module of functions that provide the functionality of operators. Functions are ordered by operator precedence, starting with least binding.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> operator <spanclass="hljs-keyword">as</span> op
<array> = np.apply_along_axis(<func>, axis, <array>) <spanclass="hljs-comment"># Func can return a scalar or array.</span>
<array> = np.apply_along_axis(<func>, axis, <array>) <spanclass="hljs-comment"># Func can return a scalar or array.</span>
</code></pre>
</code></pre>
<pre><codeclass="python language-python hljs"><array> = np.concatenate(<list_of_arrays>, axis=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Links arrays along first axis (rows).</span>
<pre><codeclass="python language-python hljs"><array> = np.concatenate(<list_of_arrays>, axis=<spanclass="hljs-number">0</span>) <spanclass="hljs-comment"># Links arrays along first axis (rows).</span>
<array> = np.row_stack/column_stack(<list_of_arrays>) <spanclass="hljs-comment"># Treats 1d arrays as rows or columns.</span>
<array> = np.vstack/column_stack(<list_of_arrays>)<spanclass="hljs-comment"># Treats 1d arrays as rows or columns.</span>
<array> = np.tile/repeat(<array>, <int/list> [, axis]) <spanclass="hljs-comment"># Tiles array or repeats its elements.</span>
<array> = np.tile/repeat(<array>, <int/list> [, axis]) <spanclass="hljs-comment"># Tiles array or repeats its elements.</span>
</code></pre>
</code></pre>
<ul>
<ul>
@ -2265,7 +2265,7 @@ 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>
<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.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.</span>
<Image>.save(<path>) <spanclass="hljs-comment"># Selects format based on extension (png/jpg…).</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>
<Image>.show() <spanclass="hljs-comment"># Opens image in the default preview app.</span>
@ -2281,10 +2281,10 @@ right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</
<Image> = Image.fromarray(np.uint8(<array>)) <spanclass="hljs-comment"># Use `<array>.clip(0, 255)` to clip values.</span>
<Image> = Image.fromarray(np.uint8(<array>)) <spanclass="hljs-comment"># Use `<array>.clip(0, 255)` to clip values.</span>
</code></pre>
</code></pre>
<div><h3id="modes-1">Modes</h3><ul>
<div><h3id="modes-1">Modes</h3><ul>
<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">'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 (i.e. true color). Each pixel is a tuple of three ints.</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 (forth int) means more transparency.</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">'HSV'</span></code> - Hue, saturation, value color space.</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>
</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
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))
hues = (<spanclass="hljs-number">255</span> * i/n_pixels <spanclass="hljs-keyword">for</span> i <spanclass="hljs-keyword">in</span> range(n_pixels))
xxxxxxxxxx