From Wikimedia Commons, the free media repository
Jump to navigation Jump to 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, do python 331. The full image will be output.png.

#!/usr/bin/env python
import urllib
import sys
import re
import os

url = '' % 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('' + 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))))