擅长:python、mysql、java
<p>您可以使用遮罩从numpy的矢量化(隐式循环)中获益,这将更快:</p>
<pre><code>mask = pixel_array == paddingVal
byte_array[mask] = 0
byte_array[~mask] = np.round(255.0 * (pixel_array[~mask] - minVal) / (maxVal - minVal - 1.0))
</code></pre>
<p>也可以这样做,这样更干净,因为您不必事先创建<code>byte_array</code>:</p>
^{pr2}$
<p>编辑:正如joekington在一篇关于这个问题的评论中所指出的,这是用内存来换取速度的。在</p>