Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
Pillow (PIL Fork) 12.0.0 documentation
Light LogoDark Logo
Pillow (PIL Fork) 12.0.0 documentation
Back to top

PixelAccess class

The PixelAccess class provides read and write access toPIL.Image data at a pixel level.

Note

Accessing individual pixels is fairly slow. If you arelooping over all of the pixels in an image, there is likelya faster way using other parts of the Pillow API.

Image,ImageChops andImageOpshave methods for many standard operations. If you wish to performa custom mapping, check outpoint().

Example

The following script loads an image, accesses one pixel from it, thenchanges it.

fromPILimportImagewithImage.open("hopper.jpg")asim: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])

PixelAccess class

classPixelAccess
__getitem__(self,xy:tuple[int,int])float|tuple[int,...]

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

Parameters:

xy – The pixel coordinate, given as (x, y).

Returns:

a pixel value for single band images, a tuple ofpixel values for multiband images.

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

Modifies the pixel at x,y. The color is given as a singlenumerical value for single band images, and a tuple formulti-band images. SeeColors for more information.

Parameters:
  • xy – The pixel coordinate, given as (x, y).

  • color – The pixel value according to its mode,e.g. tuple (r, g, b) for RGB mode.

On this page

[8]ページ先頭

©2009-2025 Movatter.jp