擅长:python、mysql、java
<p>这是对以上乔·金顿回答的修改。以下是如果图像也包含alpha通道时如何执行此操作。</p>
<pre><code>import numpy as np
import Image
im = Image.open('fig1.png')
im = im.convert('RGBA')
data = np.array(im)
r1, g1, b1 = 0, 0, 0 # Original value
r2, g2, b2, a2 = 255, 255, 255, 255 # Value that we want to replace it with
red, green, blue, alpha = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:4][mask] = [r2, g2, b2, a2]
im = Image.fromarray(data)
im.save('fig1_modified.png')
</code></pre>
<p>我花了很长时间才弄明白如何使它工作。我希望它能帮助别人。</p>