Skip to content

Instantly share code, notes, and snippets.

@FatCobra
Forked from henrik/google_art_project.rb
Created April 24, 2011 11:11
Show Gist options
  • Select an option

  • Save FatCobra/939488 to your computer and use it in GitHub Desktop.

Select an option

Save FatCobra/939488 to your computer and use it in GitHub Desktop.
Google Art Project fullsize image downloader. Specify the page URL and the tiles are downloaded, stitched and trimmed.
# Google Art Project fullsize image downloader.
# By Henrik Nyh <http://henrik.nyh.se> 2011-02-05 under the MIT license.
# Requires Ruby and ImageMagick.
#
# Usage e.g.:
# ruby google_art_project.rb http://www.googleartproject.com/museums/tate/portrait-of-william-style-of-langley-174
#
# You can specify multiple URLs on the command line, separated by space.
# Or you can specify no URLs on the command line and instead list them at the end of this file, one on each line,
# with "__END__" before the list.
#
# On OS X, it sets "Downloaded from" metadata and reveals in Finder.
#
# Can reportedly run on Windows as well, with Ruby from http://www.ruby-lang.org/en/downloads/
# and ImageMagick from http://www.imagemagick.org/script/binary-releases.php#windows
# Note that you may need to edit the DIRECTORY below.
require "open-uri"
module Kernel
def windows?
RUBY_PLATFORM.include?("mswin")
end
end
class GAPDownloader
if windows?
# Case-sensitive. Use forward slashes, or double-escape backslashes.
DIRECTORY = "C:/WINDOWS/Temp"
else
DIRECTORY = "/tmp"
end
# You can lower this if you get ridiculously high-res images otherwise.
MAX_ZOOM_ALLOWED = 10
class RuntimeError < StandardError; end
def initialize(url)
ensure_image_magick!
@url = url
verify_url!
end
def download
get_image_id
determine_zoom
get_tiles
stitch_tiles
trim
set_metadata
done
end
private
def ensure_image_magick!
if !windows? && `which montage`.empty?
error "You must have ImageMagick installed. Could not find 'montage' in your PATH."
end
end
def verify_url!
unless @url.to_s.match(%r{\Ahttp://www\.googleartproject\.com/})
error "Please specify a Google Art Project URL."
end
end
def get_image_id
@html = open(@url).read
@iid = @html[/data-thumbnail="(.+?)"/, 1]
unless @iid
error "Couldn't find an image at this URL, sorry!"
end
end
def determine_zoom
0.upto(MAX_ZOOM_ALLOWED) do |zoom|
open(tile_url(0, 0, zoom))
@max_zoom = zoom
end
rescue OpenURI::HTTPError => e
raise unless e.message == "404 Not Found"
end
def get_tiles
@max_x = 999
@max_y = 999
0.upto(@max_y) do |y|
0.upto(@max_x) do |x|
url = tile_url(x, y, @max_zoom)
begin
data = open(url) # Raises at 404.
puts "Getting #{url}..."
path = tile_path(x, y)
File.open(path, "wb") { |f| f.print data.read }
rescue OpenURI::HTTPError => e
raise unless e.message == "404 Not Found"
if y.zero?
# Found max x. Start on next row.
@max_x = x - 1
break
else
# Found max y. We have all tiles, so bail.
@max_y = y - 1
return
end
end
end
end
end
def stitch_tiles
# `montage` is ImageMagick.
# We first stitch together the tiles of each row, then stitch all rows.
# Stitching the full image all at once can get extremely inefficient for large images.
tiles_wide = @max_x + 1
tiles_high = @max_y + 1
puts "Stitching #{tiles_wide} x #{tiles_high} = #{tiles_wide*tiles_high} tiles..."
0.upto(@max_y) do |y|
tiles = (0..@max_x).map { |x| tile_path(x, y) }.join(' ')
`montage #{tiles} -geometry +0+0 -tile #{tiles_wide}x1 #{row_path(y)}`
end
tiles = (0..@max_y).map { |y| row_path(y) }.join(' ')
`montage #{tiles} -geometry +0+0 -tile 1x#{tiles_high} #{full_path}`
end
def trim
# Trim the black blocks that may appear on right and bottom.
# We first add a black border to ensure no other color is trimmed, as described on
# http://www.imagemagick.org/Usage/crop/#trim
`convert #{full_path} -bordercolor black -border 1x1 -trim #{full_path}`
end
def set_metadata
if !windows? && !`which xattr`.empty?
# Set "Downloaded from" Finder metadata, like Safari does.
system('xattr', '-w', 'com.apple.metadata:kMDItemWhereFroms', @url, full_path)
end
end
def done
puts "Done: #{full_path}"
# Reveal in Finder if on OS X.
unless windows?
`which osascript && osascript -e 'tell app "Finder"' -e 'reveal POSIX file "#{full_path}"' -e 'activate' -e 'end'`
end
end
def error(message)
raise GAPDownloader::RuntimeError, "#{message} (#{@url})"
end
def tile_url(x, y, zoom)
# The subdomain can seemingly be anything from lh3 to lh6.
"http://lh5.ggpht.com/#{@iid}=x#{x}-y#{y}-z#{zoom}"
end
def tile_path(x, y)
File.join(DIRECTORY, "gap-#{@iid}-tile-#{x}-#{y}.jpg")
end
def row_path(y)
File.join(DIRECTORY, "gap-#{@iid}-row-#{@max_zoom}-#{y}.jpg")
end
def full_path
File.join(DIRECTORY, "gap-#{@iid}-full.jpg")
end
end
if __FILE__ == $0
urls = ARGV.any? ? ARGV : (defined?(DATA) ? DATA.read.strip.split("\n") : [])
puts "Error: No URLs given!" if urls.empty?
urls.each do |url|
begin
GAPDownloader.new(url).download
rescue GAPDownloader::RuntimeError => e
puts "Error: #{e.message}"
end
end
end
@mischaz
Copy link

mischaz commented Jul 25, 2011

Where have all the Comments gone?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment