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)) # Read a representative image from the slide scene = slide.get_scene(0) print("region size: " + str(scene.size)) # Convert the image to RGB format image = scene.read_block(size=(500,0)) # Set the output filename output_filename = os.path.splitext(filename)[0] + ".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) # Convert the numpy array to a PIL image image = Image.fromarray(image) # Save the image in the ImageNet file folder format output_file = os.path.join(output_dir, output_filename) image.save(output_file)