RGB -> Gray scale
Gray scale(灰阶影像)
from PIL import Imageimg2 = Image.open('GIF\\egg0.gif') #file pathimg2 = img2.convert('L') # convert to L (Grayscale)img2.save('GIF\\egg0_grayscale.gif') #file pathimg2.close()
原图(RGB)
灰阶(Gray Scale)
gray scale -> black and white picture(黑白图)
img2 = Image.open('GIF\\egg0_grayscale.gif') width , heigth = img2.size #取得图片的宽度 、高度img2 = img2.convert('L') for x in range(width): for y in range(heigth): if img2.getpixel((x,y)) >= 127: img2.putpixel((x,y) , 255) #putpixel第一个参数是像素位置,第二个参数是要该像素变更的值,255(白色) else: img2.putpixel((x,y) , 0) #0(黑色)img2.save('GIF\\egg0_blackAndWhite.gif') img2.show()img2.close()
黑白图black and white picture
https://pillow.readthedocs.io/en/stable/reference/Image.html