The National Gallery houses the national collection of Western European painting from the 13th to the 19th centuries. It is on show 361 days a year, free of charge.

You can look at the collection online via a fancy zooming panning scrolling widget. It’s very clever, but the experience of peering at a painting through a tiny porthole (even on the ‘full screen’ version) isn’t the best one.

I don’t believe that our shared cultural heritage should be locked up behind browser widgets, so I took a look at reverse-engineering the system. That may sound controversial, but it’s actually completely in line with the Gallery’s stated aim of making its pictures available:

The Gallery aims to study and care for the collection, while encouraging the widest possible access to the pictures

Whether they actually believe their rhetoric, I don’t know. We’ll see!

Each painting is cut up into tiles at various resolutions to support panning and zooming. There’s a cursory effort at obfuscating the tile identifiers, via a little obfuscate function that turns the digits 0123456789 into vRfOdXapKz respectively and by mixing the parameters together in a strange order. But really, it’s fairly simple stuff. Knowing the size of the image, the size of a tile, and the zoom level, it’s trivial to fetch all the tiles.

Having got a set of tiles, stitching them together is a little harder. The full image can be very large (5,000 pixels on a side), and naïvely-written image processing libraries tend to use up all the available memory.

The solution I found that worked best was the montage command from ImageMagick. However, since it’s designed to work with tiles that are all the same shape, I had to do a bit of preprocessing on the truncated tiles at the right-hand and bottom edges to make them square beforehand, then crop the resultant image down to the desired size.

Having done all that, though, I can now pull down all the tiles and stitch them into a single beautiful image. I can now have high-quality offline copies of works of art to browse at my leisure, use as a desktop background, set as a screensaver, or just draw moustaches on.

The fact that you can now get these high-quality images does not, unfortunately, mean that you can do what you like with them. It may sound crazy, but, at least in the UK, copyright does apply to the photos, even though they may simply be reproductions of works made hundreds of years ago.

I’ve put my code on GitHub along with instructions. Some computing knowledge is required to use it, and it almost certainly won’t work on Windows in its current state.