Skip to content

Instantly share code, notes, and snippets.

@paractmol
Last active October 16, 2016 03:30
Show Gist options
  • Save paractmol/24b13d37d71e72e8c18566b13085ef42 to your computer and use it in GitHub Desktop.
Save paractmol/24b13d37d71e72e8c18566b13085ef42 to your computer and use it in GitHub Desktop.
# 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