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.
Asked
Active
Viewed 543 times
1 Answers
0
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)

eyllanesc
- 235,170
- 19
- 170
- 241
-
I ran your code, but found that the computer memory is not enough (16g). Memory is mainly spent on the *load* method, and this method is very time-consuming. Can't Python realize the quick opening function similar to ASAP (it also uses openslide and QT). – coding May 09 '21 at 10:50
-
@coding What do you mean by ASAP? – eyllanesc May 09 '21 at 18:06
-
Sorry for getting back to you late. The ASAP can immediately open the *. SVS* image (the size is larger than 900M), and can zoom in and out of the image. Its GitHub link is ' https://github.com/computationalpathologygroup/ASAP '. I hope to quickly open the *.svs* image based on python, and zoom in and out. There is no way to do so. – coding May 10 '21 at 00:38