2014-11-20 01:04:39 +03:00
.. py:module :: PIL.PyAccess
.. py:currentmodule :: PIL.PyAccess
2020-06-22 06:52:50 +03:00
:py:mod: `~PIL.PyAccess` Module
==============================
2014-11-20 01:04:39 +03:00
2020-06-22 06:52:50 +03:00
The :py:mod: `~PIL.PyAccess` module provides a CFFI/Python implementation of the :ref: `PixelAccess` . This implementation is far faster on PyPy than the PixelAccess version.
2014-11-20 01:04:39 +03:00
.. note :: Accessing individual pixels is fairly slow. If you are
2022-03-01 06:13:40 +03:00
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` .
2014-11-20 01:04:39 +03:00
Example
-------
2023-02-24 00:17:10 +03:00
The following script loads an image, accesses one pixel from it, then changes it. ::
2016-09-03 05:23:42 +03:00
2014-11-20 01:04:39 +03:00
from PIL import Image
2021-12-03 23:50:08 +03:00
with Image.open("hopper.jpg") as im:
2020-02-29 02:29:44 +03:00
px = im.load()
2021-12-03 23:50:08 +03:00
print(px[4, 4])
px[4, 4] = (0, 0, 0)
print(px[4, 4])
2014-11-20 01:04:39 +03:00
Results in the following::
(23, 24, 68)
(0, 0, 0)
2023-02-24 00:17:10 +03:00
Access using negative indexes is also possible. ::
2018-11-05 13:30:04 +03:00
2021-12-03 23:50:08 +03:00
px[-1, -1] = (0, 0, 0)
print(px[-1, -1])
2018-11-05 13:30:04 +03:00
2016-09-03 05:23:42 +03:00
2014-11-20 01:04:39 +03:00
:py:class: `PyAccess` Class
--------------------------
.. autoclass :: PIL.PyAccess.PyAccess()
:members: