Skip to content

Commit eae093b

Browse files
committed
Libraries
1 parent 71edb44 commit eae093b

File tree

1 file changed

+101
-101
lines changed

1 file changed

+101
-101
lines changed

README.md

Lines changed: 101 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,107 +1683,6 @@ def get_border(screen):
16831683
```
16841684

16851685

1686-
Image
1687-
-----
1688-
```python
1689-
# $ pip3 install pillow
1690-
from PIL import Image
1691-
```
1692-
1693-
#### Creates PNG image of rainbow gradient:
1694-
```python
1695-
width = 100
1696-
height = 100
1697-
size = width * height
1698-
pixels = [255 * i/size for i in range(size)]
1699-
1700-
img = Image.new('HSV', (width, height))
1701-
img.putdata([(int(a), 255, 255) for a in pixels])
1702-
img.convert(mode='RGB').save('test.png')
1703-
```
1704-
1705-
#### Adds noise to PNG image:
1706-
```python
1707-
from random import randint
1708-
add_noise = lambda value: max(0, min(255, value + randint(-20, 20)))
1709-
img = Image.open('test.png').convert(mode='HSV')
1710-
img.putdata([(add_noise(h), s, v) for h, s, v in img.getdata()])
1711-
img.convert(mode='RGB').save('test.png')
1712-
```
1713-
1714-
### Modes
1715-
* **`'1'` - 1-bit pixels, black and white, stored with one pixel per byte.**
1716-
* **`'L'` - 8-bit pixels, greyscale.**
1717-
* **`'RGB'` - 3x8-bit pixels, true color.**
1718-
* **`'RGBA'` - 4x8-bit pixels, true color with transparency mask.**
1719-
* **`'HSV'` - 3x8-bit pixels, Hue, Saturation, Value color space.**
1720-
1721-
1722-
Audio
1723-
-----
1724-
```python
1725-
import wave
1726-
from struct import pack, iter_unpack
1727-
```
1728-
1729-
### Read Frames from WAV File
1730-
```python
1731-
def read_wav_file(filename):
1732-
with wave.open(filename, 'rb') as wf:
1733-
frames = wf.readframes(wf.getnframes())
1734-
return [a[0] for a in iter_unpack('<h', frames)]
1735-
```
1736-
1737-
### Write Frames to WAV File
1738-
```python
1739-
def write_to_wav_file(filename, frames_int):
1740-
frames_short = (pack('<h', a) for a in frames_int)
1741-
with wave.open(filename, 'wb') as wf:
1742-
wf.setnchannels(1)
1743-
wf.setsampwidth(2)
1744-
wf.setframerate(44100)
1745-
wf.writeframes(b''.join(frames_short))
1746-
```
1747-
1748-
### Examples
1749-
#### Saves a sine wave to a WAV file:
1750-
```python
1751-
from math import pi, sin
1752-
sin_f = lambda i: sin(i * 2 * pi * 440 / 44100)
1753-
frames_f = (sin_f(a) for a in range(100000))
1754-
frames_i = (int(a * 30000) for a in frames_f)
1755-
write_to_wav_file('test.wav', frames_i)
1756-
```
1757-
1758-
#### Adds noise to a WAV file:
1759-
```python
1760-
from random import randint
1761-
add_noise = lambda value: max(-32768, min(32768, value + randint(-500, 500)))
1762-
frames_i = read_wav_file('test.wav')
1763-
frames_i = (add_noise(a) for a in frames_i)
1764-
write_to_wav_file('test.wav', frames_i)
1765-
```
1766-
1767-
#### Plays Popcorn:
1768-
```python
1769-
# pip3 install simpleaudio
1770-
import simpleaudio, math, struct
1771-
from itertools import chain, repeat
1772-
F = 44100
1773-
P1 = '71♪,69,,71♪,66,,62♪,66,,59♪,,,'
1774-
P2 = '71♪,73,,74♪,73,,74,,71,,73♪,71,,73,,69,,71♪,69,,71,,67,,71♪,,,'
1775-
get_pause = lambda seconds: repeat(0, int(seconds * F))
1776-
sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F)
1777-
get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
1778-
get_hz = lambda n: 8.176 * 2 ** (int(n) / 12)
1779-
parse_n = lambda note: (get_hz(note[:2]), 0.25 if len(note) > 2 else 0.125)
1780-
get_note = lambda note: get_wave(*parse_n(note)) if note else get_pause(0.125)
1781-
frames_i = chain.from_iterable(get_note(n) for n in f'{P1}{P1}{P2}'.split(','))
1782-
frames_b = b''.join(struct.pack('<h', int(a * 30000)) for a in frames_i)
1783-
simpleaudio.play_buffer(frames_b, 1, 2, F)
1784-
```
1785-
1786-
17871686
Scraping
17881687
--------
17891688
```python
@@ -2016,6 +1915,107 @@ right = [[0.1, 0.6, 0.8], [0.1, 0.6, 0.8], [0.1, 0.6, 0.8]] # Shape: (3, 3) <-
20161915
```
20171916

20181917

1918+
Image
1919+
-----
1920+
```python
1921+
# $ pip3 install pillow
1922+
from PIL import Image
1923+
```
1924+
1925+
#### Creates PNG image of rainbow gradient:
1926+
```python
1927+
width = 100
1928+
height = 100
1929+
size = width * height
1930+
pixels = [255 * i/size for i in range(size)]
1931+
1932+
img = Image.new('HSV', (width, height))
1933+
img.putdata([(int(a), 255, 255) for a in pixels])
1934+
img.convert(mode='RGB').save('test.png')
1935+
```
1936+
1937+
#### Adds noise to a PNG image:
1938+
```python
1939+
from random import randint
1940+
add_noise = lambda value: max(0, min(255, value + randint(-20, 20)))
1941+
img = Image.open('test.png').convert(mode='HSV')
1942+
img.putdata([(add_noise(h), s, v) for h, s, v in img.getdata()])
1943+
img.convert(mode='RGB').save('test.png')
1944+
```
1945+
1946+
### Modes
1947+
* **`'1'` - 1-bit pixels, black and white, stored with one pixel per byte.**
1948+
* **`'L'` - 8-bit pixels, greyscale.**
1949+
* **`'RGB'` - 3x8-bit pixels, true color.**
1950+
* **`'RGBA'` - 4x8-bit pixels, true color with transparency mask.**
1951+
* **`'HSV'` - 3x8-bit pixels, Hue, Saturation, Value color space.**
1952+
1953+
1954+
Audio
1955+
-----
1956+
```python
1957+
import wave
1958+
from struct import pack, iter_unpack
1959+
```
1960+
1961+
### Read Frames from WAV File
1962+
```python
1963+
def read_wav_file(filename):
1964+
with wave.open(filename, 'rb') as wf:
1965+
frames = wf.readframes(wf.getnframes())
1966+
return [a[0] for a in iter_unpack('<h', frames)]
1967+
```
1968+
1969+
### Write Frames to WAV File
1970+
```python
1971+
def write_to_wav_file(filename, frames_int):
1972+
frames_short = (pack('<h', a) for a in frames_int)
1973+
with wave.open(filename, 'wb') as wf:
1974+
wf.setnchannels(1)
1975+
wf.setsampwidth(2)
1976+
wf.setframerate(44100)
1977+
wf.writeframes(b''.join(frames_short))
1978+
```
1979+
1980+
### Examples
1981+
#### Saves a sine wave to a WAV file:
1982+
```python
1983+
from math import pi, sin
1984+
sin_f = lambda i: sin(i * 2 * pi * 440 / 44100)
1985+
frames_f = (sin_f(a) for a in range(100000))
1986+
frames_i = (int(a * 30000) for a in frames_f)
1987+
write_to_wav_file('test.wav', frames_i)
1988+
```
1989+
1990+
#### Adds noise to a WAV file:
1991+
```python
1992+
from random import randint
1993+
add_noise = lambda value: max(-32768, min(32768, value + randint(-500, 500)))
1994+
frames_i = read_wav_file('test.wav')
1995+
frames_i = (add_noise(a) for a in frames_i)
1996+
write_to_wav_file('test.wav', frames_i)
1997+
```
1998+
1999+
#### Plays Popcorn:
2000+
```python
2001+
# pip3 install simpleaudio
2002+
import simpleaudio, math, struct
2003+
from itertools import chain, repeat
2004+
F = 44100
2005+
P1 = '71♪,69,,71♪,66,,62♪,66,,59♪,,,'
2006+
P2 = '71♪,73,,74♪,73,,74,,71,,73♪,71,,73,,69,,71♪,69,,71,,67,,71♪,,,'
2007+
get_pause = lambda seconds: repeat(0, int(seconds * F))
2008+
sin_f = lambda i, hz: math.sin(i * 2 * math.pi * hz / F)
2009+
get_wave = lambda hz, seconds: (sin_f(i, hz) for i in range(int(seconds * F)))
2010+
get_hz = lambda n: 8.176 * 2 ** (int(n) / 12)
2011+
parse_n = lambda note: (get_hz(note[:2]), 0.25 if len(note) > 2 else 0.125)
2012+
get_note = lambda note: get_wave(*parse_n(note)) if note else get_pause(0.125)
2013+
frames_i = chain.from_iterable(get_note(n) for n in f'{P1}{P1}{P2}'.split(','))
2014+
frames_b = b''.join(struct.pack('<h', int(a * 30000)) for a in frames_i)
2015+
simpleaudio.play_buffer(frames_b, 1, 2, F)
2016+
```
2017+
2018+
20192019
Basic Script Template
20202020
---------------------
20212021
```python

0 commit comments

Comments
 (0)