microbiology / loadImagesFromSVS.py
Jose M Delgado
formatting, tiles, training
1173bfd
import os
import slideio
from PIL import Image
####################Convert SVS files to JPEG files####################
# Set the path to the svs files
svs_path = "/Users/josedelgado/Downloads/"
# Set the path to the output directory
output_path = "/Users/josedelgado/Documents/GitHub/microbiology/Images"
# Set the size of the representative image
size = (224, 224)
# Loop through the svs files
for root, dirs, files in os.walk(svs_path):
for filename in files:
if filename.endswith(".svs"):
print("processing " + os.path.join(root, filename) )
# Open the svs file
slide = slideio.open_slide(os.path.join(root, filename))
# Get the number of scenes in the slide
num_scenes = slide.num_scenes
for i in range(num_scenes):
# Get the scene
scene = slide.get_scene(i)
# Check if the scene is not None
if scene is not None:
# Get the metadata of the scene
metadata_str = scene.get_raw_metadata()
print(metadata_str)
# Loop through the channels
for j in range(scene.num_channels):
# Read the channel datay
channel_data = scene.read_block(channel_indices=[j], size=(8000,0))
# Convert the channel data to a PIL image
image = Image.fromarray(channel_data)
# Set the output filename
output_filename = os.path.splitext(filename)[0] + f"_scene_{i}_channel_{j}.jpeg"
# Set the output directory
output_dir = os.path.join(output_path, os.path.relpath(root, svs_path))
# Create the output directory if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Save the image in the ImageNet file folder format
output_file = os.path.join(output_dir, output_filename)
image.save(output_file)
else:
print(f"Scene {i} is None for file {filename}")