This blog has moved.

The content here has been left available for historical purposes, and should be considered out of date. For the most part, comments have been closed. If you have questions, feel free to contact me at justin.henry(at)uvm.edu. Any new material can be found at:
http://greengaloshes.cc.

Thanks for visiting!


Batch converting for Zoomify with ZoomifyImage

Zoomify is a pretty slick application that allows you to serve “fast, high-res images in flash”. These images can be zoomed in upon, and if you have the “Enterprise” version as we do, the images can be “annotated” with circles, arrows, labels, and more.

Recently I noticed that Wes, in a fantastic example of synergy in action, got Python upgraded on zoo so that it’s now possible to convert images into the necessary format from within a UNIX environment. This opens up some options that we hadn’t had before, such as being able to process images uploaded via a web interface, and provide web initiated batch processing of images.

Today we’ll look at how to get images converted to the “Zoomify” format, using a free, open source Python script. In short, we’ll be covering:

  • Installing the ZoomifyImage package
  • Using a PHP “wrapper” with ZoomifyImage to convert a directory of images
  • Using a PHP script to easily inspect the processed images

Installing the Python scripts and the PIL module

First, download the Python PIL imaging library. Upload it into the account you will be installing it to.

Install PIL. I did something like this, after uploading the file that I downloaded from the above site:

$ tar -xvf Imaging-1.1.5.tar
$ cd Imaging-1.1.5
$ python setup.py install --home=/path/to/desired/location

Note the use of the –home switch, as I don’t have root, and so am installing this module in my home directory. This means that the last part would look something like --home=/users/j/h/jhenry

Get the ZoomifyImage pagckage.

Upload ZoomifyBase.py and ZoomifyFileProcessor.py to a location nearby to where you’ll be processing your images. Edit ZoomifyFileProcessor to allow it to find the PIL library. I did this by adding this line just before the “import PIL.Image” statement.

sys.path.append("/path/to/installed/location/lib/python")

Note that “/path/to/installed/location” should be the same as what I specified with the “–home” switch above. So if I was installing it to my home directory, this might look more like “/users/j/h/jhenry/lib/python”. This tells python to look in the correct directory at runtime for any libraries it might need.

You should now be able to process an image with something like this. I’ve put an image into a directory called upload, which is in the same directory as the ZoomifyFileProcessor.py script.

$ python ZoomifyFileProcessor.py upload/imagename.tif

Converting a directory of images

Soon after I had completed the above, I whipped up a PHP “wrapper” for the python script so that I didn’t have to process images one at a time. Since I’m not all that familiar with Python, and the majority of the applications in which I’ll be using zoomify are built in PHP, this seemed like the way to go. All the wrapper does is look through a specified directory, and execute the ZoomifyFileProcessor.py command on each file that it finds there.

All this wrapper really does is loop through the directory and run the converter on any file that is not a directory, checking to see first if the image hasn’t been already converted. I’ve put it together as a class, so it should be relatively easy to adapt to other applications. Since we’re running in safe mode, it’s easier to run it from the command line, rather than calling it inside of a web process. I’m pretty sure there’s a way around this, but I haven’t had a chance to do further testing. So, I put my zoomify.php class file (which will also be used to check our results, below) and the zoomifyImageWrapper.php file that calls the appropriate function from the zoomify class into the same directory as the zoomifyFileProcessor.py and ZoomifyBase.py files which I previously had uploaded when installing the converter. I’ve made sure to edit the path to my image directory in zoomifyWrapper.php:

$imagepath = "path/to/image/folder/";

Now, assuming I’ve uploaded my images to be converted into this directory, I should be able to run my converter from prompt as such:

$ php zoomifyImageWrapper.php

If you see something like this in the output, you should be on the right track:

zoo> php zoomifyImageWrapper.php 
Content-type: text/html
X-Powered-By: PHP/4.3.8

Processing all files in images/ ...
Processing images/DSCN0680.JPG...
Processing images/DSCN0681.JPG...
Processing images/DSCN0682.JPG...
Processing images/DSCN0683.JPG...

Inspecting a directory of converted images

Now we want to be able to see our results. To do this, I have an index.php file, and the ZoomifyDynamicViewer.php file in the same directory as the zoomify.php class file. I also have the zoomifyDynamicViewer.swf file that came with the Zoomify package. In index.php I set my $imagepath variable to whatever I set it to in zoomifyImageWrapper.php above.

Now when I load up the index.php file in a web browser, I should see a list of links which take me to zoomified versions of my images.

Wrapping Up

After all that, my directory structure looks something like this. You can view the example index file here, and download and view the PHP files as an archived file here.

ZoomifyBase.py
ZoomifyFileProcessor.py
images/
index.php
zoomify.php
zoomifyDynamicViewer.php
zoomifyDynamicViewer.swf
zoomifyImageWrapper.php

For Reference

Some links referenced above:

8 Responses to “Batch converting for Zoomify with ZoomifyImage”

  1. Rob says:

    That’s really pretty slick. This would be good to integrate with an Ajax tiled image browser too, as you wouldn’t need flash then. I would like to give it a whirl on some of the stuff I’m doing for Animal Science if you don’t mind me badgering you over the wall :-D

    Nice work.

  2. Hi,
    is there any reason this should’t work in Windows ? I’ve installed Python24, got the PIL, edited the php files (running php and apache locally), run the converter, and it says it’s converting the images (which it lists correctly), but when I go to see the results, there’s nothing there…. Is there a debug command, perhaps ?

    Thanks a lot

    Ciaran

  3. justin says:

    Are you able to process individual images (i.e. when you run the “python ZoomifyFileProcessor.py upload/imagename.tif” command, does it produce a result)?

    As for debugging, I’m afraid the wrapper scripts I wrote aren’t quite that complete (or elegant, for that matter :) ). In lieu of such a command, you could try changing your error reporting level in php, as well as removing the “@” symbols from before the function calls in the code. The wrapper also executes a “chmod” command on the resulting directories (this is to fix a unix permissions issue) - you could try commenting out that line, as this might be something windows doesn’t expect.

  4. Hi,
    sorted that out. It’s now running sweetly on my laptop. There was no issue (under XP) with the chmod commands, incidentally.

    I’ve now ported it to my production linux environment, where it runs fine for the first image. But when I point it at the next image, it bombs out with [Errno 13] Permission denied removing ./pix/aaaaxxcc/TileGroup0/5-1-4.jpg
    where I have,of course, a zoomified file called aaaxxcc.jpg in the ./pix/ directory.

    Why would it want to *remove * files in a totally different directory when building a new zoomified image ?

    Thanks again!

    Ciaran

  5. Here’s a new one. Some TIF files work fine, others don’t. The problem files create a picure directory, and a Tileset0 directory within it. Only one tile is created, and this is placed in the top level directory (where the overall structure is toplevel-picture-Tileset0). No XML file is created.

    The image wrapper code doesn’t indicate a message - says it has processed the TIF file as usual, though you can it has had problems by the fact that it takes only moments.

    The exact same problem appears with some (but again not all) PNG files. So far (touch wood), I’ve had no problem with JPEGs.

    Any ideas ?

    thanks!

    Ciaran

  6. justin says:

    Have you tried running the converter directly on the image, independent of the wrapper code? That might give a better idea of where the problem is occurring. You might also try running in verbose mode - I believe both php and python have a “verbose” switch that you can activate from the command line, usually by specifying a -v or -V flag. This would cause errors and such to show up in the output.

  7. adam smith says:

    I enjoyed reading your post and am glad to see ZoomifyImage being used under PHP. I am in the process of updating the software and documentation (including a bug fix that Ciaran found) and wanted to know if it was OK to link to your post in my documentation? I am also adding a folder to the software for another user contribution and could add your PHP code also, giving you full credit of course, if you would like.

    Thanks for considering this,
    adam

  8. [...] Batch converting for Zoomify with ZoomifyImage by Justin Henry [...]