How show Whole-slide image (.svs) in PySide2

793 views Asked by At

I want to use pyside2 to develop a pathological image recognition tool, but I don't know how to display the whole slide image (the suffix is .svs), just like the function of ASAP software can quickly open and display images.

1

There are 1 answers

3
eyllanesc On

You can use the OpenSlide read_region() method that returns a PIL.Image that can be converted to QImage using the PIL.ImageQt module.

import sys
from functools import cached_property

from PySide2.QtGui import QImage, QPixmap, QGuiApplication
from PySide2.QtWidgets import QApplication, QComboBox, QLabel, QVBoxLayout, QWidget
import openslide
from PIL.ImageQt import ImageQt


class OpenSlideViewer(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self._slide = None

        lay = QVBoxLayout(self)
        lay.addWidget(self.combo_level)
        lay.addWidget(self.label_slide)

        self.combo_level.currentIndexChanged.connect(self.handle_current_index_changed)

    @property
    def slide(self):
        return self._slide

    @cached_property
    def combo_level(self):
        return QComboBox()

    @cached_property
    def label_slide(self):
        return QLabel(scaledContents=True)

    def load(self, filename):
        self._slide = openslide.OpenSlide(filename)
        self.combo_level.clear()
        for level in range(self.slide.level_count):
            self.combo_level.addItem(f"level-{level}", level)
        self.combo_level.setCurrentIndex(0)

    def change_level(self, level):
        image = self.slide.read_region(
            (0, 0), level, self.slide.level_dimensions[level]
        )
        qimage = ImageQt(image)
        pixmap = QPixmap.fromImage(qimage)
        self.label_slide.setPixmap(pixmap)

    def handle_current_index_changed(self):
        level = self.combo_level.currentData()
        self.change_level(level)


def main(args):
    app = QApplication(args)

    viewer = OpenSlideViewer()
    filename = "/path/of/filename.svs"
    viewer.load(filename)
    viewer.resize(640, 480)
    viewer.show()

    ret = app.exec_()

    sys.exit(ret)


if __name__ == "__main__":
    main(sys.argv)