Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Python Imaging Library/Editing Pixels

From Wikibooks, open books for an open world
<Python Imaging Library

With PIL you can easily access and change the data stored in the pixels of an image. To get the pixel map, callload() on an image. The pixel data can then be retrieved by indexing the pixel map as an array.

pixelMap=img.load()#create the pixel mappixel=pixelMap[0,0]#get the first pixel's value

When you change the pixel data, it is changed in the image it came from (since the pixel map is just a reference to the data rather than a copy).

Example

[edit |edit source]
Resultant image

The following snippet shows how to change the pixel values in an image based on the index of the pixel:

fromPILimportImage# PIL accesses images in Cartesian co-ordinates, so it is Image[columns, rows]img=Image.new('RGB',(250,250),"black")# create a new black imagepixels=img.load()# create the pixel mapforiinrange(img.size[0]):# for every col:forjinrange(img.size[1]):# For every rowpixels[i,j]=(i,j,100)# set the colour accordinglyimg.show()
Retrieved from "https://en.wikibooks.org/w/index.php?title=Python_Imaging_Library/Editing_Pixels&oldid=4032616"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp