Last active
March 17, 2022 09:02
-
-
Save bdunnette/f94210460afd0514bd06ce9ab2c46bf6 to your computer and use it in GitHub Desktop.
Example of extracting a tile from a BioFormats-readable file (e.g. Zeiss CZI) using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires both javabridge and python-bioformats: | |
# pip install python-bioformats | |
# On e.g. Ubuntu you may need to add JAVA_HOME to /etc/profile: | |
# JAVA_HOME=/usr/lib/jvm/default-java | |
# PATH=$PATH:$HOME/bin:$JAVA_HOME/bin | |
# export JAVA_HOME | |
# export JRE_HOME | |
# export PATH | |
import sys | |
import os | |
from PIL import Image | |
import javabridge as jv | |
import bioformats as bf | |
jv.start_vm(class_path=bf.JARS) | |
path = sys.argv[1] | |
# print(dir(bf)) | |
TILE_SIZE = 256 | |
with bf.ImageReader(path, perform_init=True) as reader: | |
rdr = reader.rdr | |
# data = reader.rdr.openBytes(0, 0, 0, 256, 256) | |
image_x = rdr.getSizeX()/2 | |
image_y = rdr.getSizeY()/2 | |
image_data = reader.rdr.openBytesXYWH( | |
0, image_x, image_y, TILE_SIZE, TILE_SIZE) | |
# print(bytearray(image_data)) | |
i = Image.frombytes('RGB', (TILE_SIZE, TILE_SIZE), image_data) | |
filename = '_'.join([os.path.splitext(path)[0], str(image_x), | |
str(image_y), str(TILE_SIZE), str(TILE_SIZE)]) + '.png' | |
i.save(filename, 'PNG') | |
jv.kill_vm() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment