ImageWidget.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Qt import
  2. from PyQt5.QtWidgets import QWidget, QLabel
  3. from PyQt5.QtGui import QPixmap, QImage
  4. from PyQt5.QtCore import Qt
  5. # import
  6. import numpy as np
  7. # miam import
  8. import miam.image.Image as MIMG
  9. class ImageWidget(QWidget):
  10. """description of class"""
  11. def __init__(self,image):
  12. super().__init__()
  13. # create a QtLabel for pixmap
  14. self.label = QLabel(self)
  15. # image content
  16. self.image = None
  17. self.set(image)
  18. def resize(self):
  19. self.label.resize(self.size())
  20. self.label.setPixmap(self.imagePixmap.scaled(self.size(),Qt.KeepAspectRatio))
  21. def set(self,image):
  22. # read image
  23. self.image = image
  24. # compute pixmap
  25. height, width, channel = self.image.shape
  26. bytesPerLine = channel * width
  27. # mask
  28. colorData = self.image.colorData*self.image.mask[...,np.newaxis] if self.image.hasMask() else self.image.colorData
  29. # QImage
  30. qImg = QImage((colorData*255).astype(np.uint8), width, height, bytesPerLine, QImage.Format_RGB888)
  31. self.imagePixmap = QPixmap.fromImage(qImg)
  32. self.resize()