Skip to content
3 changes: 2 additions & 1 deletion modules/bioformats2ometiff.nf
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ process bioformats2ometiff {
touch "${image.simpleName}.ome.tiff"
"""
script:
def rgb_flag = meta.he ? '--rgb' : ''
"""
bioformats2raw $image 'raw_dir'
raw2ometiff 'raw_dir' "${image.simpleName}.ome.tiff"
raw2ometiff ${rgb_flag} 'raw_dir' "${image.simpleName}.ome.tiff"
"""
}
44 changes: 37 additions & 7 deletions modules/make_miniature.nf
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,45 @@ process make_miniature {
"""
#!/usr/bin/env python

from tiffslide import TiffSlide
import matplotlib.pyplot as plt
import os
import tifffile
from PIL import Image
import numpy as np

slide = TiffSlide('$image')

thumb = slide.get_thumbnail((512, 512))
if thumb.mode in ("RGBA", "P"):
# Open the OME-TIFF file
with tifffile.TiffFile('$image') as tif:
# For pyramidal images, use the smallest level for efficiency
if len(tif.series) > 0 and hasattr(tif.series[0], 'levels') and len(tif.series[0].levels) > 1:
# Get the smallest pyramid level
level = tif.series[0].levels[-1]
img_array = level.asarray()
else:
# No pyramid or single level, read the full resolution
img_array = tif.asarray()

# Ensure proper data type for PIL (uint8)
if img_array.dtype != np.uint8:
# Scale to uint8 range if needed
img_min = img_array.min()
img_max = img_array.max()
if img_max > 255:
# Avoid division by zero
if img_max > img_min:
img_array = ((img_array - img_min) / (img_max - img_min) * 255).astype(np.uint8)
else:
img_array = np.zeros_like(img_array, dtype=np.uint8)
else:
img_array = img_array.astype(np.uint8)

# Convert numpy array to PIL Image
thumb = Image.fromarray(img_array)

# Create thumbnail (maintains aspect ratio)
thumb.thumbnail((512, 512))

# Ensure RGB mode
if thumb.mode != "RGB":
thumb = thumb.convert("RGB")

thumb.save('miniature.jpg')
"""
} else {
Expand Down
Loading