Image regeneration with nearest neighbor search So what I would like to achieve is this:


User submits Image 1 - this image is then broken down into X by X sized blocks
User submits Image 2 - then the program/algorithm takes all the blocks generated in the above step, and reconstructs Image 2 using them


Example Image 2:



Example Output:



What I would like to know is, are there any libraries, or some easy way of doing this (ideally in Javascript, as I don't know any other languages) - or even some standalone software tool to accomplish this?
I would have done it with a script and NETPBM like so:

1) reducing Image1 (the tiles) to size X by X -> Image1R 

2) recording the mapping from pixel values of Image1R to coordinates (no need to worry about duplicates as an exact RGB match of two or more averaged pixels is unlikely)

3) remapping Image2 using Image1R as the color map -> Image2M

4) reading pixels of Image2M, consulting the map, and picking the tile corresponding to the coordinates

5) concatenating the selected tiles across and down

There is a separate tool for all the above image manipulations in NETPBM.

Any scripting language that allows spawning other processes and supports associative arrays, would do.