擅长:python、mysql、java
<p>用<a href="https://pillow.readthedocs.io/en/latest/" rel="noreferrer">Pillow</a>怎么样:</p>
<pre><code>from PIL import Image
img = Image.open('image.png').convert('LA')
img.save('greyscale.png')
</code></pre>
<hr/>
<p>使用matplotlib和<a href="https://pillow.readthedocs.io/en/3.2.x/reference/Image.html#PIL.Image.Image.convert" rel="noreferrer">the formula</a></p>
<pre><code>Y' = 0.2989 R + 0.5870 G + 0.1140 B
</code></pre>
<p>你可以:</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
def 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()
</code></pre>