From 93c0de29e87142a86eb9af42aecad5d185248609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 19 Dec 2018 10:22:57 +0100 Subject: [PATCH] Pillow --- README.md | 67 +++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 8269d61..ae33b85 100644 --- a/README.md +++ b/README.md @@ -1103,7 +1103,6 @@ def p_handler(sport): return json.dumps([home_odds, away_odds]) ``` - Curses ------ ```python @@ -1125,6 +1124,39 @@ def get_border(screen): return Coords(width - 1, height - 1) ``` +Image +----- +#### Creates png image of greyscale gradient: +```python +# $ pip3 install pillow +from PIL import Image +width, height = 100, 100 +img = Image.new('L', (width, height), 'white') +img.putdata([255*a/(width*height) for a in range(width*height)]) +img.save('out.png') +``` + +### Modes +* `1` - 1-bit pixels, black and white, stored with one pixel per byte +* `L` - 8-bit pixels, greyscale +* `RGB` - 3x8-bit pixels, true color +* `RGBA` - 4x8-bit pixels, true color with transparency mask +* `HSV` - 3x8-bit pixels, Hue, Saturation, Value color space + +Audio +----- +#### Saves list of floats with values between 0 and 1 to a WAV file: +```python +import wave, struct +frames = [struct.pack('h', int((a-0.5)*60000)) for a in ] +wf = wave.open(, 'wb') +wf.setnchannels(1) +wf.setsampwidth(4) +wf.setframerate(44100) +wf.writeframes(b''.join(frames)) +wf.close() +``` + Profile ------- #### Basic: @@ -1165,39 +1197,6 @@ def get_datetime_string(a_datetime): return a_datetime.strftime('%Y%m%d%H%M%S') ``` -Image ------ -#### Creates png image of greyscale gradient: -```python -# $ pip3 install pillow -from PIL import Image -width, height = 100, 100 -img = Image.new('L', (width, height), 'white') -img.putdata([255*a/(width*height) for a in range(width*height)]) -img.save('out.png') -``` - -### Modes -* `1` - 1-bit pixels, black and white, stored with one pixel per byte -* `L` - 8-bit pixels, greyscale -* `RGB` - 3x8-bit pixels, true color -* `RGBA` - 4x8-bit pixels, true color with transparency mask -* `HSV` - 3x8-bit pixels, Hue, Saturation, Value color space - -Audio ------ -#### Saves list of floats with values between 0 and 1 to a WAV file: -```python -import wave, struct -frames = [struct.pack('h', int((a-0.5)*60000)) for a in ] -wf = wave.open(, 'wb') -wf.setnchannels(1) -wf.setsampwidth(4) -wf.setframerate(44100) -wf.writeframes(b''.join(frames)) -wf.close() -``` - Progress Bar ------------ ### Basic: