是否可以在Python中更改单个像素的颜色?

是否可以在Python中更改单个像素的颜色?,python,python-imaging-library,pixel,Python,Python Imaging Library,Pixel,我需要python来改变图片上单个像素的颜色,我该怎么做 以Gabi Purcaru的例子为基础,这里是从 使用PIL可靠修改单个像素的最简单方法是: x, y = 10, 25 shade = 20 from PIL import Image im = Image.open("foo.png") pix = im.load() if im.mode == '1': value = int(shade >= 127) # Black-and-white (1-bit) elif

我需要python来改变图片上单个像素的颜色,我该怎么做

以Gabi Purcaru的例子为基础,这里是从

使用PIL可靠修改单个像素的最简单方法是:

x, y = 10, 25
shade = 20

from PIL import Image
im = Image.open("foo.png")
pix = im.load()

if im.mode == '1':
    value = int(shade >= 127) # Black-and-white (1-bit)
elif im.mode == 'L':
    value = shade # Grayscale (Luminosity)
elif im.mode == 'RGB':
    value = (shade, shade, shade)
elif im.mode == 'RGBA':
    value = (shade, shade, shade, 255)
elif im.mode == 'P':
    raise NotImplementedError("TODO: Look up nearest color in palette")
else:
    raise ValueError("Unexpected mode for PNG image: %s" % im.mode)

pix[x, y] = value 

im.save("foo_new.png")

这将在PIL 1.1.6及更高版本中起作用。如果你运气不好,不得不支持旧版本,你可以牺牲性能,用
im.putpixel((x,y),value)替换
pix[x,y]=value

@akonsu:从他的标签判断,他已经找到了那个库……什么文件格式?仅位图数据?图像为PNG格式。读取