Skip to content

Instantly share code, notes, and snippets.

@zetter zetter/Unzoomify
Created Oct 8, 2010

Embed
What would you like to do?
# Hacked from http://github.com/dball/zoomifier/blob/master/lib/zoomifier.rb
require 'fileutils'
require 'open-uri'
require 'rubygems'
require 'rmagick'
require 'rexml/document'
class Zoomify
TILESIZE = 256
def self.unzoomify(url)
tmpdir = 'tmp'
# FileUtils.rm_rf(tmpdir) if File.exists?(tmpdir)
# Dir.mkdir(tmpdir)
doc = nil
begin
open("#{url}/ImageProperties.xml") do |f|
doc = REXML::Document.new(f)
end
rescue OpenURI::HTTPError
return nil
end
attrs = doc.root.attributes
return nil unless attrs['TILESIZE'] == '256' && attrs['VERSION'] == '1.8'
width = attrs['WIDTH'].to_i
height = attrs['HEIGHT'].to_i
tiles = attrs['NUMTILES'].to_i
@image_paths = (0 .. tiles/256).to_a
max_level = 7
puts "max_level: #{max_level}"
image = Magick::Image.new(width, height)
(0 .. width / TILESIZE).each do |column|
(0 .. height / TILESIZE).each do |row|
puts "getting: #{max_level}-#{column}-#{row}.jpg"
filename = "#{max_level}-#{column}-#{row}.jpg"
get_tile(url, tmpdir, filename)
tile_image = Magick::Image.read("#{tmpdir}/#{filename}").first
image.composite!(tile_image, column*TILESIZE, row*TILESIZE, Magick::OverCompositeOp)
time_image = nil
GC.start
end
end
image.write('file.png') { }
image = nil
GC.start
end
def self.tile_group(num)
"TileGroup#{num}"
end
def self.get_tile(url, tmpdir, filename)
return filename if File.exists? "#{tmpdir}/#{filename}"
@image_paths.each do |path|
begin
open("#{tmpdir}/#{filename}", 'wb') {|f| f.write(open("#{url}/#{tile_group(path)}/#{filename}").read)}
[path - 1, path + 1, path].each do |p|
item = @image_paths.delete(p)
@image_paths.unshift(item) if item
end
puts "path: #{path}"
return filename
rescue OpenURI::HTTPError
end
end
nil
end
end
Zoomify.unzoomify('http://example.com')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.