Problem related to .tif and .scn while extracting images from them using python-bioformats

Hi,
I have downloaded some trestle .tif and .scn images from here (http://openslide.cs.cmu.edu/download/openslide-testdata/Trestle/).

When I load this image with an openslide I can see the series of pyramid structure as below:
levels dimensions: ((40000, 27712), (20000, 13856), (10000, 6928), (4992, 3456), (2496, 1728), (1248, 864), (624, 432))

But when load the same image with python-bioformats then it just shows or select first series 0 = (40000, 27712), not all.
I pass below code to do that:

# setup reader
reader = bioformats.ImageReader(input_any1)
omeMeta = bioformats.metadatatools.createOMEXMLMetadata()
reader.setMetadataStore(omeMeta)
reader.setId(input_any1)
metadata = javabridge.jdictionary_to_string_dictionary(reader.rdr.getMetadata())
get = reader.rdr.getSeriesCount(input_any1)
gett = reader.rdr.getImageCount(input_any1)

The output of get is 1, I believe it should be 7 as below shown by bftools.

**Anyway, I also tried to visualize the metadata of this image using bftools and it shows that image has 7 **
series as provided below:

Reading core metadata
filename = 111.tif
Series count = 7
Series #0 :
	Image count = 1
	RGB = true (3) 
	Interleaved = false
	Indexed = false (true color)
	Width = 40000
	Height = 27712
	SizeZ = 1
	SizeT = 1
	SizeC = 3 (effectively 1)
	Thumbnail size = 21632 x 15360
	Endianness = intel (little)
	Dimension order = XYCZT (uncertain)
	Pixel type = uint8
	Valid bits per pixel = 8
	Metadata complete = true
	Thumbnail series = false
	-----
	Plane #0 <=> Z 0, C 0, T 0

Series #1 :
	Image count = 1
	RGB = true (3) 
	Interleaved = false
	Indexed = false (true color)
	Width = 20000
	Height = 13856
	SizeZ = 1
	SizeT = 1
	SizeC = 3 (effectively 1)
	Thumbnail size = 21632 x 15360
	Endianness = intel (little)
	Dimension order = XYCZT (uncertain)
	Pixel type = uint8
	Valid bits per pixel = 8
	Metadata complete = true
	Thumbnail series = true
	-----
	Plane #0 <=> Z 0, C 0, T 0

Series #2 :
	Image count = 1
	RGB = true (3) 
	Interleaved = false
	Indexed = false (true color)
	Width = 10000
	Height = 6928
	SizeZ = 1
	SizeT = 1
	SizeC = 3 (effectively 1)
	Thumbnail size = 21632 x 15360
	Endianness = intel (little)
	Dimension order = XYCZT (uncertain)
	Pixel type = uint8
	Valid bits per pixel = 8
	Metadata complete = true
	Thumbnail series = true
	-----
	Plane #0 <=> Z 0, C 0, T 0

Series #3 :
	Image count = 1
	RGB = true (3) 
	Interleaved = false
	Indexed = false (true color)
	Width = 4992
	Height = 3456
	SizeZ = 1
	SizeT = 1
	SizeC = 3 (effectively 1)
	Thumbnail size = 21632 x 15360
	Endianness = intel (little)
	Dimension order = XYCZT (uncertain)
	Pixel type = uint8
	Valid bits per pixel = 8
	Metadata complete = true
	Thumbnail series = true
	-----
	Plane #0 <=> Z 0, C 0, T 0

Series #4 :
	Image count = 1
	RGB = true (3) 
	Interleaved = false
	Indexed = false (true color)
	Width = 2496
	Height = 1728
	SizeZ = 1
	SizeT = 1
	SizeC = 3 (effectively 1)
	Thumbnail size = 21632 x 15360
	Endianness = intel (little)
	Dimension order = XYCZT (uncertain)
	Pixel type = uint8
	Valid bits per pixel = 8
	Metadata complete = true
	Thumbnail series = true
	-----
	Plane #0 <=> Z 0, C 0, T 0

Series #5 :
	Image count = 1
	RGB = true (3) 
	Interleaved = false
	Indexed = false (true color)
	Width = 1248
	Height = 864
	SizeZ = 1
	SizeT = 1
	SizeC = 3 (effectively 1)
	Thumbnail size = 21632 x 15360
	Endianness = intel (little)
	Dimension order = XYCZT (uncertain)
	Pixel type = uint8
	Valid bits per pixel = 8
	Metadata complete = true
	Thumbnail series = true
	-----
	Plane #0 <=> Z 0, C 0, T 0

Series #6 :
	Image count = 1
	RGB = true (3) 
	Interleaved = false
	Indexed = false (true color)
	Width = 624
	Height = 432
	SizeZ = 1
	SizeT = 1
	SizeC = 3 (effectively 1)
	Thumbnail size = 21632 x 15360
	Endianness = intel (little)
	Dimension order = XYCZT (uncertain)
	Pixel type = uint8
	Valid bits per pixel = 8
	Metadata complete = true
	Thumbnail series = true
	-----
	Plane #0 <=> Z 0, C 0, T 0


Reading global metadata
Background Color: E6E6E6
BitsPerSample: 8
Compression: JPEG
Copyright: (c) 2005 Trestle Corp.
DateTime: 2010:01:04 14:41:53
HostComputer: TRESTLE-CP01
ImageLength: 33792
ImageWidth: 46592
JPEG Quality: 75
MetaDataPhotometricInterpretation: RGB
NumberOfChannels: 3
Objective Power: 10
Orientation: 1st row - top; 1st column - left
OverlapsXY: 64 64 32 32 16 16
PhotometricInterpretation: YCbCr
PlanarConfiguration: Chunky
ReferenceBlackWhite: 0
SamplesPerPixel: 3
Software: MedScan v3.4.2.1 - Release
TileByteCounts: 2503
TileLength: 352
TileOffsets: 8
TileWidth: 448
White Balance: C0AAA1
XPosition: 44595
XResolution: 0.574691891670227
YCbCrSubSampling: chroma image dimensions are half the luma image dimensions
YPosition: 23405
YResolution: 0.5750624537467957

And, I have another question too:
When I load SCN image with python-bioformats it shows me that, there are 27 series:
Checking file format [Leica SCN]

Initializing reader

LeicaSCNReader initializing /Users/yubraj/Desktop/urgent_wsi/input/Leica-2.scn

Reading IFDs

Populating metadata

Populating OME metadata

Initialization took 0.248s

Reading core metadata

filename = /Users/yubraj/Desktop/urgent_wsi/input/Leica-2.scn

Series count = 27

Series #0 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 1616

Height = 4668

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 44 x 128

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #1 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 404

Height = 1167

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 44 x 128

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #2 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 101

Height = 291

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 44 x 128

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = true

-----

Plane #0 <=> Z 0, C 0, T 0

Series #3 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 39168

Height = 26048

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #4 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 9792

Height = 6512

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #5 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 2448

Height = 1628

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #6 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 612

Height = 407

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #7 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 153

Height = 101

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #8 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 38

Height = 25

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = true

-----

Plane #0 <=> Z 0, C 0, T 0

Series #9 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 39360

Height = 23360

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #10 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 9840

Height = 5840

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #11 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 2460

Height = 1460

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #12 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 615

Height = 365

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #13 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 154

Height = 91

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #14 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 38

Height = 22

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = true

-----

Plane #0 <=> Z 0, C 0, T 0

Series #15 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 39360

Height = 23360

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #16 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 9840

Height = 5840

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #17 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 2460

Height = 1460

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #18 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 615

Height = 365

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #19 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 154

Height = 91

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #20 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 38

Height = 22

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 22

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = true

-----

Plane #0 <=> Z 0, C 0, T 0

Series #21 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 39168

Height = 26048

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #22 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 9792

Height = 6512

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #23 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 2448

Height = 1628

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #24 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 612

Height = 407

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #25 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 153

Height = 101

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = false

-----

Plane #0 <=> Z 0, C 0, T 0

Series #26 :

Image count = 1

RGB = true (3)

Interleaved = false

Indexed = false (true color)

Width = 38

Height = 25

SizeZ = 1

SizeT = 1

SizeC = 3 (effectively 1)

Thumbnail size = 38 x 25

Endianness = intel (little)

Dimension order = XYCZT (certain)

Pixel type = uint8

Valid bits per pixel = 8

Metadata complete = true

Thumbnail series = true

-----

Plane #0 <=> Z 0, C 0, T 0

Reading global metadata

BitsPerSample: 8

Compression: JPEG

ImageLength: 4668

ImageWidth: 1616

MetaDataPhotometricInterpretation: RGB

NumberOfChannels: 3

PhotometricInterpretation: YCbCr

PlanarConfiguration: Chunky

ReferenceBlackWhite: 0

ResolutionUnit: Centimeter

SamplesPerPixel: 3

TileLength: 512

TileWidth: 512

XResolution: 608.3301634770519

YCbCrSubSampling: chroma image dimensions are half the luma image dimensions

YResolution: 608.3301634770519

Reading series #0 metadata

collection.name: ImageCollection_0000002583

collection.uuid: urn:uuid:85036cce-a631-4ea1-9da8-c197977aae03

creationDate: 2014-11-26T14:16:39.753Z

device.model for image: Leica SCN400;Leica SCN

device.version for image: 1.5.1.10804 2012/05/10 13:29:07;1.5.1.10864

scanSettings.illuminationSettings.illuminationSource for image: brightfield

scanSettings.illuminationSettings.numericalAperture for image: 0.65

scanSettings.objectiveSettings.objective for image: 40

view.offsetX for image: 8971682

view.offsetY for image: 40843483

view.sizeX for image: 9792000

view.sizeY for image: 6512000

view.spacingZ for image: 400

But when I load the same image with openslide it just shows 6 series as follows:
level_dimensions: ((106259, 306939), (26565, 76735), (6642, 19184), (1661, 4796), (416, 1201), (104, 298))

What is going on here? I didn’t understand the concept of this image.
Can anyone please help me to clarify how to solve this problem?
Images can be downloaded from :

Any suggestion :slightly_smiling_face:

Hi @dmc-8288, apologies for the late reply to this as I was off over the Christmas break.

For the inconsistencies with the Tiff file, this is due to the way various tools handle pyramids. There will be 2 ways in which pyramids can be accessed, either with flattened resolutions, so each resolution appears as a separate series, or as a pyramid in which you will have a single series with multiple resolutions.

The command line tools by default will flatten the resolutions giving you a series count of 7. If you instead use ./showinf -noflat path/to/myFile.tif then it shows 1 series and 7 resolutions.

Series count = 1
Series #0 :
	Resolutions = 7
		sizeX[0] = 40000
		sizeX[1] = 20000
		sizeX[2] = 10000
		sizeX[3] = 4992
		sizeX[4] = 2496
		sizeX[5] = 1248
		sizeX[6] = 624

From Bio-Formats 6.0.0 onwards there are additional API methods that can be used to make pyramids easier to work with. These can be enabled by calling setFlattenedResolutions(false) prior to setId.

After setFlattenedResolutions(false) , each series represents an entire image pyramid and not just a single resolution. Calling setSeries(...) then skips over all other resolutions in the same pyramid, to either the next pyramid (if multiple pyramids are stored), or the thumbnail or barcode image (if present). To access the smaller resolutions in the pyramid, use the getResolutionCount() and setResolution(int) methods.

The SCN issue may be different, which of the scn files were you using for the test? Are you seeing the same issue if the file is in a folder on its own?

Hi @dgault,
Sorry for the late reply, I was busy on university exam.
First of all, thank you for your reply related to my problem.
I couldn’t find the functions which you have mentioned above like **setFlattenedResolutions(false), **
getResolutionCount(), setResolution() in a python-bioformat.

I understand your above post explanation but I didn’t understand how I will able to do that because I couldn’t find those functions which you have illustrated above to explain in python-bioformats like setFlattenedResolutions(false), getResolutionCount(), setResolution()

Is there any other way to set setFlattenedResolutions() to false so that image will behave as a single series with multiple resolutions or opposite of it in python-bioformats?

Currently, I am passing these lines of code:

reader = bioformats.ImageReader(input_any1)
omeMeta = bioformats.metadatatools.createOMEXMLMetadata()
reader.setMetadataStore(omeMeta)
reader.setId(input_any1)
metadata = javabridge.jdictionary_to_string_dictionary(reader.rdr.getMetadata())
get = reader.rdr.getSeriesCount(input_any1)
gett = reader.rdr.getImageCount(input_any1)
gettt = reader.rdr.getResolutionCount()
getttt = reader.rdr.setResolution(2)

And error:

Traceback (most recent call last):
  File "/snap/pycharm-community/224/plugins/python-ce/helpers/pydev/pydevd.py", line 1477, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-community/224/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/yuvi/PycharmProjects/task-1/temp/-image.py", line 60, in <module>
    gettt = reader.rdr.getResolutionCount()
AttributeError: 'IFormatReader' object has no attribute 'getResolutionCount'

And this the link for the Leice image which I was talking in my first post, Leica-2.scn
http://openslide.cs.cmu.edu/download/openslide-testdata/Leica/

For the missing methods in python it may be that you need to upgrade your python-bioformats to the latest 4.0.0 to pick up the pyramid API updates.

Hopefully that should resolve the resolution issues. I will retest the scn file now and get back to you this afternoon.

For the scn file, the 27 different series looks to be correct based on the internal metadata from the file which seems to have 5 images with either 3 or 6 resolutions each:

  <collection name="ImageCollection_0000002583" uuid="urn:uuid:85036cce-a631-4ea1-9da8-c197977aae03" sizeX="26564529" sizeY="76734666">
    <image name="mrw_0000040301" uuid="urn:uuid:9e28bed4-b366-47c6-b15d-079e3336ffa8">
      <creationDate>2014-11-26T14:09:07.39Z</creationDate>
      <device model="Leica SCN400;Leica SCN" version="1.5.1.10804 2012/05/10 13:29:07;1.5.1.10864" />
      <pixels sizeX="1616" sizeY="4668">
        <dimension sizeX="1616" sizeY="4668" r="0" ifd="0" />
        <dimension sizeX="404" sizeY="1167" r="1" ifd="1" />
        <dimension sizeX="101" sizeY="291" r="2" ifd="2" />
      </pixels>
      <view sizeX="26564529" sizeY="76734666" offsetX="0" offsetY="0" spacingZ="0" />
      <scanSettings>
        <objectiveSettings>
          <objective>0.60833</objective>
        </objectiveSettings>
        <illuminationSettings>
          <numericalAperture>0.7</numericalAperture>
          <illuminationSource>brightfield</illuminationSource>
        </illuminationSettings>
      </scanSettings>
    </image>
    <image name="mrw_0000040302" uuid="urn:uuid:3abcee7c-ed5f-4cd8-b957-6e003738bc1c">
      <creationDate>2014-11-26T14:12:11.25Z</creationDate>
      <device model="Leica SCN400;Leica SCN" version="1.5.1.10804 2012/05/10 13:29:07;1.5.1.10864" />
      <pixels sizeX="39168" sizeY="26048">
        <dimension sizeX="39168" sizeY="26048" r="0" ifd="3" />
        <dimension sizeX="9792" sizeY="6512" r="1" ifd="4" />
        <dimension sizeX="2448" sizeY="1628" r="2" ifd="5" />
        <dimension sizeX="612" sizeY="407" r="3" ifd="6" />
        <dimension sizeX="153" sizeY="101" r="4" ifd="7" />
        <dimension sizeX="38" sizeY="25" r="5" ifd="8" />
      </pixels>
      <view sizeX="9792000" sizeY="6512000" offsetX="10317168" offsetY="18642978" spacingZ="400" />
      <scanSettings>
        <objectiveSettings>
          <objective>40</objective>
        </objectiveSettings>
        <illuminationSettings>
          <numericalAperture>0.65</numericalAperture>
          <illuminationSource>brightfield</illuminationSource>
        </illuminationSettings>
      </scanSettings>
    </image>
    <image name="mrw_0000040303" uuid="urn:uuid:3b2e0657-61ae-40b8-a106-c467a741921f">
      <creationDate>2014-11-26T14:13:39.107Z</creationDate>
      <device model="Leica SCN400;Leica SCN" version="1.5.1.10804 2012/05/10 13:29:07;1.5.1.10864" />
      <pixels sizeX="39360" sizeY="23360">
        <dimension sizeX="39360" sizeY="23360" r="0" ifd="9" />
        <dimension sizeX="9840" sizeY="5840" r="1" ifd="10" />
        <dimension sizeX="2460" sizeY="1460" r="2" ifd="11" />
        <dimension sizeX="615" sizeY="365" r="3" ifd="12" />
        <dimension sizeX="154" sizeY="91" r="4" ifd="13" />
        <dimension sizeX="38" sizeY="22" r="5" ifd="14" />
      </pixels>
      <view sizeX="9840000" sizeY="5840000" offsetX="10370790" offsetY="12536174" spacingZ="400" />
      <scanSettings>
        <objectiveSettings>
          <objective>40</objective>
        </objectiveSettings>
        <illuminationSettings>
          <numericalAperture>0.65</numericalAperture>
          <illuminationSource>brightfield</illuminationSource>
        </illuminationSettings>
      </scanSettings>
    </image>
    <image name="mrw_0000040304" uuid="urn:uuid:7463b858-97fb-44d3-be5a-46f3563f354d">
      <creationDate>2014-11-26T14:15:06.387Z</creationDate>
      <device model="Leica SCN400;Leica SCN" version="1.5.1.10804 2012/05/10 13:29:07;1.5.1.10864" />
      <pixels sizeX="39360" sizeY="23360">
        <dimension sizeX="39360" sizeY="23360" r="0" ifd="15" />
        <dimension sizeX="9840" sizeY="5840" r="1" ifd="16" />
        <dimension sizeX="2460" sizeY="1460" r="2" ifd="17" />
        <dimension sizeX="615" sizeY="365" r="3" ifd="18" />
        <dimension sizeX="154" sizeY="91" r="4" ifd="19" />
        <dimension sizeX="38" sizeY="22" r="5" ifd="20" />
      </pixels>
      <view sizeX="9840000" sizeY="5840000" offsetX="9025305" offsetY="34633180" spacingZ="400" />
      <scanSettings>
        <objectiveSettings>
          <objective>40</objective>
        </objectiveSettings>
        <illuminationSettings>
          <numericalAperture>0.65</numericalAperture>
          <illuminationSource>brightfield</illuminationSource>
        </illuminationSettings>
      </scanSettings>
    </image>
    <image name="mrw_0000040305" uuid="urn:uuid:8c134c4d-50de-43ec-aa55-0dc86b848a85">
      <creationDate>2014-11-26T14:16:39.753Z</creationDate>
      <device model="Leica SCN400;Leica SCN" version="1.5.1.10804 2012/05/10 13:29:07;1.5.1.10864" />
      <pixels sizeX="39168" sizeY="26048">
        <dimension sizeX="39168" sizeY="26048" r="0" ifd="21" />
        <dimension sizeX="9792" sizeY="6512" r="1" ifd="22" />
        <dimension sizeX="2448" sizeY="1628" r="2" ifd="23" />
        <dimension sizeX="612" sizeY="407" r="3" ifd="24" />
        <dimension sizeX="153" sizeY="101" r="4" ifd="25" />
        <dimension sizeX="38" sizeY="25" r="5" ifd="26" />
      </pixels>
      <view sizeX="9792000" sizeY="6512000" offsetX="8971682" offsetY="40843483" spacingZ="400" />
      <scanSettings>
        <objectiveSettings>
          <objective>40</objective>
        </objectiveSettings>
        <illuminationSettings>
          <numericalAperture>0.65</numericalAperture>
          <illuminationSource>brightfield</illuminationSource>
        </illuminationSettings>
      </scanSettings>
    </image>
  </collection>

I uninstall the previous version and re-install the latest version of python-bioformats but the problem is still the same. I am curious, am I calling this setFlattenedResolutions(false) function in the right way or not.

ERROR:
Traceback (most recent call last):
File “/snap/pycharm-community/224/plugins/python-ce/helpers/pydev/pydevd.py”, line 1477, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File “/snap/pycharm-community/224/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+"\n", file, ‘exec’), glob, loc)
File “/home/yuvi/PycharmProjects/task-1/temp/WSD-BMD-bioformats-image.py”, line 56, in
reader.rdr.setFlattenedResolutions()
AttributeError: ‘IFormatReader’ object has no attribute 'setFlattenedResolutions’

It says IFORMATReader has no setFlattenedResolutions function.
Is it the right way to call it?
Any other way beside it to read tiff or tif files in bioformats. What if I convert these tiff or tif files into another format like OME.tiff, will I suffer from the same problem?

And for SCN, yes u r right it has a 27 series. First 3 are not so important and the remaining 4 is the repetition of each other.

The way the resolutions are handled would be the same for the converted files. The method would be setFlattenedResolutions(false) which should be called before setId, so for the previous example it would be:

reader = bioformats.ImageReader(input_any1)
omeMeta = bioformats.metadatatools.createOMEXMLMetadata()
reader.setMetadataStore(omeMeta)
reader.rdr.setFlattenedResolutions(false)
reader.setId(input_any1)
metadata = javabridge.jdictionary_to_string_dictionary(reader.rdr.getMetadata())
get = reader.rdr.getSeriesCount(input_any1)
gett = reader.rdr.getImageCount(input_any1)
gettt = reader.rdr.getResolutionCount()
getttt = reader.rdr.setResolution(2)

There i some further info on the pyramid handling at Working with whole slide images — Bio-Formats 6.6.0 documentation and some examples of using the Bio-Formats Java API at bio-formats-examples/SubResolutionExample.java at 436cfe7da62ff673754970256e2f21aaabfb1015 · ome/bio-formats-examples · GitHub (not exactly the same as through python but should give you an idea of the calls and order of events)

I tried everything which you have suggested above, but nothing work. I think there is no such function in the python version. And yesterday after updating python-bioformats to the latest one, now it says **"AttributeError: 'ImageReader' object has no attribute 'setId' "**

I don’t think python-bioformats ImageReader function has these function inside them: setFlattenedResolutions(false), getResolutionCount(), setResolution()
Any suggestion!

One alternative would be to use the javabridge as below to create the reader directly. I ran this as a quick test and it had the pyramidal functions and returned the correct values:

path ="myPath"
rdr = javabridge.JClassWrapper('loci.formats.in.LeicaSCNReader')()
rdr.setOriginalMetadataPopulated(True)
rdr.setFlattenedResolutions(False)
rdr.setId(path)
print("Series Count", rdr.getSeriesCount())
print("Image Count", rdr.getImageCount())
print("Res Count", rdr.getResolutionCount())
rdr.setResolution(2)
2 Likes

Hello there,
Thank you very much for your wonderful reply and awesome solution.
Now it seems like using the above-suggested method, I can read any image which is suggested over here loci.formats.in (Bio-Formats API)
This is what I was searching for a long time.
I hope the above piece of code will help anyone who likes to read WSI or any other images in python using the python-bioformats library.
Thank you :slightly_smiling_face:

Hi there,
Using your code, I was able to load the image but while trying to read tiles I am getting an error related to reading function I guess, rdr.read has something to do

Code:

rdr = javabridge.JClassWrapper('loci.formats.in.TrestleReader')()
rdr.setOriginalMetadataPopulated(True)
rdr.setFlattenedResolutions(True)
rdr.setId(input_any1)
rdr.getSeriesCount()
rdr.getImageCount()
print("Series Count", rdr.getSeriesCount())
print("Image Count", rdr.getImageCount())
print("Res Count", rdr.getResolutionCount())

for series in range(rdr.getSeriesCount()):
     for zstack in range(rdr.getImageCount()):
          # Read and write tiles
          bf_tiles_org = rdr.read(zstack, rescale=False)

Error:

Series Count 7
Image Count 1
Res Count 1

Traceback (most recent call last):
  File "/snap/pycharm-community/224/plugins/python-ce/helpers/pydev/pydevd.py", line 1477, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-community/224/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/yuvi/PycharmProjects/task-1/temp/WSI-BMD-rough_temp.py", line 147, in <module>
    tile_SizeX, tile_SizeY, instance_cnt, output_save_per_stack, z_stack)
  File "/home/yuvi/PycharmProjects/task-1/temp/tile_org.py", line 38, in tile_size_org
    bf_tiles_org = rdr.read(zstack, rescale=False,
  File "/home/yuvi/anaconda3/envs/task-1/lib/python3.7/site-packages/javabridge/wrappers.py", line 93, in __getattr__
    raise AttributeError()
AttributeError
python-BaseException

I don’t know how to solve this problem.
I just want to read the tiles image.
Any help :slightly_smiling_face:

As we have used the javabridge to create the reader directly rather than go through the python-bioformats wrapper then we will have to recreate some of the code that they use to access openByes. Below is an example of how this might look to read a plane. Longer term it would be good for all users to have the resolution API methods added to python-bioformats in https://github.com/CellProfiler/python-bioformats/blob/05a053b2739f31a1876c0ce5980f9198b7c86c81/bioformats/formatreader.py:

FormatTools = bioformats.formatreader.make_format_tools_class()
if pixel_type == FormatTools.INT8:
  dtype = np.int8
  scale = 255
elif pixel_type == FormatTools.UINT8:
  dtype = np.uint8
  scale = 255
elif pixel_type == FormatTools.UINT16:
  dtype = '<u2' if little_endian else '>u2'
  scale = 65535
elif pixel_type == FormatTools.INT16:
  dtype = '<i2' if little_endian else '>i2'
  scale = 65535
elif pixel_type == FormatTools.UINT32:
  dtype = '<u4' if little_endian else '>u4'
  scale = 2**32
elif pixel_type == FormatTools.INT32:
  dtype = '<i4' if little_endian else '>i4'
  scale = 2**32-1
elif pixel_type == FormatTools.FLOAT:
  dtype = '<f4' if little_endian else '>f4'
  scale = 1
elif pixel_type == FormatTools.DOUBLE:
  dtype = '<f8' if little_endian else '>f8'
  scale = 1

openBytes = javabridge.make_method('openBytes','(IIIII)[B',
                                          '''Get the specified image plane as a byte array
                                          (corresponds to openBytes(int no, int x, int y, int w, int h))
                                          no - image plane number
                                          x,y - offset into image
                                          w,h - dimensions of image to return''')
image = np.frombuffer(openBytes(rdr, 0, 0, 0, sizeX, sizeY), dtype)

Hi there,
I try your above suggestion to read the image and it read the image in tiles but the image which is read is in grayscale although the original image is a colour image. Do you know why it didn’t show the colour image?

Code:

bf_tiles_org = rdr.openBytes(zstack, tileX_org, tileY_org, effTileSizeX_org, effTileSizeY_org)
B = bf_tiles_org.reshape(effTileSizeY_org, effTileSizeX_org, 3)
plt.imshow(B)
plt.show()

Output:

bf_tiles_org = {ndarray: (786432,)} [227 221 217 215 218 222 224 224 226 225 225 224 224 224 226 225 225 217, 215 219 220 217 216 220 216 219 222 222 219 218 221 223 221 218 216 217, 221 222 220 216 220 224 227 226 222 220 222 225 223 221 219 219 220 220, 219 218 215 219 222 221 219 217 218 220 222 221 220 219 218 219 220 221, 218 223 223 218 215 218 221 222 218 216 216 220 227 229 225 219 224 219, 215 216 222 226 225 224 227 228 226 226]
B = {ndarray: (512, 512, 3)} [[[227 221 217],  [215 218 222],  [224 224 226],  ...,  [216 222 221],  [220 221 222],  [221 219 218]],, [[223 224 223],  [220 217 218],  [224 229 226],  ...,  [222 223 222],  [221 222 223],  [223 221 219]],, [[218 220 223],  [226 228 226],  [222 219 225],  ...,  [216 222 211],  [206 214 226],  [229 225 220]],, ...,, [[222 223 224],  [226 227 229],  [229 230 227],  ...,  [211 219 223],  [224 219 216],  [217 219 219]],, [[232 231 230],  [229 229 230],  [231 232 228],  ...,  [223 227 226],  [225 224 223],  [221 220 219]],, [[228 228 227],  [227 227 226],  [226 226 236],  ...,  [219 219 220],  [222 223 223],  [221 219 217]]]
effTileSizeX_org = {int} 512
effTileSizeY_org = {int} 512

As you can see above in (B) it’s an RBG image, but the cropped image came to gray why is that?
And also openBytes not cropping the image in a right away as the example below.
Figure_1

Can you tell me where I :slightly_smiling_face: am doing wrong?

Instead if you try with the following it should display correct. The order of the of the reshape params will depend on if the RGB image is interleaved or not:

B = bf_tiles_org.reshape(3, effTileSizeY_org, effTileSizeX_org)
B = B.transpose(1, 2, 0)
pylab.imshow(B)
pylab.show()

Thank you for your suggestion :slightly_smiling_face:
I manage to load trestle .tif images and I was also able to view its series of count and tiles utilizing openBytes command. But when I load other images which have a format of .tif, .tiff and .jpg (i don’t know it’s vendor) using what you have suggested above (Java bridge.JClassWrapper()) and as well as using simple bioformats.ImageReader() commands, it just shows one series of the count (I don’t know what should I pass now to view it’s all series).

Secondly, I also tried to convert these image into a pyramid if they are not already into pyramid structure using the below code:
Code:

im = pyvips.Image.new_from_file(input_path)

# openslide will add an alpha
if im.hasalpha():
    im = im[:-1]

image_height = im.height
image_bands = im.bands

# reorganise as a toilet-roll image
im = pyvips.Image.arrayjoin(im.bandsplit(), across=1)

# set minimal OME metadata
# before we can modify an image (set metadata in this case), we must take a
# private copy
im = im.copy()
im.set_type(pyvips.GValue.gint_type, "page-height", image_height)
im.set_type(pyvips.GValue.gstr_type, "image-description",
f"""<?xml version="1.0" encoding="UTF-8"?>
<OME xmlns="http://www.openmicroscopy.org/Schemas/OME/2016-06"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2016-06 http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd">
    <Image ID="Image:0">
        <!-- Minimum required fields about image dimensions -->
        <Pixels DimensionOrder="XYCZT"
                ID="Pixels:0"
                SizeC="{image_bands}"
                SizeT="1"
                SizeX="{im.width}"
                SizeY="{image_height}"
                SizeZ="1"
                Type="uint8">
        </Pixels>
    </Image>
</OME>""")
# im.tiffsave("x1.tif", compression="jpeg", tile=True,
#              tile_width=512, tile_height=512,
#              pyramid=True, subifd=True)
im.tiffsave("x11.ome.tiff", compression="jpeg", tile=True,
            tile_width=512, tile_height=512,
            pyramid=True, subifd=True)

It worked but the generated output came into grayscale having 3 separate channel for each series (You can view the generated output on below link: x1.tif, x11.ome.tiff, x.ome-gray.tiff).
Here are the input and generated output images.
https://uapt33090-my.sharepoint.com/:f:/g/personal/ygupta_ua_pt/ElUrNh8e4CxKkTvEho80VPIB7ZusVNA7eG--XUewZ6NI-A?e=g7BWI5

CMU-1.tiff, the earthworm. tiff, Rectum.tif, and 2.jpg are the input and other remaining are the generated images.

Question:

  1. Now I have tried both ways to view its series of count but still, it didn’t show all series (It just show the first series)? Why is that?
  2. Do you have any suggestion for conversion of these input images into a pyramid structure without splitting the colour images into 3 channels. I mean, if I pass colour image then it should produce RBG pyramid structure not a 3 separate channel for each series.

Thank you!

I dont think that method is going to work here, the output tiff is being read as a standard tiff image and the inserted XML is not correct. To convert the images to pyramidal OME-TIFF you would need to use the Bio-Formats writer API to do so. A Java example of how to convert an image while generating pyramids can be found at bio-formats-examples/GeneratePyramidResolutions.java at 436cfe7da62ff673754970256e2f21aaabfb1015 · ome/bio-formats-examples · GitHub. So you would be looking to replicate this in python similar to reading steps you have gone through so far.

If it is in any possible though I would recommend looking at use the existing command line tools for the conversion though. Its not in python but would certainly simplify the conversion and generation steps (Bio-Formats Downloads | Open Microscopy Environment (OME) and docs at Converting a file to different format — Bio-Formats 6.6.0 documentation)

Hi,
I try to implement that code into python but I am getting some error related to the constructor.

Code:

import javabridge
import bioformats
import math
import bioformats.formatreader as R
import bioformats.formatwriter as W
import bioformats.metadatatools
from bioformats import metadatatools

file = "/home/Rectum_de_chat_HE.tif"
out = "/home/Rectum_de_chat_HE.ome.tif"

javabridge.start_vm(class_path=bioformats.JARS, max_heap_size="50G")
# Scale and Resolution
scale = 1
resolutions = 2
# Function call
Pyramid = javabridge.JClassWrapper('loci.formats.ome.OMEPyramidStore')()
factory = javabridge.JClassWrapper('loci.common.services.ServiceFactory')()
OMEXMLService = javabridge.JClassWrapper('loci.formats.services.OMEXMLService')
rdr = javabridge.JClassWrapper('loci.formats.FormatTools')
OMEX = javabridge.JClassWrapper('ome.xml.model.enums.DimensionOrder')
OMEY = javabridge.JClassWrapper('ome.xml.model.enums.PixelType')
OMEZ = javabridge.JClassWrapper('ome.xml.model.primitives.PositiveInteger')
scaler = javabridge.JClassWrapper('loci.common.image.SimpleImageScaler')
# Image Reader
ImageReader = R.make_image_reader_class()
reader = ImageReader()
service = factory.methods.get(OMEXMLService.__class__)
omeMeta = bioformats.metadatatools.createOMEXMLMetadata()
reader.setMetadataStore(omeMeta)
reader.setId(file)

# Image Writer
ImageWriter = W.make_image_writer_class()
writer = ImageWriter()
writer.setMetadataRetrieve(omeMeta)
writer.setId(out)

for i in range(1, resolutions):
    divScale = math.pow(scale, i)
    Pyramid.setResolutionSizeX(OMEZ(reader.getSizeX() / divScale), 0, i)
    Pyramid.setResolutionSizeY(OMEZ(reader.getSizeY() / divScale), 0, i)
    i += 1

img = reader.openBytesXYWH(0, 40000, 10000, 512, 512)
type = reader.getPixelType()
writer.saveBytes(0, img)

for v in range(1, resolutions):
    writer.setResolution(v)
    x = omeMeta.getResolutionSizeX(0, v).getValue()
    y = omeMeta.getResolutionSizeY(0, v).getValue()
    downsample = scaler.downsample(img, reader.getSizeX(), reader.getSizeY(), math.pow(scale, v),
                                   rdr.FormatTools.getBytesPerPixel(type), reader.isLittleEndian(),
                                   rdr.FormatTools.isFloatingPoint(type), reader.getRGBChannelCount(),
                                   reader.isInterleaved())
    writer.saveBytes(0, downsample)

Error:

Traceback (most recent call last):
  File "/home/yuvi/PycharmProjects/task-1/temp/TEMP-BF.py", line 47, in <module>
    Pyramid.setResolutionSizeX(OMEZ(reader.getSizeX() / divScale), 0, i)
  File "/home/yuvi/anaconda3/envs/task-1/lib/python3.7/site-packages/javabridge/wrappers.py", line 361, in __call__
    raise TypeError("No matching constructor found")
TypeError: No matching constructor found

I can run bfconvert function in the command line to convert images into ome.tiff but I want to perform this conversion in python.
How can I solve the above issue?
:slightly_smiling_face:

I think that issue is caused by trying to create a PositiveInteger with OMEZ(reader.getSizeX() / divScale). Instead this already exists in python-bioformats so you can reuse the following
bioformats.metadatatools.PositiveInteger(int(reader.getSizeX()/divScale))

Also in the above example it is worth pointing out that you will want to use the OMEPyramidStore created with the reader to initially populate it, so reader.setMetadataStore(omeMeta) should become reader.setMetadataStore(OMEPyramidStore)