Having trouble to extract image from WSI image of CZI format?

@sebi06
Hello there,
I have multiple WSI images files of the CZI extension and currently, I am trying to extract or visualize these images in python using python-bioformats library. The code which I am following can be seen below. I can visualize the same image in ImageJ but here in python, it shows a different kind of image.
I know these are not an RBG image as other WSI images have but rather it has a pattern of LSM images.

code:
reader = bioformats.ImageReader(input_path)
omeMeta = bioformats.metadatatools.createOMEXMLMetadata()
reader.rdr.setId(input_path)
print("Series Count", reader.rdr.getSeriesCount())
print("Image Count", reader.rdr.getImageCount())
print("Image Channel", reader.rdr.getSizeC())
metadata = javabridge.jdictionary_to_string_dictionary(reader.rdr.getMetadata())
# for channel in range(reader.rdr.getSizeC()):
#    channel += 0
for Series in range(reader.rdr.getSeriesCount()):  # czi
    reader.rdr.setSeries(Series)
    width = reader.rdr.getSizeX()
    height = reader.rdr.getSizeY()
    for zstack in range(reader.rdr.getImageCount()):
        bf_tiles_org = reader.read(z=zstack, rescale=False)
        # img_tiles_org = Image.fromarray(bf_tiles_org).convert('RGB')
        img_tiles_org = np.array(Image.fromarray((bf_tiles_org * 255).astype(np.uint8)))
        pylab.imshow(img_tiles_org)
        pylab.show()

Generated Output:
1
And when we visualize the same in ImageJ we get below image, it has four series:


I am having a problem visualizing this file in python.
Is there any help to fix it? :slightly_smiling_face:

Hi @dmc-8288

i have a few two idea here:

  • in ZEN we use BGR (or BGRA) to store such images, so you might need to convert this
  • I would recommend to aicspylibczi to such images

Hope it helps.

Could you upload an example image? Thanks!

Thank you for the reply.
I found the problem, I didn’t realize that it was a 16-bit image and after reading the image by bioformats I passed the array to Opencv-python library and it worked well to visualize the image.

1 Like

Hi @dmc-8288

cool. Can you post the working code snippet. Maybe you “save” somebody from some headaches :slight_smile:

1 Like

Here the code to read and visualize 16-bit generated WSI image with extension CZI.

Code:

reader = bioformats.ImageReader(input_path)
omeMeta = bioformats.metadatatools.createOMEXMLMetadata()
reader.rdr.setId(input_path)
print("Series Count", reader.rdr.getSeriesCount())
print("Image Count", reader.rdr.getImageCount())
print("Image Channel", reader.rdr.getSizeC())
metadata = javabridge.jdictionary_to_string_dictionary(reader.rdr.getMetadata())

for Series in range(reader.rdr.getSeriesCount()):  # czi
    reader.rdr.setSeries(Series)
    width = reader.rdr.getSizeX()
    height = reader.rdr.getSizeY()
    for zstack in range(reader.rdr.getImageCount()):
        bf_tiles_org = reader.read(z=zstack, rescale=False)
        # normalizing
        img_scaled = cv2.normalize(bf_tiles_org, dst=None, alpha=0, beta=65535, norm_type=cv2.NORM_MINMAX)
        # conversion to RGB
        img_tiles_org = cv2.cvtColor(img_scaled, cv2.COLOR_BGR2RGB)
        # visualization
        cv2.imshow('image', img_tiles_org)
        # add wait key. window waits till user press any key
        cv2.waitKey(30)
        # and finally destroy/Closing all open windows
        cv2.destroyAllWindows()

Happy coding :slightly_smiling_face:

1 Like