python PIL像素点
时间: 2023-11-11 21:00:35 浏览: 127
PIL是Python Imaging Library的缩写,它是Python中一个非常常用的图像处理库。在PIL中,可以通过Image对象的getpixel()和putpixel()方法来获取和修改像素点的值。
例如,我们可以通过下面的代码来获取一张图片中某个像素点的RGB值:
```python
from PIL import Image
# 打开图片
img = Image.open('test.jpg')
# 获取像素点的RGB值
r, g, b = img.getpixel((100, 100))
print(r, g, b)
```
这里我们打开了一张名为'test.jpg'的图片,并获取了它在(100, 100)位置上的像素点的RGB值。
如果我们想要修改某个像素点的值,可以使用putpixel()方法:
```python
from PIL import Image
# 打开图片
img = Image.open('test.jpg')
# 修改像素点的RGB值
img.putpixel((100, 100), (255, 0, 0))
# 保存修改后的图片
img.save('test_modified.jpg')
```
这里我们将(100, 100)位置上的像素点的RGB值修改为(255, 0, 0),即红色。然后将修改后的图片保存为'test_modified.jpg'。
阅读全文
相关推荐








