更改PIL中的像素颜色值
发布于 2021-01-29 18:33:59
我需要在python中更改图像的像素颜色。除了像素值(255,0,0)红色外,我需要将每个像素颜色值更改为黑色(0,0,0)。我尝试了以下代码,但没有帮助。
from PIL import Image
im = Image.open('A:\ex1.jpg')
for pixel in im.getdata():
if pixel == (255,0,0):
print "Red coloured pixel"
else:
pixel = [0, 0, 0]
关注者
0
被浏览
51
1 个回答
-
参见此Wikibook:https
:
//zh.wikibooks.org/wiki/Python_Imaging_Library/Editing_Pixels修改该代码以适合您的问题:
pixels = img.load() # create the pixel map for i in range(img.size[0]): # for every pixel: for j in range(img.size[1]): if pixels[i,j] != (255, 0, 0): # change to black if not red pixels[i,j] = (0, 0 ,0)