Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Adding RMagick to combine images
require 'open-uri'
require 'uri'
require 'rmagick'
class PhotoDownloader
RESOURCE = 'http://access.nypl.org/image.php/'
def initialize(id, zoom, dimension)
@id = id
@zoom = zoom
@width, @height = dimension
end
def save
0.upto(@height-1) do |height|
0.upto(@width-1) do |width|
download(form_url(height, width), "#{@id}_#{height}_#{width}.png")
end
end
end
def combine
@tiles ||= save
@image_list = Magick::ImageList.new
verticals = @tiles.each_with_index.map do |tiles, n|
image_list = Magick::ImageList.new
combine = "combine-#{n}.png"
tiles.each do |tile|
image_list.push(Magick::Image.from_blob(tile).first)
end
image_list.append(true)
end
verticals.each do |combine|
@image_list.push(combine)
end
@image_list.append(false).write("#{@id}.png")
end
private
def form_url(x,y)
URI.join(RESOURCE, "#{@id}/", fetch_tile(x,y))
end
def fetch_tile(x = 0, y = 0)
"tiles/0/#{@zoom}/#{x}_#{y}.png"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment