Skip to content

Instantly share code, notes, and snippets.

@PolCPP
Created May 22, 2020 01:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PolCPP/9f4387093d510e57274dd7c083b77d62 to your computer and use it in GitHub Desktop.
Save PolCPP/9f4387093d510e57274dd7c083b77d62 to your computer and use it in GitHub Desktop.
Quick&Dirty script to transform DZI to EDZ. Should only need py2 bash & command line zip. To run just: python dzi2edz.py duomo duomo/duomo.dzi where duomo is your dzi folder and duomo/duomo.dzi is the xml file
#!/usr/bin/env python
import sys
import json
import os
from zipfile import ZipFile
from xml.dom import minidom
dir_name = sys.argv[1]
fileHeader = 59 + len(dir_name)
zip_file = dir_name + ".zip"
dzi_file = minidom.parse(sys.argv[2])
image_tag = dzi_file.getElementsByTagName('Image')[0]
size_tag = dzi_file.getElementsByTagName('Size')[0]
file_table = {
"Image": {
"TileFormat": "edz",
"Format": image_tag.attributes['Format'].value,
"Overlap": image_tag.attributes['Overlap'].value,
"TileSize": image_tag.attributes['TileSize'].value,
"Size": {
"Height": size_tag.attributes['Height'].value,
"Width": size_tag.attributes['Width'].value
},
"ZipFile": zip_file,
"Ranges": {}
}
}
os.system("zip -Z store -r %s %s" % (zip_file, dir_name))
f = open(zip_file, "r")
file_contents = f.read()
f.close()
with ZipFile(zip_file, 'r') as zip:
files = zip.infolist()
for file in files:
if not file.filename.endswith('/'):
#Slow safe way
#data = zip.read(file)
#offset = file_contents.find(data)
#size = len(data)
#Fast/Magic i don't get how i made it work way
offset = file.header_offset + fileHeader + len(file.filename.replace(dir_name + "/", ""));
size = file.file_size;
file_table["Image"]["Ranges"][file.filename] = {
"offset": offset,
"size": size
}
f = open(dir_name + ".json", "w")
f.write(json.dumps(file_table))
f.close()
# 6285
# 6350
#python -c 'f=open("duomo.zip","rb");f.seek(6315);print(f.read(196));f.close()'
@jbhanks
Copy link

jbhanks commented Jun 23, 2023

When I first tried to run this, I got a decoding error, but I was able to run it after changing line 33 to f = open(zip_file, "rb") . However Although the json and zip files are created, I am unable to load the edz ("Unable to load TileSource").

The js console gives me this:

No TileSource was able to open http://127.0.0.1:5002/static/dzi/testimage_files.json [object Object] tilesource.js:983:14
    determineType tilesource.js:983
    t tilesource.js:464
    success tilesource.js:521
    onreadystatechange openseadragon.js:2412
    (Async: EventHandlerNonNull)
    makeAjaxRequest openseadragon.js:2401
    getImageInfo tilesource.js:514
    TileSource tilesource.js:190
    addTiledImage viewer.js:2546
    (Async: setTimeout handler)
    addTiledImage viewer.js:2543
    addTiledImage viewer.js:1656
    open viewer.js:709
    open viewer.js:714
    open viewer.js:577
    Viewer viewer.js:483
    OpenSeadragon openseadragon.js:805
    <anonymous> test:25

Should I be pointing to the json file in lieu of the dzi file?

This is how I have done it:

        <script type="text/javascript">
            var viewer = OpenSeadragon({
                id:            "openseadragon1",
                prefixUrl:     "/openseadragon/images/",
                sequenceMode:  true,
                tileFormat: 'jpg',
                tileSources:   [
                    "http://127.0.0.1:5002/static/dzi/testimage_files.json",
                    "http://127.0.0.1:5002/static/dzi/testimage.dzi",
                    "https://libimages1.princeton.edu/loris/pudl0001%2F4609321%2Fs42%2F00000001.jp2/info.json"
                ]
            });

The first one gives the TileSource error, the rest (including the original dzi) load fine.

@jbhanks
Copy link

jbhanks commented Jun 23, 2023

Some progress: I just cloned this whole repo and got past the above errors, so I guess there must have been breaking changes since the OSD version this was based off of. It wasn't finding the tiles and I saw the paths were wrong, so I corrected the paths in the json file to remove the paths from my computer and just have the paths relative to the json file.

Now I am getting errors like Tile 10/1_1 failed to load: http://127.0.0.1:5002/static/dzi/onion_tonecurves_files.zip - error: Image load aborted .

@jbhanks
Copy link

jbhanks commented Jun 24, 2023

It seems like it's failing the check of whether the file is an image (line 1543 in tiledimage.js) :

function onTileLoad( tiledImage, tile, time, image, errorMsg, tileRequest ) {
    if ( !image ) {
        $.console.log( "Tile %s failed to load: %s - error: %s", tile, tile.url, errorMsg );
        /**
         * Triggered when a tile fails to load.
         *
         * @event tile-load-failed
         * @memberof OpenSeadragon.Viewer
         * @type {object}
         * @property {OpenSeadragon.Tile} tile - The tile that failed to load.
         * @property {OpenSeadragon.TiledImage} tiledImage - The tiled image the tile belongs to.
         * @property {number} time - The time in milliseconds when the tile load began.
         * @property {string} message - The error message.
         * @property {XMLHttpRequest} tileRequest - The XMLHttpRequest used to load the tile if available.
         */
        tiledImage.viewer.raiseEvent("tile-load-failed", {
            tile: tile,
            tiledImage: tiledImage,
            time: time,
            message: errorMsg,
            tileRequest: tileRequest
        });
        tile.loading = false;
        tile.exists = false;
        return;
    }

@PolCPP
Copy link
Author

PolCPP commented Jun 24, 2023

@jbhanks I don't remember having to add that. But i'm using OSD 4.0.1

Here it is a production edz file and the code i'm actually using on production (well i turned it into a cli lib, the one in production is called from Laravel). https://ion.red/edz.zip as long as an example i ran with some random anime image.

You'll need vips installed, zip and php..

and to run it

php edzgen.php test.jpg ./ cont 1234

As for a working production example url how i can DM you one?

@jbhanks
Copy link

jbhanks commented Jun 24, 2023

Ok, with your demo zip I get the same result Unable to open [object Object]: Unable to load TileSource . I just followed you, so you should be able to DM me now. Thank you so much for your help!

2 other questions:
What do you put for the tileFormat? I have tried jpeg, edz and zip.
Is php only needed for the generation script you included in the zip, or is it needed for the actual viewing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment