<div><h2id="exit"><ahref="#exit"name="exit">#</a>Exit</h2><p><strong>Exits the interpreter by raising SystemExit exception.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> sys
<div><h2id="exit"><ahref="#exit"name="exit">#</a>Exit</h2><p><strong>Exits the interpreter by raising SystemExit exception.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> sys
sys.exit() <spanclass="hljs-comment"># Exits with exit code 0 (success).</span>
sys.exit() <spanclass="hljs-comment"># Exits with exit code 0 (success).</span>
sys.exit(<el>) <spanclass="hljs-comment"># Prints to stderr and exits with 1.</span>
sys.exit(<el>) <spanclass="hljs-comment"># Prints to stderr and exits with 1.</span>
sys.exit(<int>) <spanclass="hljs-comment"># Exits with passed exit code.</span>
sys.exit(<int>) <spanclass="hljs-comment"># Exits with the passed exit code.</span>
<li><strong>Formatter also supports: pathname, filename, funcName, lineno, thread and process.</strong></li>
<li><strong>Formatter also supports: pathname, filename, funcName, lineno, thread and process.</strong></li>
<li><strong>A <codeclass="python hljs"><spanclass="hljs-string">'handlers.RotatingFileHandler'</span></code> creates and deletes log files based on 'maxBytes' and 'backupCount' arguments.</strong></li>
<li><strong>A <codeclass="python hljs"><spanclass="hljs-string">'handlers.RotatingFileHandler'</span></code> creates and deletes log files based on 'maxBytes' and 'backupCount' arguments.</strong></li>
</ul>
</ul>
<div><h4id="createsaloggerthatwritesallmessagestoafileandsendsthemtotherootloggerthatprintstostdout">Creates a logger that writes all messages to a file and sends them to the root logger that prints to stdout:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>logging.basicConfig(level=<spanclass="hljs-string">'WARNING'</span>)
<div><h4id="createsaloggerthatwritesallmessagestoafileandsendsthemtotherootloggerthatprintstostderr">Creates a logger that writes all messages to a file and sends them to the root logger that prints to stderr:</h4><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>logging.basicConfig(level=<spanclass="hljs-string">'WARNING'</span>)
<div><h2id="web"><ahref="#web"name="web">#</a>Web</h2><p><strong>Flask is a micro web framework/server. If you just want to open a html file in a web browser use <codeclass="python hljs"><spanclass="hljs-string">'webbrowser.open(<path>)'</span></code> instead.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install flask</span>
<div><h2id="web"><ahref="#web"name="web">#</a>Web</h2><p><strong>Flask is a micro web framework/server. If you just want to open a html file in a web browser use <codeclass="python hljs"><spanclass="hljs-string">'webbrowser.open(<path>)'</span></code> instead.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install flask</span>
<pre><codeclass="python language-python hljs"><Image> = Image.new(<spanclass="hljs-string">'<mode>'</span>, (width, height)) <spanclass="hljs-comment"># Also `color=<int/tuple/str>`.</span>
<Image> = Image.open(<path>) <spanclass="hljs-comment"># Identifies format based on file contents.</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 the path extension.</span>
<Image>.show() <spanclass="hljs-comment"># Opens image in the default preview app.</span>
<pre><codeclass="python language-python hljs"><Image> = Image.new(<spanclass="hljs-string">'<mode>'</span>, (width, height)) <spanclass="hljs-comment"># Also `color=<int/tuple/str>`.</span>
<Image> = Image.open(<path>) <spanclass="hljs-comment"># Identifies format based on file contents.</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 the path extension.</span>
<Image>.show() <spanclass="hljs-comment"># Opens image in the default preview app.</span>
</code></pre>
</code></pre>
<pre><codeclass="python language-python hljs"><int/tuple> = <Image>.getpixel((x, y)) <spanclass="hljs-comment"># Returns a pixel.</span>
<Image>.putpixel((x, y), <int/tuple>) <spanclass="hljs-comment"># Writes a pixel to the image.</span>
<ImagingCore> = <Image>.getdata() <spanclass="hljs-comment"># Returns a flattened view of the pixels.</span>
<Image>.putdata(<list/ImagingCore>) <spanclass="hljs-comment"># Writes a flattened sequence of pixels.</span>
<Image>.paste(<Image>, (x, y)) <spanclass="hljs-comment"># Writes passed image to the image.</span>
<pre><codeclass="python language-python hljs"><int/tuple> = <Image>.getpixel((x, y)) <spanclass="hljs-comment"># Returns a pixel.</span>
<Image>.putpixel((x, y), <int/tuple>) <spanclass="hljs-comment"># Writes a pixel to the image.</span>
<ImagingCore> = <Image>.getdata() <spanclass="hljs-comment"># Returns a flattened view of the pixels.</span>
<Image>.putdata(<list/ImagingCore>) <spanclass="hljs-comment"># Writes a flattened sequence of pixels.</span>
<Image>.paste(<Image>, (x, y)) <spanclass="hljs-comment"># Writes passed image to the image.</span>
<pre><codeclass="python language-python hljs"><array> = np.array(<Image>) <spanclass="hljs-comment"># Creates NumPy array from the image.</span>
<Image> = Image.fromarray(np.uint8(<array>)) <spanclass="hljs-comment"># Use <array>.clip(0, 255) to clip the values.</span>
<pre><codeclass="python language-python hljs"><array> = np.array(<Image>) <spanclass="hljs-comment"># Creates NumPy array from the image.</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">'1'</span></code> - 1-bit pixels, black and white, stored with one pixel per byte.</strong></li>
<div><h3id="imagedraw">Image Draw</h3><pre><codeclass="python language-python hljs"><ImageDraw> = ImageDraw.Draw(<Image>) <spanclass="hljs-comment"># Object for adding 2D graphics to the image.</span>
<ImageDraw>.point((x, y)) <spanclass="hljs-comment"># Draws a point. Truncates floats into ints.</span>
<ImageDraw>.line((x1, y1, x2, y2 [, ...])) <spanclass="hljs-comment"># To get anti-aliasing use Image's resize().</span>
<ImageDraw>.arc((x1, y1, x2, y2), deg1, deg2) <spanclass="hljs-comment"># Always draws in clockwise direction.</span>
<ImageDraw>.rectangle((x1, y1, x2, y2)) <spanclass="hljs-comment"># To rotate use Image's rotate() and paste().</span>
<ImageDraw>.polygon((x1, y1, x2, y2, ...)) <spanclass="hljs-comment"># Last point gets connected to the first.</span>
<ImageDraw>.ellipse((x1, y1, x2, y2)) <spanclass="hljs-comment"># To rotate use Image's rotate() and paste().</span>
$ <spanclass="hljs-built_in">source</span><name>/bin/activate <spanclass="hljs-comment"># Activates venv. On Windows run `<name>\Scripts\activate`.</span>
$ <spanclass="hljs-built_in">source</span><name>/bin/activate <spanclass="hljs-comment"># Activates venv. On Windows run `<name>\Scripts\activate`.</span>
$ pip3 install <library><spanclass="hljs-comment"># Installs the library into active environment.</span>
$ pip3 install <library><spanclass="hljs-comment"># Installs the library into active environment.</span>
$ python3 <path><spanclass="hljs-comment"># Runs the script in active environment. Also `./<path>`.</span>
$ python3 <path><spanclass="hljs-comment"># Runs the script in active environment. Also `./<path>`.</span>