Created
October 8, 2010 06:41
-
-
Save zetter/616443 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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