使用pillow读取、修改图片RGB像素

获取像素方法:

from PIL import Image
img = Image.open("xxx.jpg")
#将图片分成小像素方块
pix = img.load()
# 获取图片大小
width = img.size[0]
height = img.size[1]
#width,height = img.size
for x in range(width):
    for y in range(height):
        r, g, b = pix[x, y]
        print(r, g, b)

修改像素方法:

from PIL import Image
img = Image.open("xxx.jpg")
#将图片分成小像素方块
pix = img.load()
# 获取图片大小
width = img.size[0]
height = img.size[1]
for x in range(0,width):
    for y in range(0,height):
        rgb = pix[x,y]      # 获取一个像素块的rgb
        r, g, b = pix[x, y]
        if b>130 and r<120: # 自定义某些规则
            pix[x, y] = (255, 0, 0) # 修改单个像素点
 
#============3.保存图片============
img.save("new.jpg")
posted @ 2021-04-14 11:59  爱吃砂糖橘的白龙  阅读(2508)  评论(2编辑  收藏  举报