Skip to content

Instantly share code, notes, and snippets.

@tdiprima
Forked from jpstroop/dzi_to_iiif.py
Created October 15, 2020 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdiprima/ec9f6967666205074afa8c8c31d02c50 to your computer and use it in GitHub Desktop.
Save tdiprima/ec9f6967666205074afa8c8c31d02c50 to your computer and use it in GitHub Desktop.
DZI syntax to IIIF
# Take params from the DZI syntax and turn them into an IIIF request
#
# Copyright (C) 2009 CodePlex Foundation
# Copyright (C) 2010-2013 OpenSeadragon contributors
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# - Neither the name of CodePlex Foundation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import math
# would-be params from dzi syntax, i.e. `/level/x_y.jpg`
level = 9
x = 0
y = 0
# would-be values from iiif info service
img_tile_width = 256
img_tile_height = 256
img_width = 2717
img_height = 3600
# constants
IIIF_ROTATION = '0'
IIIF_QUALITY = 'native.jpg'
## get the scale (level as a decimal)
max_img_dimension = max(img_width, img_height)
max_level = int(math.ceil(math.log(max_img_dimension, 2)))
scale = math.pow(0.5, max_level - level)
## get iiif size
iiif_pct_size = scale * 100
iiif_size = str('pct:' + str(iiif_pct_size))
## iiif region
iiif_region = ''
iiif_tile_size_width = int(math.ceil(img_tile_width / scale))
iiif_tile_size_height = int(math.ceil(img_tile_height / scale))
# image dimensions at this level
level_width = int(math.ceil(img_width * scale))
level_height = int(math.ceil(img_height * scale))
if level_width < img_tile_width or level_height < img_tile_height:
iiif_region = 'full'
else:
iiif_tile_x = x * iiif_tile_size_width + x
iiif_tile_y = y * iiif_tile_size_height + y
iiif_tile_w = min(iiif_tile_size_width, img_width - iiif_tile_x)
iiif_tile_h = min(iiif_tile_size_height, img_height - iiif_tile_y)
bounds = (iiif_tile_x, iiif_tile_y, iiif_tile_w, iiif_tile_h)
iiif_region = ','.join(map(str, bounds))
print '/'.join((iiif_region, iiif_size, IIIF_ROTATION, IIIF_QUALITY))
import math
# DZI to IIIF 2.0
# constants
IIIF_ROTATION = '0'
IIIF_QUALITY = 'default.jpg'
def dzi_to_iiif(level, x_idx, y_idx, img_w, img_h, img_tile_w, image_tile_h):
## get the scale (level as a decimal)
long_dimension = max(img_w, img_h)
max_level = int(math.ceil(math.log(long_dimension, 2)))
print max_level
scale = math.pow(0.5, max_level - level)
## get iiif size
iiif_size = '%d,' % int(math.ceil(img_w * scale))
## iiif region
iiif_region = ''
iiif_tile_size_width = int(math.ceil(img_tile_w / scale))
iiif_tile_size_height = int(math.ceil(img_tile_h / scale))
# image dimensions at this level
level_width = int(math.ceil(img_w * scale))
level_height = int(math.ceil(img_h * scale))
if level_width < img_tile_w or level_height < img_tile_h:
iiif_region = 'full'
else:
iiif_tile_x = x_idx * iiif_tile_size_width + x_idx
iiif_tile_y = y_idx * iiif_tile_size_height + y_idx
iiif_tile_w = min(iiif_tile_size_width, img_w - iiif_tile_x)
iiif_tile_h = min(iiif_tile_size_height, img_h - iiif_tile_y)
bounds = (iiif_tile_x, iiif_tile_y, iiif_tile_w, iiif_tile_h)
iiif_region = ','.join(map(str, bounds))
return '/'.join((iiif_region, iiif_size, IIIF_ROTATION, IIIF_QUALITY))
# would-be params from dzi syntax, i.e. `/level/x_y.jpg`
level = 10
x = 0
y = 0
# would-be values from iiif info service
img_tile_w = 1024
img_tile_h = 1024
img_w = 5434
img_h = 7200
print dzi_to_iiif(level, x, y, img_w, img_h, img_tile_w, img_tile_h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment