convert image pixels from square to hexagonal
<p>How can i convert the pixels of an image from square to hexagonal? Doing so i need to extract the rgb values from each hex pixel. Is there any library or function that simplify this process?</p>

<p>Example : Mona Lisa Hexagonal Pixel Shape </p>

<p><a href="http://i.stack.imgur.com/o3isE.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/o3isE.jpg" alt="enter image description here"></a>
Nothing tried.  Thanks</p>

<p>Here's a possible approach, though I am sure if you are able to write code to read, manipulate and use pixels from a file format that hasn't been invented yet, you should be able to create that file yourself ;-)</p>

<p>You could generate a hexagonal grid, using <strong>ImageMagick</strong> which is installed on most Linux distros and is available for OSX and Windows. Here, I am just doing things at the command-line in the Terminal, but there are Python, Perl, PHP, .Net, C/C++ and other bindings too - so take your pick.</p>

<p>First make a grid of hexagons - you'll have to work out the size you need, mine is arbitrary:</p>

<pre><code>convert -size 512x256 pattern:hexagons hexagons.png
</code></pre>

<p><a href="http://i.stack.imgur.com/dvvcT.png" rel="nofollow"><img src="http://i.stack.imgur.com/dvvcT.png" alt="enter image description here"></a></p>

<p>Now, fill in the hexagons, each with a different colour, I am just doing some examples of flood-filling here to give you the idea. Ideally, you would colour the first (top-left) hexagon with colour <code>#000</code> and the next one across with <code>#001</code> so that you could iterate through the coordinates of the output image as consecutive colours. Also, depending on your output image size, you may need to use a 32-bit PNG to accommodate the number of hexels (hexagonal pixels).</p>

<pre><code>convert hexagons.png                          \
   -fill red - draw "color 100,100 floodfill" \
   -fill blue -draw "color 200,200 floodfill" \
   colouredmask.png
</code></pre>

<p><a href="http://i.stack.imgur.com/APU6L.png" rel="nofollow"><img src="http://i.stack.imgur.com/APU6L.png" alt="enter image description here"></a></p>

<p>Now iterate through all the colours, making every colour <em>except</em> that colour transparent. Note that I have added a black border just so you can see the context on StackOverflow's white background:</p>

<pre><code>convert colouredmask.png -fill none +opaque red onecell.png
</code></pre>

<p><a href="http://i.stack.imgur.com/6hqzS.png" rel="nofollow"><img src="http://i.stack.imgur.com/6hqzS.png" alt="enter image description here"></a></p>

<p>Now mask the original image with that mask and get the average colour of that one cell and write it to your yet-to-be-invented file format. Repeat for all cells/colours.</p>

<p>Note that the basic hexagon pattern is 30x18, so you should size your grid as multiples of that for it to tesselate properly.</p>

<p>Note that if you have lots of these to process, you should consider using something like <strong>GNU Parallel</strong> to take advantage of multiple cores. So, if you make a script called <code>ProcessOneImage</code> and you have 2,000 images to do, you would use:</p>

<pre><code>parallel ProcessOneImage ::: *.png
</code></pre>

<p>and it will keep, say 8, jobs running all the time if your PC has 8 cores. There are many more options, try <code>man parallel</code>. </p>

python opencv numpy image-processing