读取图片RGB
读取图片RGB
读取坐标位置为(100,100)处的像素颜色的RGB值
from PIL import Image
image = Image.open('./1.jpg')
# 获取指定坐标处像素的RGB值
x, y = 100, 100
r, g, b = image.getpixel((x, y))
print(f"坐标({x}, {y})处的RGB值为: R={r}, G={g}, B={b}")
我依旧拿dabai.png进行测试,但是报错
我将图片宽高修改为正确的宽高之后,成功读出图片RGB
循环遍历图片所有像素
from PIL import Image
image = Image.open('./1.jpg')
width, height = image.size
for y in range(height):
for x in range(width):
r, g, b = image.getpixel((x, y))
print(f"坐标({x}, {y})处的RGB值为: R={r}, G={g}, B={b}")
getpixel()
方法获取该坐标处像素的 RGB 值