File: PixelAccess.rst

package info (click to toggle)
pillow 12.0.0-1
  • links: PTS
  • area: main
  • in suites: forky
  • size: 72,636 kB
  • sloc: python: 49,518; ansic: 38,787; makefile: 302; sh: 168; javascript: 85
file content (66 lines) | stat: -rw-r--r-- 2,022 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
.. _PixelAccess:

:py:class:`PixelAccess` class
=============================

The PixelAccess class provides read and write access to
:py:class:`PIL.Image` data at a pixel level.

.. note:: Accessing individual pixels is fairly slow. If you are
          looping over all of the pixels in an image, there is likely
          a faster way using other parts of the Pillow API.

          :mod:`~PIL.Image`, :mod:`~PIL.ImageChops` and :mod:`~PIL.ImageOps`
          have methods for many standard operations. If you wish to perform
          a custom mapping, check out :py:meth:`~PIL.Image.Image.point`.

Example
-------

The following script loads an image, accesses one pixel from it, then
changes it. ::

    from PIL import Image

    with Image.open("hopper.jpg") as im:
        px = im.load()
    print(px[4, 4])
    px[4, 4] = (0, 0, 0)
    print(px[4, 4])

Results in the following::

    (23, 24, 68)
    (0, 0, 0)

Access using negative indexes is also possible. ::

    px[-1, -1] = (0, 0, 0)
    print(px[-1, -1])



:py:class:`PixelAccess` class
-----------------------------

.. class:: PixelAccess
  :canonical: PIL.Image.core.PixelAccess

  .. method:: __getitem__(self, xy: tuple[int, int]) -> float | tuple[int, ...]

        Returns the pixel at x,y. The pixel is returned as a single
        value for single band images or a tuple for multi-band images.

        :param xy: The pixel coordinate, given as (x, y).
        :returns: a pixel value for single band images, a tuple of
                  pixel values for multiband images.

  .. method:: __setitem__(self, xy: tuple[int, int], color: float | tuple[int, ...]) -> None

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

        :param xy: The pixel coordinate, given as (x, y).
        :param color: The pixel value according to its mode,
                      e.g. tuple (r, g, b) for RGB mode.