Skip to content

Commit 3a8b905

Browse files
authored
Added ImageDraw alpha channel examples (#9201)
2 parents 7cb074f + 410fb60 commit 3a8b905

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/reference/ImageDraw.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,43 @@ Color names
5757

5858
See :ref:`color-names` for the color names supported by Pillow.
5959

60+
Alpha channel
61+
^^^^^^^^^^^^^
62+
63+
By default, when drawing onto an existing image, the image's pixel values are simply
64+
replaced by the new color::
65+
66+
im = Image.new("RGBA", (1, 1), (255, 0, 0))
67+
d = ImageDraw.Draw(im)
68+
d.rectangle((0, 0, 1, 1), (0, 255, 0, 127))
69+
assert im.getpixel((0, 0)) == (0, 255, 0, 127)
70+
71+
# Alpha channel values have no effect when drawing with RGB mode
72+
im = Image.new("RGB", (1, 1), (255, 0, 0))
73+
d = ImageDraw.Draw(im)
74+
d.rectangle((0, 0, 1, 1), (0, 255, 0, 127))
75+
assert im.getpixel((0, 0)) == (0, 255, 0)
76+
77+
If you would like to combine translucent color with an RGB image, then initialize the
78+
ImageDraw instance with the RGBA mode::
79+
80+
from PIL import Image, ImageDraw
81+
im = Image.new("RGB", (1, 1), (255, 0, 0))
82+
d = ImageDraw.Draw(im, "RGBA")
83+
d.rectangle((0, 0, 1, 1), (0, 255, 0, 127))
84+
assert im.getpixel((0, 0)) == (128, 127, 0)
85+
86+
If you would like to combine translucent color with an RGBA image underneath, you will
87+
need to combine multiple images::
88+
89+
from PIL import Image, ImageDraw
90+
im = Image.new("RGBA", (1, 1), (255, 0, 0, 255))
91+
im2 = Image.new("RGBA", (1, 1))
92+
d = ImageDraw.Draw(im2)
93+
d.rectangle((0, 0, 1, 1), (0, 255, 0, 127))
94+
im.paste(im2.convert("RGB"), mask=im2)
95+
assert im.getpixel((0, 0)) == (128, 127, 0, 255)
96+
6097
Fonts
6198
^^^^^
6299

0 commit comments

Comments
 (0)