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

科技网编2023-04-08 12:492080
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')

评论区