From d890a8a38581b9ad09bdd9296cead8bebd819042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 21 Aug 2019 13:50:34 +0200 Subject: [PATCH] Image --- README.md | 45 ++++++++++++++++++++++++++++++++++++++------- index.html | 47 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 9ec9d27..572295a 100644 --- a/README.md +++ b/README.md @@ -2564,6 +2564,44 @@ Image from PIL import Image ``` +```python + = Image.new('', (width, height)) + = Image.open('') + = .convert(mode='') +.save('') +.show() +``` + +```python + = img.getpixel((x, y)) # Returns a pixel. +.putpixel((x, y), ) # Writes tuple/int to image. + = .getdata() # Returns a sequence of tuples/ints. +.putdata() # Writes a sequence of tuples/ints. +.paste(, (x, y)) # Writes an image to image. +``` + +### 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.** + +### ImageDraw +```python +from PIL import ImageDraw + = ImageDraw.Draw() +.point((x, y), fill=None) +.line((x1, y1, x2, y2 [, ...]), fill=None, width=0, joint=None) +.arc((x1, y1, x2, y2), from_deg, to_deg, fill=None, width=0) +.rectangle((x1, y1, x2, y2), fill=None, outline=None, width=0) +.polygon((x1, y1, x2, y2 [, ...]), fill=None, outline=None) +.ellipse((x1, y1, x2, y2), fill=None, outline=None, width=0) +``` +* **Use `'fill='` to set the primary color.** +* **Use `'outline='` to set the secondary color.** +* **Colors can be specified as tuple, int, `'#rrggbb'` string or a color name.** + ### Examples #### Creates a PNG image of a rainbow gradient: ```python @@ -2584,13 +2622,6 @@ img.putdata([(add_noise(h), s, v) for h, s, v in img.getdata()]) img.convert(mode='RGB').save('test.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.** - ### Animation #### Creates a GIF of a bouncing ball: ```python diff --git a/index.html b/index.html index 20e2966..aa91de9 100644 --- a/index.html +++ b/index.html @@ -2183,6 +2183,41 @@ right = [[0.1, 0.6from PIL import Image +
<Image> = Image.new('<mode>', (width, height))
+<Image> = Image.open('<path>')
+<Image> = <Image>.convert(mode='<mode>')
+<Image>.save('<path>')
+<Image>.show()
+
+
<tuple/int> = img.getpixel((x, y))        # Returns a pixel.
+<Image>.putpixel((x, y), <tuple/int>)     # Writes tuple/int to image.
+<ImagingCore> = <Image>.getdata()         # Returns a sequence of tuples/ints.
+<Image>.putdata(<list/tuple>)             # Writes a sequence of tuples/ints.
+<Image>.paste(<Image>, (x, y))            # Writes an image to image.
+
+

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.
  • +

ImageDraw

from PIL import ImageDraw
+<ImageDraw> = ImageDraw.Draw(<Image>)
+<ImageDraw>.point((x, y), fill=None)
+<ImageDraw>.line((x1, y1, x2, y2 [, ...]), fill=None, width=0, joint=None) 
+<ImageDraw>.arc((x1, y1, x2, y2), from_deg, to_deg, fill=None, width=0)
+<ImageDraw>.rectangle((x1, y1, x2, y2), fill=None, outline=None, width=0)
+<ImageDraw>.polygon((x1, y1, x2, y2 [, ...]), fill=None, outline=None)
+<ImageDraw>.ellipse((x1, y1, x2, y2), fill=None, outline=None, width=0)
+
+ + + +
    +
  • Use 'fill=<color>' to set the primary color.
  • +
  • Use 'outline=<color>' to set the secondary color.
  • +
  • Colors can be specified as tuple, int, '#rrggbb' string or a color name.
  • +

Examples

Creates a PNG image of a rainbow gradient:

WIDTH, HEIGHT = 100, 100
 size = WIDTH * HEIGHT
 hue = [255 * i/size for i in range(size)]
@@ -2199,13 +2234,7 @@ img.putdata([(add_noise(h), s, v) for h, s, v
 img.convert(mode='RGB').save('test.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.
  • -

Animation

Creates a GIF of a bouncing ball:

# $ pip3 install imageio
+

Animation

Creates a GIF of a bouncing ball:

# $ pip3 install imageio
 from PIL import Image, ImageDraw
 import imageio
 WIDTH, R = 126, 10
@@ -2218,9 +2247,7 @@ frames = []
     frames.append(frame)
 frames += reversed(frames[1:-1])
 imageio.mimsave('test.gif', frames, duration=0.03)
-
- - +

#Audio

import wave