Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7caf5e

Browse files
committed
Image
1 parent 413eac0 commit f7caf5e

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,14 +2562,15 @@ Image
25622562
from PIL import Image
25632563
```
25642564

2565+
### Examples
25652566
#### Creates a PNG image of a rainbow gradient:
25662567
```python
2567-
width = 100
2568-
height = 100
2569-
size = width * height
2570-
pixels = [255 * i/size for i in range(size)]
2568+
WIDTH = 100
2569+
HEIGHT = 100
2570+
SIZE = WIDTH * HEIGHT
2571+
pixels = [255 * i/SIZE for i in range(SIZE)]
25712572

2572-
img = Image.new('HSV', (width, height))
2573+
img = Image.new('HSV', (WIDTH, HEIGHT))
25732574
img.putdata([(int(a), 255, 255) for a in pixels])
25742575
img.convert(mode='RGB').save('test.png')
25752576
```
@@ -2591,18 +2592,17 @@ img.convert(mode='RGB').save('test.png')
25912592
* **`'HSV'` - 3x8-bit pixels, Hue, Saturation, Value color space.**
25922593

25932594
### Animation
2594-
25952595
#### Creates a GIF of a bouncing ball:
25962596
```python
25972597
from PIL import Image, ImageDraw
25982598
import imageio
2599-
height, r = 126, 10
2599+
HEIGHT, R = 126, 10
26002600
frames = []
2601-
for velocity in range(1, 16):
2602-
y = sum(range(velocity))
2603-
frame = Image.new('L', (height, height))
2601+
for velocity in range(15):
2602+
y = sum(range(velocity+1))
2603+
frame = Image.new('L', (HEIGHT, HEIGHT))
26042604
draw = ImageDraw.Draw(frame)
2605-
draw.ellipse((height/2-r, y, height/2+r, y+2*r), fill='white')
2605+
draw.ellipse((HEIGHT/2-R, y, HEIGHT/2+R, y+2*R), fill='white')
26062606
frames.append(frame)
26072607
frames += reversed(frames[1:-1])
26082608
imageio.mimsave('test.gif', frames, duration=0.03)

index.html

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,15 +2181,16 @@
21812181
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
21822182
</code></pre></div>
21832183

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)]
21882188

2189-
img = Image.new(<span class="hljs-string">'HSV'</span>, (width, height))
2189+
img = Image.new(<span class="hljs-string">'HSV'</span>, (WIDTH, HEIGHT))
21902190
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])
21912191
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+
21932194

21942195
<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
21952196
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,13 +2207,13 @@
22062207
<li><strong><code class="python hljs"><span class="hljs-string">'HSV'</span></code> - 3x8-bit pixels, Hue, Saturation, Value color space.</strong></li>
22072208
</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
22082209
<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>
22102211
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))
22142215
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>)
22162217
frames.append(frame)
22172218
frames += reversed(frames[<span class="hljs-number">1</span>:<span class="hljs-number">-1</span>])
22182219
imageio.mimsave(<span class="hljs-string">'test.gif'</span>, frames, duration=<span class="hljs-number">0.03</span>)
@@ -2240,7 +2241,7 @@
22402241
wf.writeframes(<span class="hljs-string">b''</span>.join(frames_short))
22412242
</code></pre></div>
22422243

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
22442245
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>))
22452246
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)
22462247
write_to_wav_file(<span class="hljs-string">'test.wav'</span>, frames_i)

0 commit comments

Comments
 (0)