File size: 2,289 Bytes
1173bfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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}")