Created
September 28, 2021 21:06
-
-
Save ctrueden/6032196e10257f509bc85de42987d49c to your computer and use it in GitHub Desktop.
Generate OME-TIFF pyramids using Python
This file contains 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
#!/usr/env/bin python | |
# Generate pyramidal OME-TIFF. | |
# Adapted from: | |
# https://github.com/ome/bio-formats-examples/blob/88d3f109093fb3350242061b00624af001f495e8/src/main/java/GeneratePyramidResolutions.java#L39-L43 | |
import sys | |
import scyjava | |
scyjava.config.endpoints.append('ome:formats-bsd:6.7.0') | |
Integer = scyjava.jimport('java.lang.Integer') | |
Arrays = scyjava.jimport('java.util.Arrays') | |
SimpleImageScaler = scyjava.jimport('loci.common.image.SimpleImageScaler') | |
ServiceFactory = scyjava.jimport('loci.common.services.ServiceFactory') | |
FormatTools = scyjava.jimport('loci.formats.FormatTools') | |
ImageReader = scyjava.jimport('loci.formats.ImageReader') | |
ImageWriter = scyjava.jimport('loci.formats.ImageWriter') | |
OMEXMLService = scyjava.jimport('loci.formats.services.OMEXMLService') | |
DimensionOrder = scyjava.jimport('ome.xml.model.enums.DimensionOrder') | |
PositiveInteger = scyjava.jimport('ome.xml.model.primitives.PositiveInteger') | |
args = sys.argv | |
args = [ | |
'/Users/curtis/data/FluorescentCells.jpg', | |
'2', | |
'4', | |
'/Users/curtis/Desktop/FluorescentCells-pyramid.ome.tif' | |
] | |
if len(args) < 4: | |
print("python generate-pyramid-resolutions.py input-file scale-factor resolution-count output-file") | |
sys.exit(1) | |
in_file = args[0] | |
out_file = args[3] | |
scale = int(args[1]) | |
resolutions = int(args[2]) | |
reader = ImageReader() | |
factory = ServiceFactory() | |
service = factory.getInstance(OMEXMLService.class_) | |
meta = service.createOMEXMLMetadata() | |
reader.setMetadataStore(meta) | |
reader.setId(in_file) | |
for i in range(1, resolutions): | |
divScale = pow(scale, i) | |
meta.setResolutionSizeX(PositiveInteger(Integer(reader.getSizeX() // divScale)), 0, i) | |
meta.setResolutionSizeY(PositiveInteger(Integer(reader.getSizeY() // divScale)), 0, i) | |
scaler = SimpleImageScaler() | |
img = reader.openBytes(0) | |
# write image plane to disk | |
print("Writing image to '" + out_file + "'...") | |
writer = ImageWriter() | |
writer.setMetadataRetrieve(meta) | |
writer.setId(out_file) | |
writer.saveBytes(0, img) | |
pixelType = reader.getPixelType() | |
for i in range(1, resolutions): | |
writer.setResolution(i) | |
x = meta.getResolutionSizeX(0, i).getValue() | |
y = meta.getResolutionSizeY(0, i).getValue() | |
downsample = scaler.downsample(img, reader.getSizeX(), \ | |
reader.getSizeY(), pow(scale, i), \ | |
FormatTools.getBytesPerPixel(pixelType), reader.isLittleEndian(), \ | |
FormatTools.isFloatingPoint(pixelType), reader.getRGBChannelCount(), \ | |
reader.isInterleaved()) | |
writer.saveBytes(0, downsample) | |
writer.close() | |
reader.close() | |
print("Done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment