擅长:python、mysql、java
<p>这对我有效。它有能力分成n*m块。相应地贴上你的图片。</p>
<pre><code>def chunkify(img, block_width=4, block_height=4):
shape = img.shape
x_len = shape[0]//block_width
y_len = shape[1]//block_height
chunks = []
x_indices = [i for i in range(0, shape[0]+1, block_width)]
y_indices = [i for i in range(0, shape[1]+1, block_height)]
shapes = list(zip(x_indices, y_indices))
for i in range(len(shapes)):
try:
start_x = shapes[i][0]
start_y = shapes[i][1]
end_x = shapes[i+1][0]
end_y = shapes[i+1][1]
chunks.append( shapes[start_x:end_x][start_y:end_y] )
except IndexError:
print('End of Array')
return chunks
</code></pre>
<p><a href="https://github.com/QuantumNovice/ImageProcessing/blob/master/image_chunkify.py" rel="nofollow noreferrer">https://github.com/QuantumNovice/ImageProcessing/blob/master/image_chunkify.py</a></p>