Page 1 of 1

Hi-res bio-formats 2D images to DeepZoom (OpenSeadragon)

PostPosted: Mon Dec 05, 2016 4:15 pm
by darwinjob
Hi
For those who might be interested in the subject.
This is a really simple way to create online viewers for your hi-res images.
In addition to bio-formats library you will need PyramidIO:
https://github.com/usnistgov/pyramidio
and the implementation of PartialImageReader interface:
Code: Select all
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.NotImplementedException;

import loci.formats.FormatException;
import loci.formats.ImageReader;
import loci.formats.gui.BufferedImageReader;
import gov.nist.isg.pyramidio.PartialImageReader;

public class BioFormatsPartialImageReader implements PartialImageReader {

   private BufferedImageReader bufferedImageReader;
   private ImageReader imageReader;

   public BioFormatsPartialImageReader(ImageReader imageReader) {
      this.imageReader = imageReader;
      bufferedImageReader = new BufferedImageReader(imageReader);
   }

   public BufferedImage read() throws IOException {
      throw new NotImplementedException("XXX");

   }

   public BufferedImage read(Rectangle rectangle) throws IOException {
      try {
         return bufferedImageReader.openImage(0, rectangle.x, rectangle.y,
               rectangle.width, rectangle.height);
      } catch (FormatException e) {
         throw new IOException(e);
      }
   }

   public int getWidth() {
      return imageReader.getSizeX();
   }

   public int getHeight() {
      return imageReader.getSizeY();
   }

}


Then, to create the pyramid:
Code: Select all
public class App {
   public static void main(String[] args) {

      ImageReader imageReaderBF = new ImageReader();

      try {
          imageReaderBF.setId("path_to_you_file");
         
         ScalablePyramidBuilder spb = new ScalablePyramidBuilder(tileSize, tileOverlap, tileFormat, "dzi");
         FilesArchiver archiver = new DirectoryArchiver(new File("path_to_output_folder"));
         PartialImageReader pir = new BioFormatsPartialImageReader(imageReaderBF);

         spb.buildPyramid(pir, "pyramidName", archiver, 1, amout_of_image_to_cache);

      } catch (FormatException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }


The resulting pyramid can be used with OpenSeadragon library to create online viewer:
Code: Select all
<div id="openseadragon1" style="width: 800px; height: 600px;"></div>
<script src="openseadragon.min.js"></script>
<script type="text/javascript">
    var viewer = OpenSeadragon({
        id: "openseadragon1",
        prefixUrl: "images/",
        tileSources: "output2/pyramidName.dzi"
    });
</script>

Example (processed from 119780x102791 pixels, TIFF, JPEG-compressed ):
http://folk.uio.no/dmitrd/openseadragon/

Re: Hi-res bio-formats 2D images to DeepZoom (OpenSeadragon)

PostPosted: Tue Dec 13, 2016 1:35 pm
by hflynn
Hi,

Thanks very much for sharing this. Is it okay if we aim to reproduce this info as an example in one of our code repos on GitHub so it is easier for the wider community to find in the future? We will of course credit you,

Thanks,
Helen

Re: Hi-res bio-formats 2D images to DeepZoom (OpenSeadragon)

PostPosted: Tue Dec 13, 2016 4:08 pm
by darwinjob
Of course! Happy to help :)
Peace 8-)