用
Pillow怎么做:
from PIL import Imageimg = Image.open('image.png').convert('LA')img.save('greyscale.png')使用matplotlib和公式Y' = 0.2989 R + 0.5870 G + 0.1140 B
你可以做:
import numpy as npimport matplotlib.pyplot as pltimport matplotlib.image as mpimgdef rgb2gray(rgb): return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])img = mpimg.imread('image.png') gray = rgb2gray(img) plt.imshow(gray, cmap=plt.get_cmap('gray'), vmin=0, vmax=1)plt.show()


