Automation

How to automate the process

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the OpenSeadragon Deep Dive course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to OpenSeadragon Deep Dive, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course OpenSeadragon Deep Dive
  • [00:00 - 00:11] In the previous lessons, you created a DZI using VIPS on the command line and loaded it into your viewer running on the local server. If you only have a few files to convert, the command line is just fine.

    [00:12 - 00:25] But if you have dozens, or if you're regularly going to be getting new images, based on user submission for instance, you're definitely going to want to automate the process. In this lesson, we'll write a Node.js script to convert your images using VIPS.

    [00:26 - 00:43] If you don't already have Node installed, now is the time to go to nodejs.org and install it. There is an npm package for VIPS, but I generally just use Node's child_process .exec, since that allows me to do everything I would normally do on the command line without having to look up additional documentation.

    [00:44 - 00:54] So, using our previous example that we did on the command line, you can do the exact same thing via Node, like so. From there, it's relatively simple to go through a list of files and convert them all.

    [00:55 - 01:02] Here are a couple more large images from Wikimedia Commons. Download those and stick them in the same folder as the girl with a pearl ear ring.

    [01:03 - 01:09] We now have three large images, put their names without the JPEG extension in an array. Then we can loop through that array.

    [01:10 - 01:20] Instead of hard coding the command, we'll use a template string to substitute the filename in. Note that we're adding the JPEG back to the input filename, but not for the output, since those are going to be DZI files.

    [01:21 - 01:27] Go ahead and run it, like so. It should process for a bit and then spit out the three DZI files along with their tilesets.

    [01:28 - 01:41] One thing to note about what we just did is that all the conversions are happening more or less simultaneously. This is because the exact call is asynchronous, so as we loop through the array , we are starting each call immediately, but then they take a while to complete.

    [01:42 - 01:48] If you want to visualize this, you can add additional logging, like so. If you run that, you should see something like this.

    [01:49 - 02:00] In this case, that's fine, but in production, you'll likely want to have more control over who gets converted and when. For that, I prefer running in serial, where we don't start the next one until the previous one is done.

    [02:01 - 02:14] This can be accomplished using async await. First, we'll use Node's promisify utility to make exact return a promise, then we'll wrap our code in an async function, turn our forEach into a for loop, and call exec using await.

    [02:15 - 02:21] Now you should see this when you run it. You've created a Node.js script that converts whatever image you give it into DZI.

    [02:22 - 02:28] In the next lesson, we'll further automate the process from downloading the images, to gathering information about them, and organizing the results.