我有一个png文件,我想删除所有非黑色像素(将非黑色像素转换为白色)。我如何在Python中轻松做到这一点?谢谢!
这是使用PIL的一种方法:
from PIL import Image
# Separate RGB arrays
im = Image.open(file(filename, 'rb'))
R, G, B = im.convert('RGB').split()
r = R.load()
g = G.load()
b = B.load()
w, h = im.size
# Convert non-black pixels to white
for i in range(w):
for j in range(h):
if(r[i, j] != 0 or g[i, j] != 0 or b[i, j] != 0):
r[i, j] = 255 # Just change R channel
# Merge just the R channel as all channels
im = Image.merge('RGB', (R, R, R))
im.save("black_and_white.png")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句