python3---取图片中特定的像素替换指定的颜色像素

python3—取图片中特定的像素替换指定的颜色像素

1、原始图片
这里写图片描述

2、修改脚本:

from PIL import Image

i = 1
j = 1
img = Image.open("e:/pic/222.jpg")#读取系统的内照片
print (img.size)#打印图片大小
print (img.getpixel((4,4)))

width = img.size[0]#长度
height = img.size[1]#宽度
for i in range(0,width):#遍历所有长度的点
    for j in range(0,height):#遍历所有宽度的点
        data = (img.getpixel((i,j)))#打印该图片的所有点
        print (data)#打印每个像素点的颜色RGBA的值(r,g,b,alpha)
        print (data[0])#打印RGBA的r值
        if (data[0]>=170 and data[1]>=170 and data[2]>=170):#RGBA的r值大于170,并且g值大于170,并且b值大于170
            img.putpixel((i,j),(234,53,57,255))#则这些像素点的颜色改成大红色
img = img.convert("RGB")#把图片强制转成RGB
img.save("e:/pic/testee1.jpg")#保存修改像素点后的图片

3、运行脚本:
这里写图片描述

4、图片变化成
这里写图片描述

参考:https://www.cnblogs.com/kongzhagen/p/6295925.html
https://zhidao.baidu.com/question/812387495068639252.html
http://fengmm521.blog.163.com/blog/static/25091358201512013147207/

猜你喜欢

转载自blog.csdn.net/xwbk12/article/details/78998196
今日推荐