|
2022 | 2022 | <li><strong><code class="python hljs"><span class="hljs-string">'<time>'</span></code> - Time of day.</strong></li>
|
2023 | 2023 | <li><strong><code class="python hljs"><span class="hljs-string">'<str>'</span></code> - Any of above as a string: <code class="python hljs"><span class="hljs-string">'100 MB'</span></code>, <code class="python hljs"><span class="hljs-string">'1 month'</span></code>, <code class="python hljs"><span class="hljs-string">'monday at 12:00'</span></code>, …</strong></li>
|
2024 | 2024 | </ul>
|
2025 |
| -<div><h3 id="retention">Retention</h3><p><strong>Sets a condition which old log files are deleted.</strong></p><pre><code class="python language-python hljs">retention=<int>|<datetime.timedelta>|<str> |
| 2025 | +<div><h3 id="retention">Retention</h3><p><strong>Sets a condition which old log files get deleted.</strong></p><pre><code class="python language-python hljs">retention=<int>|<datetime.timedelta>|<str> |
2026 | 2026 | </code></pre></div>
|
2027 | 2027 |
|
2028 | 2028 |
|
|
2228 | 2228 | <pre><code class="python language-python hljs"><tuple/int> = <Image>.getpixel((x, y)) <span class="hljs-comment"># Returns a pixel.</span>
|
2229 | 2229 | <Image>.putpixel((x, y), <tuple/int>) <span class="hljs-comment"># Writes a pixel to image.</span>
|
2230 | 2230 | <ImagingCore> = <Image>.getdata() <span class="hljs-comment"># Returns a sequence of pixels.</span>
|
2231 |
| -<Image>.putdata(<list/tuple>) <span class="hljs-comment"># Writes a sequence of pixels.</span> |
| 2231 | +<Image>.putdata(<list/tuple/ImagingCore>) <span class="hljs-comment"># Writes a sequence of pixels.</span> |
2232 | 2232 | <Image>.paste(<Image>, (x, y)) <span class="hljs-comment"># Writes an image to image.</span>
|
2233 | 2233 | </code></pre>
|
2234 | 2234 | <div><h3 id="modes-1">Modes</h3><ul>
|
|
2239 | 2239 | <li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - 3x8-bit pixels, Hue, Saturation, Value color space.</strong></li>
|
2240 | 2240 | </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>
|
2241 | 2241 | size = WIDTH * HEIGHT
|
2242 |
| -hue = [<span class="hljs-number">255</span> * i/size <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(size)] |
| 2242 | +hues = [<span class="hljs-number">255</span> * i/size <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(size)] |
2243 | 2243 | img = Image.new(<span class="hljs-string">'HSV'</span>, (WIDTH, HEIGHT))
|
2244 |
| -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]) |
| 2244 | +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> hues]) |
2245 | 2245 | img.convert(<span class="hljs-string">'RGB'</span>).save(<span class="hljs-string">'test.png'</span>)
|
2246 | 2246 | </code></pre></div></div></div>
|
2247 | 2247 |
|
|
2320 | 2320 | +-----------+-------------+------+-------------+
|
2321 | 2321 | </code></pre></div>
|
2322 | 2322 |
|
2323 |
| -<div><h3 id="readfloatframesfromwavfile">Read Float Frames from WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_wav_file</span><span class="hljs-params">(filename)</span>:</span> |
| 2323 | +<div><h3 id="readfloatsamplesfromwavfile">Read Float Samples from WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_wav_file</span><span class="hljs-params">(filename)</span>:</span> |
2324 | 2324 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_int</span><span class="hljs-params">(a_bytes)</span>:</span>
|
2325 | 2325 | an_int = int.from_bytes(a_bytes, <span class="hljs-string">'little'</span>, signed=width!=<span class="hljs-number">1</span>)
|
2326 | 2326 | <span class="hljs-keyword">return</span> an_int - <span class="hljs-number">128</span> * (width == <span class="hljs-number">1</span>)
|
2327 | 2327 | <span class="hljs-keyword">with</span> wave.open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
|
2328 | 2328 | frames = file.readframes(file.getnframes())
|
2329 | 2329 | width = file.getsampwidth()
|
2330 |
| - chunks = (frames[i: i + width] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, len(frames), width)) |
2331 |
| - <span class="hljs-keyword">return</span> [get_int(a) / pow(<span class="hljs-number">2</span>, width * <span class="hljs-number">8</span> - <span class="hljs-number">1</span>) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> chunks] |
| 2330 | + samples = (frames[i: i + width] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, len(frames), width)) |
| 2331 | + <span class="hljs-keyword">return</span> [get_int(a) / pow(<span class="hljs-number">2</span>, width * <span class="hljs-number">8</span> - <span class="hljs-number">1</span>) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> samples] |
2332 | 2332 | </code></pre></div>
|
2333 | 2333 |
|
2334 |
| -<div><h3 id="writefloatframestowavfile">Write Float Frames to WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_wav_file</span><span class="hljs-params">(filename, frames_float, nchannels=<span class="hljs-number">1</span>, sampwidth=<span class="hljs-number">2</span>, framerate=<span class="hljs-number">44100</span>)</span>:</span> |
| 2334 | +<div><h3 id="writefloatsamplestowavfile">Write Float Samples to WAV File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_wav_file</span><span class="hljs-params">(filename, float_samples, nchannels=<span class="hljs-number">1</span>, sampwidth=<span class="hljs-number">2</span>, framerate=<span class="hljs-number">44100</span>)</span>:</span> |
2335 | 2335 | <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_bytes</span><span class="hljs-params">(a_float)</span>:</span>
|
2336 | 2336 | a_float = max(<span class="hljs-number">-1</span>, min(<span class="hljs-number">1</span> - <span class="hljs-number">2e-16</span>, a_float))
|
2337 | 2337 | a_float += sampwidth == <span class="hljs-number">1</span>
|
|
2341 | 2341 | file.setnchannels(nchannels)
|
2342 | 2342 | file.setsampwidth(sampwidth)
|
2343 | 2343 | file.setframerate(framerate)
|
2344 |
| - file.writeframes(<span class="hljs-string">b''</span>.join(get_bytes(a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> frames_float)) |
| 2344 | + file.writeframes(<span class="hljs-string">b''</span>.join(get_bytes(a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> float_samples)) |
2345 | 2345 | </code></pre></div>
|
2346 | 2346 |
|
2347 | 2347 | <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
|
|
2351 | 2351 |
|
2352 | 2352 |
|
2353 | 2353 | <div><h4 id="addsnoisetoamonowavfile">Adds noise to a mono WAV file:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> random
|
2354 |
| -add_noise = <span class="hljs-keyword">lambda</span> value: value + (random()<span class="hljs-number">-0.5</span>) * <span class="hljs-number">0.03</span> |
| 2354 | +add_noise = <span class="hljs-keyword">lambda</span> value: value + (random() - <span class="hljs-number">0.5</span>) * <span class="hljs-number">0.03</span> |
2355 | 2355 | frames_f = (add_noise(a) <span class="hljs-keyword">for</span> a <span class="hljs-keyword">in</span> read_wav_file(<span class="hljs-string">'test.wav'</span>))
|
2356 | 2356 | write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_f)
|
2357 | 2357 | </code></pre></div>
|
|
0 commit comments