Spaces:
Runtime error
Runtime error
File size: 1,572 Bytes
8c24611 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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) |