Python PIL putpixel () 方法:像素级修改图像教程


发布日期 : 2024-06-17 18:19:54 UTC

访问量: 10 次浏览

Python PIL putpixel()方法

PIL是Python成像库,它为Python解释器提供了图像编辑功能。PixelAccess类提供了对PIL.Image数据在像素级的读写权限。

访问单个像素是相当慢的。如果你要在一个图像中的所有像素上进行循环,那么使用 Pillow API的其他部分可能会有更快的方法。

putpixel() 修改x,y处的像素。对于单波段图像,颜色是以单一数值给出的,对于多波段图像,是一个元组。

语法

putpixel(self, xy, color)

参数

xy
像素坐标,以(x, y)的形式给出。

value
– 像素值。

返回

一个带有像素的图像。

使用的图片

Python PIL putpixel()方法方法")

   
# Importing Image from PIL package 
from PIL import Image
      
# creating a image object
image = Image.open(r'C:\Users\System-Pc\Desktop\python.png') 
  
width, height = image.size
  
for x in range(height):
    image.putpixel( (x, x), (0, 0, 0, 255) )
  
image.show()

输出

Python PIL putpixel()方法方法")

另一个例子

这里我们改变颜色参数。

使用到的图片

Python PIL putpixel()方法方法")

# Importing Image from PIL package 
from PIL import Image
      
# creating a image object
image = Image.open(r'C:\Users\System-Pc\Desktop\ybear.jpg') 
  
width, height = image.size
  
for x in range(height):
    image.putpixel( (x, x), (10, 10, 10, 255) )
  
image.show()

输出

Python PIL putpixel()方法方法")