|
217 | 217 | <strong><span class="hljs-string"><span class="hljs-string">'5. Data'</span></span></strong>: [<a href="#csv">CSV</a>, <a href="#json">JSON</a>, <a href="#pickle">Pickle</a>, <a href="#sqlite">SQLite</a>, <a href="#bytes">Bytes</a>, <a href="#struct">Struct</a>, <a href="#array">Array</a>, <a href="#memoryview">MemoryView</a>, <a href="#deque">Deque</a>],
|
218 | 218 | <strong><span class="hljs-string"><span class="hljs-string">'6. Advanced'</span></span></strong>: [<a href="#threading">Threading</a>, <a href="#operator">Operator</a>, <a href="#introspection">Introspection</a>, <a href="#metaprograming">Metaprograming</a>, <a href="#eval">Eval</a>, <a href="#coroutine">Coroutine</a>],
|
219 | 219 | <strong><span class="hljs-string"><span class="hljs-string">'7. Libraries'</span></span></strong>: [<a href="#progressbar">Progress_Bar</a>, <a href="#plot">Plot</a>, <a href="#table">Table</a>, <a href="#curses">Curses</a>, <a href="#logging">Logging</a>, <a href="#scraping">Scraping</a>, <a href="#web">Web</a>, <a href="#profile">Profile</a>,
|
220 |
| - <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#audio">Audio</a>] |
| 220 | + <a href="#numpy">NumPy</a>, <a href="#image">Image</a>, <a href="#animation">Animation</a>, <a href="#audio">Audio</a>, <a href="#synthesizer">Synthesizer</a>] |
221 | 221 | }
|
222 | 222 | </code></pre></div></div>
|
223 | 223 |
|
|
2189 | 2189 | <Image>.save(<span class="hljs-string">'<path>'</span>)
|
2190 | 2190 | <Image>.show()
|
2191 | 2191 | </code></pre>
|
2192 |
| -<pre><code class="python language-python hljs"><tuple/int> = img.getpixel((x, y)) <span class="hljs-comment"># Returns a pixel.</span> |
2193 |
| -<Image>.putpixel((x, y), <tuple/int>) <span class="hljs-comment"># Writes tuple/int to image.</span> |
2194 |
| -<ImagingCore> = <Image>.getdata() <span class="hljs-comment"># Returns a sequence of tuples/ints.</span> |
2195 |
| -<Image>.putdata(<list/tuple>) <span class="hljs-comment"># Writes a sequence of tuples/ints.</span> |
2196 |
| -<Image>.paste(<Image>, (x, y)) <span class="hljs-comment"># Writes an image to image.</span> |
| 2192 | +<pre><code class="python language-python hljs"><tuple/int> = img.getpixel((x, y)) <span class="hljs-comment"># Returns a pixel.</span> |
| 2193 | +<Image>.putpixel((x, y), <tuple/int>) <span class="hljs-comment"># Writes a pixel to image.</span> |
| 2194 | +<ImagingCore> = <Image>.getdata() <span class="hljs-comment"># Returns a sequence of pixels.</span> |
| 2195 | +<Image>.putdata(<list/tuple>) <span class="hljs-comment"># Writes a sequence of pixels.</span> |
| 2196 | +<Image>.paste(<Image>, (x, y)) <span class="hljs-comment"># Writes an image to image.</span> |
2197 | 2197 | </code></pre>
|
2198 | 2198 | <div><h3 id="modes-1">Modes</h3><ul>
|
2199 | 2199 | <li><strong><code class="python hljs"><span class="hljs-string">'1'</span></code> - 1-bit pixels, black and white, stored with one pixel per byte.</strong></li>
|
2200 | 2200 | <li><strong><code class="python hljs"><span class="hljs-string">'L'</span></code> - 8-bit pixels, greyscale.</strong></li>
|
2201 | 2201 | <li><strong><code class="python hljs"><span class="hljs-string">'RGB'</span></code> - 3x8-bit pixels, true color.</strong></li>
|
2202 | 2202 | <li><strong><code class="python hljs"><span class="hljs-string">'RGBA'</span></code> - 4x8-bit pixels, true color with transparency mask.</strong></li>
|
2203 | 2203 | <li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - 3x8-bit pixels, Hue, Saturation, Value color space.</strong></li>
|
2204 |
| -</ul><div><h3 id="imagedraw">ImageDraw</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> ImageDraw |
2205 |
| -<ImageDraw> = ImageDraw.Draw(<Image>) |
2206 |
| -<ImageDraw>.point((x, y), fill=<span class="hljs-keyword">None</span>) |
2207 |
| -<ImageDraw>.line((x1, y1, x2, y2 [, ...]), fill=<span class="hljs-keyword">None</span>, width=<span class="hljs-number">0</span>, joint=<span class="hljs-keyword">None</span>) |
2208 |
| -<ImageDraw>.arc((x1, y1, x2, y2), from_deg, to_deg, fill=<span class="hljs-keyword">None</span>, width=<span class="hljs-number">0</span>) |
2209 |
| -<ImageDraw>.rectangle((x1, y1, x2, y2), fill=<span class="hljs-keyword">None</span>, outline=<span class="hljs-keyword">None</span>, width=<span class="hljs-number">0</span>) |
2210 |
| -<ImageDraw>.polygon((x1, y1, x2, y2 [, ...]), fill=<span class="hljs-keyword">None</span>, outline=<span class="hljs-keyword">None</span>) |
2211 |
| -<ImageDraw>.ellipse((x1, y1, x2, y2), fill=<span class="hljs-keyword">None</span>, outline=<span class="hljs-keyword">None</span>, width=<span class="hljs-number">0</span>) |
2212 |
| -</code></pre></div></div> |
2213 |
| - |
2214 |
| - |
2215 |
| - |
2216 |
| -<ul> |
2217 |
| -<li><strong>Use <code class="python hljs"><span class="hljs-string">'fill=<color>'</span></code> to set the primary color.</strong></li> |
2218 |
| -<li><strong>Use <code class="python hljs"><span class="hljs-string">'outline=<color>'</span></code> to set the secondary color.</strong></li> |
2219 |
| -<li><strong>Colors can be specified as tuple, int, <code class="python hljs"><span class="hljs-string">'#rrggbb'</span></code> string or a color name.</strong></li> |
2220 |
| -</ul> |
2221 |
| -<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> |
| 2204 | +</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> |
2222 | 2205 | size = WIDTH * HEIGHT
|
2223 | 2206 | hue = [<span class="hljs-number">255</span> * i/size <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(size)]
|
2224 | 2207 | img = Image.new(<span class="hljs-string">'HSV'</span>, (WIDTH, HEIGHT))
|
2225 | 2208 | img.putdata([(int(h), <span class="hljs-number">255</span>, <span class="hljs-number">255</span>) <span class="hljs-keyword">for</span> h <span class="hljs-keyword">in</span> hue])
|
2226 | 2209 | img.convert(mode=<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
|
2227 |
| -</code></pre></div></div> |
| 2210 | +</code></pre></div></div></div> |
| 2211 | + |
| 2212 | + |
2228 | 2213 |
|
2229 | 2214 |
|
2230 | 2215 | <div><h4 id="addsnoisetoapngimage">Adds noise to a PNG image:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint
|
|
2234 | 2219 | img.convert(mode=<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
|
2235 | 2220 | </code></pre></div>
|
2236 | 2221 |
|
2237 |
| -<div><h2 id="animation"><a href="#animation" name="animation">#</a>Animation</h2><div><h4 id="createsagifofabouncingball">Creates a GIF of a bouncing ball:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install imageio</span> |
| 2222 | +<div><h3 id="imagedraw">ImageDraw</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> ImageDraw |
| 2223 | +<ImageDraw> = ImageDraw.Draw(<Image>) |
| 2224 | +<ImageDraw>.point((x, y), fill=<span class="hljs-keyword">None</span>) |
| 2225 | +<ImageDraw>.line((x1, y1, x2, y2 [, ...]), fill=<span class="hljs-keyword">None</span>, width=<span class="hljs-number">0</span>, joint=<span class="hljs-keyword">None</span>) |
| 2226 | +<ImageDraw>.arc((x1, y1, x2, y2), from_deg, to_deg, fill=<span class="hljs-keyword">None</span>, width=<span class="hljs-number">0</span>) |
| 2227 | +<ImageDraw>.rectangle((x1, y1, x2, y2), fill=<span class="hljs-keyword">None</span>, outline=<span class="hljs-keyword">None</span>, width=<span class="hljs-number">0</span>) |
| 2228 | +<ImageDraw>.polygon((x1, y1, x2, y2 [, ...]), fill=<span class="hljs-keyword">None</span>, outline=<span class="hljs-keyword">None</span>) |
| 2229 | +<ImageDraw>.ellipse((x1, y1, x2, y2), fill=<span class="hljs-keyword">None</span>, outline=<span class="hljs-keyword">None</span>, width=<span class="hljs-number">0</span>) |
| 2230 | +</code></pre></div> |
| 2231 | + |
| 2232 | +<ul> |
| 2233 | +<li><strong>Use <code class="python hljs"><span class="hljs-string">'fill=<color>'</span></code> to set the primary color.</strong></li> |
| 2234 | +<li><strong>Use <code class="python hljs"><span class="hljs-string">'outline=<color>'</span></code> to set the secondary color.</strong></li> |
| 2235 | +<li><strong>Colors can be specified as tuple, int, <code class="python hljs"><span class="hljs-string">'#rrggbb'</span></code> string or a color name.</strong></li> |
| 2236 | +</ul> |
| 2237 | +<div><h2 id="animation"><a href="#animation" name="animation">#</a>Animation</h2><div><h4 id="createsagifofabouncingball">Creates a GIF of a bouncing ball:</h4><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install pillow imageio</span> |
2238 | 2238 | <span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageDraw
|
2239 | 2239 | <span class="hljs-keyword">import</span> imageio
|
2240 | 2240 | WIDTH, R = <span class="hljs-number">126</span>, <span class="hljs-number">10</span>
|
|
0 commit comments