User:InverseHypercube/tretyakov.py

From Wikimedia Commons, the free media repository
Jump to: navigation, search

A Python script for downloading full-resolution images from the Tretyakov Gallery website. You must have ImageMagick installed, and run it in an empty directory since it creates files and will overwrite existing ones if they have the same name. You must call it with the image ID as the argument; for example, for http://www.tretyakovgallery.ru/en/collection/_show/image/_id/331, do python tretyakov.py 331. The full image will be output.png.

#!/usr/bin/env python
import urllib
import sys
import re
import os
 
url = 'http://www.tretyakovgallery.ru/ajax/?action=fetch_image&id=%s' % sys.argv[1]
url_finder = re.compile("\['src'\] = '(.+?\.jpg)'")
 
images = urllib.urlopen(url).read()
 
image_list = re.findall(url_finder, images)
 
width = images.count("Images['1']") / 4
height = len(image_list) / width
 
for index, image in enumerate(image_list):
    urllib.urlretrieve('http://www.tretyakovgallery.ru/pictures/' + image, str(index) + '.jpg')
 
rows = [range(row * width, row * width + width) for row in range(height)]
 
for index, row in enumerate(rows):
    os.system('convert %s +append %s' % (' '.join([str(a) + '.jpg' for a in row]), str(index) + '.png'))
 
os.system('convert %s -append output.png' % ' '.join(str(a) + '.png' for a in range(len(rows))))