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)