simplify numpy array representation of image
<p>I have an image, read into <code>np.array</code> by PIL.
In my case, it's a<code>(1000, 1500)</code> <code>np.array</code>.</p>

<p>I'd like to simplify it for visualisation purposes.
By simplification, I following transformation from this matrix</p>

<pre><code>1 1 1 1 0 0
1 0 1 0 0 0 
</code></pre>

<p>to </p>

<pre><code>1 1 0
</code></pre>

<p>so, essentially, look at every <code>2*2</code> sample, if it satisfies some criteria, like more than 50% of 1's -> count it as a 1, if not, count is as a 0.</p>

<p>I don't know how to call it properly, but I believe there should be some well known mathematic procedure for doing so.</p>

<p>You could use <a href="http://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.fromarray" rel="nofollow"><code>PIL.Image.fromarray</code></a> to take the image into PIL, then <a href="http://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.resize" rel="nofollow">resize</a> or convert that image into a <a href="http://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.thumbnail" rel="nofollow">thumbnail</a> of your desired size. one benefit is this can be easily saved to file, drawn to a canvas, or displayed for debugging. only be aware of numeric types and image modes (a pitfall I often fall into). </p>

<p>creating the image is done by:</p>

<pre><code>from PIL import Image
image = Image.fromarray(arr).astype(np.uint8) #unsigned bytes for 8-bit grayscale
smallerImage = image.resize(desired_size, resample=Image.BILINEAR) #resize returns a copy thumbnail modifies in place
</code></pre>

<p>Getting the image back to a numpy array is as simple as: <code>np.array(image.getdata()).reshape(image.size)</code></p>

python arrays image numpy image-processing