|
2181 | 2181 | <span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
|
2182 | 2182 | </code></pre></div>
|
2183 | 2183 |
|
2184 |
| -<div><h4 id="createsapngimageofarainbowgradient">Creates a PNG image of a rainbow gradient:</h4><pre><code class="python language-python hljs">width = <span class="hljs-number">100</span> |
2185 |
| -height = <span class="hljs-number">100</span> |
2186 |
| -size = width * height |
2187 |
| -pixels = [<span class="hljs-number">255</span> * i/size <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(size)] |
| 2184 | +<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 = <span class="hljs-number">100</span> |
| 2185 | +HEIGHT = <span class="hljs-number">100</span> |
| 2186 | +SIZE = WIDTH * HEIGHT |
| 2187 | +pixels = [<span class="hljs-number">255</span> * i/SIZE <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(SIZE)] |
2188 | 2188 |
|
2189 |
| -img = Image.new(<span class="hljs-string">'HSV'</span>, (width, height)) |
| 2189 | +img = Image.new(<span class="hljs-string">'HSV'</span>, (WIDTH, HEIGHT)) |
2190 | 2190 | img.putdata([(int(a), <span class="hljs-number">255</span>, <span class="hljs-number">255</span>) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> pixels])
|
2191 | 2191 | img.convert(mode=<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
|
2192 |
| -</code></pre></div> |
| 2192 | +</code></pre></div></div> |
| 2193 | + |
2193 | 2194 |
|
2194 | 2195 | <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
|
2195 | 2196 | add_noise = <span class="hljs-keyword">lambda</span> value: max(<span class="hljs-number">0</span>, min(<span class="hljs-number">255</span>, value + randint(<span class="hljs-number">-20</span>, <span class="hljs-number">20</span>)))
|
|
2206 | 2207 | <li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - 3x8-bit pixels, Hue, Saturation, Value color space.</strong></li>
|
2207 | 2208 | </ul><div><h3 id="animation">Animation</h3><div><h4 id="createsagifofabouncingball">Creates a GIF of a bouncing ball:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageDraw
|
2208 | 2209 | <span class="hljs-keyword">import</span> imageio
|
2209 |
| -height, r = <span class="hljs-number">126</span>, <span class="hljs-number">10</span> |
| 2210 | +HEIGHT, R = <span class="hljs-number">126</span>, <span class="hljs-number">10</span> |
2210 | 2211 | frames = []
|
2211 |
| -<span class="hljs-keyword">for</span> velocity <span class="hljs-keyword">in</span> range(<span class="hljs-number">1</span>, <span class="hljs-number">16</span>): |
2212 |
| - y = sum(range(velocity)) |
2213 |
| - frame = Image.new(<span class="hljs-string">'L'</span>, (height, height)) |
| 2212 | +<span class="hljs-keyword">for</span> velocity <span class="hljs-keyword">in</span> range(<span class="hljs-number">15</span>): |
| 2213 | + y = sum(range(velocity+<span class="hljs-number">1</span>)) |
| 2214 | + frame = Image.new(<span class="hljs-string">'L'</span>, (HEIGHT, HEIGHT)) |
2214 | 2215 | draw = ImageDraw.Draw(frame)
|
2215 |
| - draw.ellipse((height/<span class="hljs-number">2</span>-r, y, height/<span class="hljs-number">2</span>+r, y+<span class="hljs-number">2</span>*r), fill=<span class="hljs-string">'white'</span>) |
| 2216 | + draw.ellipse((HEIGHT/<span class="hljs-number">2</span>-R, y, HEIGHT/<span class="hljs-number">2</span>+R, y+<span class="hljs-number">2</span>*R), fill=<span class="hljs-string">'white'</span>) |
2216 | 2217 | frames.append(frame)
|
2217 | 2218 | frames += reversed(frames[<span class="hljs-number">1</span>:<span class="hljs-number">-1</span>])
|
2218 | 2219 | imageio.mimsave(<span class="hljs-string">'test.gif'</span>, frames, duration=<span class="hljs-number">0.03</span>)
|
|
2240 | 2241 | wf.writeframes(<span class="hljs-string">b''</span>.join(frames_short))
|
2241 | 2242 | </code></pre></div>
|
2242 | 2243 |
|
2243 |
| -<div><h3 id="examples">Examples</h3><div><h4 id="savesasinewavetoamonowavfile">Saves a sine wave to a mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> pi, sin |
| 2244 | +<div><h3 id="examples-1">Examples</h3><div><h4 id="savesasinewavetoamonowavfile">Saves a sine wave to a mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> math <span class="hljs-keyword">import</span> pi, sin |
2244 | 2245 | frames_f = (sin(i * <span class="hljs-number">2</span> * pi * <span class="hljs-number">440</span> / <span class="hljs-number">44100</span>) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">100000</span>))
|
2245 | 2246 | frames_i = (int(a * <span class="hljs-number">30000</span>) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> frames_f)
|
2246 | 2247 | write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_i)
|
|
0 commit comments