Skip to content

Commit d58f4d5

Browse files
authored
Added "Colors" to concepts (#9067)
2 parents 2195faf + 4cfef00 commit d58f4d5

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

docs/handbook/concepts.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ Palette
101101
The palette mode (``P``) uses a color palette to define the actual color for
102102
each pixel.
103103

104+
.. _colors:
105+
106+
Colors
107+
------
108+
109+
To specify colors, you can use tuples with a value for each channel in the image, e.g.
110+
``Image.new("RGB", (1, 1), (255, 0, 0))``.
111+
112+
If an image has a single channel, you can use a single number instead, e.g.
113+
``Image.new("L", (1, 1), 255)``. For "F" mode images, floating point values are also
114+
accepted. In the case of "P" mode images, these will be indexes for the color palette.
115+
116+
If a single value is used for an image with more than one channel, it will still be
117+
parsed::
118+
119+
>>> from PIL import Image
120+
>>> im = Image.new("RGBA", (1, 1), 0x04030201)
121+
>>> im.getpixel((0, 0))
122+
(1, 2, 3, 4)
123+
124+
Some methods accept other forms, such as color names. See :ref:`color-names`.
125+
104126
Info
105127
----
106128

docs/reference/ImageDraw.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ Colors
4545
^^^^^^
4646

4747
To specify colors, you can use numbers or tuples just as you would use with
48-
:py:meth:`PIL.Image.new` or :py:meth:`PIL.Image.Image.putpixel`. For “1”,
49-
“L”, and “I” images, use integers. For “RGB” images, use a 3-tuple containing
50-
integer values. For “F” images, use integer or floating point values.
48+
:py:meth:`PIL.Image.new`. See :ref:`colors` for more information.
5149

5250
For palette images (mode “P”), use integers as color indexes. In 1.1.4 and
5351
later, you can also use RGB 3-tuples or color names (see below). The drawing

docs/reference/PixelAccess.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Access using negative indexes is also possible. ::
5959

6060
Modifies the pixel at x,y. The color is given as a single
6161
numerical value for single band images, and a tuple for
62-
multi-band images.
62+
multi-band images. See :ref:`colors` for more information.
6363

6464
:param xy: The pixel coordinate, given as (x, y).
6565
:param color: The pixel value according to its mode,

src/PIL/Image.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,9 +1730,10 @@ def paste(
17301730
details).
17311731
17321732
Instead of an image, the source can be a integer or tuple
1733-
containing pixel values. The method then fills the region
1734-
with the given color. When creating RGB images, you can
1735-
also use color strings as supported by the ImageColor module.
1733+
containing pixel values. The method then fills the region
1734+
with the given color. When creating RGB images, you can
1735+
also use color strings as supported by the ImageColor module. See
1736+
:ref:`colors` for more information.
17361737
17371738
If a mask is given, this method updates only the regions
17381739
indicated by the mask. You can use either "1", "L", "LA", "RGBA"
@@ -1988,7 +1989,8 @@ def putdata(
19881989
sequence ends. The scale and offset values are used to adjust the
19891990
sequence values: **pixel = value*scale + offset**.
19901991
1991-
:param data: A flattened sequence object.
1992+
:param data: A flattened sequence object. See :ref:`colors` for more
1993+
information about values.
19921994
:param scale: An optional scale value. The default is 1.0.
19931995
:param offset: An optional offset value. The default is 0.0.
19941996
"""
@@ -2047,7 +2049,7 @@ def putpixel(
20472049
Modifies the pixel at the given position. The color is given as
20482050
a single numerical value for single-band images, and a tuple for
20492051
multi-band images. In addition to this, RGB and RGBA tuples are
2050-
accepted for P and PA images.
2052+
accepted for P and PA images. See :ref:`colors` for more information.
20512053
20522054
Note that this method is relatively slow. For more extensive changes,
20532055
use :py:meth:`~PIL.Image.Image.paste` or the :py:mod:`~PIL.ImageDraw`
@@ -3055,12 +3057,12 @@ def new(
30553057
:param mode: The mode to use for the new image. See:
30563058
:ref:`concept-modes`.
30573059
:param size: A 2-tuple, containing (width, height) in pixels.
3058-
:param color: What color to use for the image. Default is black.
3059-
If given, this should be a single integer or floating point value
3060-
for single-band modes, and a tuple for multi-band modes (one value
3061-
per band). When creating RGB or HSV images, you can also use color
3062-
strings as supported by the ImageColor module. If the color is
3063-
None, the image is not initialised.
3060+
:param color: What color to use for the image. Default is black. If given,
3061+
this should be a single integer or floating point value for single-band
3062+
modes, and a tuple for multi-band modes (one value per band). When
3063+
creating RGB or HSV images, you can also use color strings as supported
3064+
by the ImageColor module. See :ref:`colors` for more information. If the
3065+
color is None, the image is not initialised.
30643066
:returns: An :py:class:`~PIL.Image.Image` object.
30653067
"""
30663068

0 commit comments

Comments
 (0)