Esempio n. 1
0
    def test_bitmap_to_icon(self):
        qpixmap = QPixmap(32, 64)
        qpixmap.fill(QColor(0x44, 0x88, 0xcc))

        qimage = bitmap_to_icon(qpixmap)

        self.assertIsInstance(qimage, QIcon)
Esempio n. 2
0
def convert_bitmap(image, width=0, height=0):
    if isinstance(image, ImageResource):
        pix = traitsui_convert_bitmap(image)
    elif isinstance(image, (PILImage.Image, )):
        try:
            data = image.tostring('raw', 'RGBA')
        except NotImplementedError:
            data = image.tobytes('raw', 'RGBA')
        im = QImage(data, image.size[0], image.size[1], QImage.Format_ARGB32)
        pix = QPixmap.fromImage(QImage.rgbSwapped(im))
    else:
        s = image.shape
        if len(s) >= 2:
            pix = QPixmap.fromImage(array2qimage(image))
        else:
            pix = QPixmap()

    if pix:
        if width > 0 and height > 0:
            pix = pix.scaled(width, height)
        elif width > 0:
            pix = pix.scaledToWidth(width)
        if height > 0:
            pix = pix.scaledToHeight(height)

    return pix
Esempio n. 3
0
    def _set_image(self, v):
        if v != self._count:
            self._count = v
            if v in self._images:
                pix = self._images[v]
            else:
                reader = QImageReader(self._path)

                c = v % self._column_cnt
                r = v / self._column_cnt
                w, h = 22, 22
                reader.setClipRect(QRect(c * w, r * h, w, h))

                pix = QPixmap()
                pix.convertFromImage(reader.read())
                self._images[v] = pix

            self.label.setPixmap(pix)
Esempio n. 4
0
    def _set_image(self, v):
        if v != self._count:
            self._count = v
            if v in self._images:
                pix = self._images[v]
            else:
                reader = QImageReader(self._path)

                c = v % self._column_cnt
                r = v / self._column_cnt
                w, h = 22, 22
                reader.setClipRect(QRect(c * w, r * h, w, h))

                pix = QPixmap()
                pix.convertFromImage(reader.read())
                self._images[v] = pix

            self.label.setPixmap(pix)
Esempio n. 5
0
    def set_tile(self, image):

        data = image.tobytes('raw', 'RGB')

        im = QImage(data, image.size[0], image.size[1], QImage.Format_RGB888)
        pix = QPixmap.fromImage(im)

        self._pix_maps.append(pix)
        # print(self._pix_map)
        self.update()
    def mouseMoveEvent(self, event):

        if QLineF(event.screenPos(),
                  event.buttonDownScreenPos(Qt.LeftButton)).length() < QApplication.startDragDistance():
            return

        drag = QDrag(event.widget())
        mime = QMimeData()
        drag.setMimeData(mime)

        mime.setText("ExpressionBasedForce")

        pixmap = QPixmap(int(self.boundingRect().width()),
                         int(self.boundingRect().height()))
        pixmap.fill(Qt.white)

        painter = QPainter(pixmap)
        painter.setRenderHint(QPainter.Antialiasing)

        self.paint(painter, QStyleOptionGraphicsItem(), event.widget())
        painter.end()

        pixmap.setMask(pixmap.createHeuristicMask())

        drag.setPixmap(pixmap)
        drag.setHotSpot(QPoint(int(self.boundingRect().width()/2.0), int(self.boundingRect().height()/2.0)))

        drag.exec_()
        self.setCursor(Qt.OpenHandCursor)
Esempio n. 7
0
    def mouseMoveEvent(self, event):

        if QLineF(event.screenPos(),
                  event.buttonDownScreenPos(Qt.LeftButton)).length() < QApplication.startDragDistance():
            return

        print "AtomItem::mouseMoveEvent"
        drag = QDrag(event.widget())
        mime = QMimeData()
        # A weak solution that could not be implemented in
        # C++
        mime.atom = self.atom
        mime.atom_item = self
        drag.setMimeData(mime)

        mime.setText("Atom")

        pixmap = QPixmap(int(self.boundingRect().width()),
                         int(self.boundingRect().height()))
        pixmap.fill(Qt.white)

        painter = QPainter(pixmap)
        painter.setRenderHint(QPainter.Antialiasing)

        self.paint(painter, QStyleOptionGraphicsItem(), event.widget())
        painter.end()

        pixmap.setMask(pixmap.createHeuristicMask())

        drag.setPixmap(pixmap)
        drag.setHotSpot(QPoint(int(self.boundingRect().width()/2.0), int(self.boundingRect().height()/2.0)))

        drag.exec_()
        self.setCursor(Qt.OpenHandCursor)
Esempio n. 8
0
def convert_bitmap(image, width=0, height=0):
    if isinstance(image, ImageResource):
        pix = traitsui_convert_bitmap(image)
    elif isinstance(image, (PILImage.Image,)):
        try:
            data = image.tostring('raw', 'RGBA')
        except NotImplementedError:
            data = image.tobytes('raw', 'RGBA')
        im = QImage(data, image.size[0], image.size[1], QImage.Format_ARGB32)
        pix = QPixmap.fromImage(QImage.rgbSwapped(im))
    else:
        s = image.shape
        if len(s) >= 2:
            pix = QPixmap.fromImage(array2qimage(image))
        else:
            pix = QPixmap()

    if pix:
        if width > 0 and height > 0:
            pix = pix.scaled(width, height)
        elif width > 0:
            pix = pix.scaledToWidth(width)
        if height > 0:
            pix = pix.scaledToHeight(height)

    return pix
Esempio n. 9
0
    def test_resize_bitmap_smooth(self):
        qpixmap = QPixmap(32, 64)
        qpixmap.fill(QColor(0x44, 0x88, 0xcc))

        qpixmap = resize_bitmap(qpixmap, (128, 128), mode=ScaleMode.smooth)

        self.assertIsInstance(qpixmap, QPixmap)
        self.assertEqual(qpixmap.width(), 128)
        self.assertEqual(qpixmap.height(), 128)
Esempio n. 10
0
    def test_resize_bitmap_expand(self):
        qpixmap = QPixmap(32, 64)
        qpixmap.fill(QColor(0x44, 0x88, 0xcc))

        qpixmap = resize_bitmap(qpixmap, (128, 128), AspectRatio.keep_expand)

        self.assertIsInstance(qpixmap, QPixmap)
        self.assertEqual(qpixmap.width(), 128)
        self.assertEqual(qpixmap.height(), 256)
Esempio n. 11
0
    def _update(self):
        if self.value:
            # w, h = self.control.width(), self.control.height()
            # img = self.value.get_image_data(size=(w, h))
            img = self.value.get_image_data()
            if img is not None:
                s = img.shape
                if s:
                    im = QImage(img, s[1], s[0], QImage.Format_RGB32)
                    # im = QImage(img, s[1], s[0], QImage.Format_RGB16)
                    if self.swap:
                        im = QImage.rgbSwapped(im)

                    pix = QPixmap.fromImage(im)
                    self.control.setPixmap(pix)
Esempio n. 12
0
    def _update(self):
        if self.value:
            # w, h = self.control.width(), self.control.height()
            # img = self.value.get_image_data(size=(w, h))
            img = self.value.get_image_data()
            if img is not None:
                s = img.shape
                if s:
                    im = QImage(img, s[1], s[0], QImage.Format_RGB32)
                    # im = QImage(img, s[1], s[0], QImage.Format_RGB16)
                    if self.swap:
                        im = QImage.rgbSwapped(im)

                    pix = QPixmap.fromImage(im)
                    self.control.setPixmap(pix)
Esempio n. 13
0
def image_to_bitmap(image):
    """ Convert a QImage to a QPixmap.
    Parameters
    ----------
    image : QImage
        The QImage to convert.

    Return
    ------
    bitmap : QPixmap
        The corresponding QPixmap.
    """
    bitmap = QPixmap.fromImage(image)
    # keep a reference to the QImage to ensure underlying data is available
    bitmap._image = image
    return bitmap
Esempio n. 14
0
 def set_frame(self):
     ok, data = self.cap.read()
     shape = data.shape
     im = QImage(data, shape[1], shape[0], QImage.Format_RGB888)
     pix = QPixmap.fromImage(QImage.rgbSwapped(im))
     self.label.setPixmap(pix)
Esempio n. 15
0
 def set_frame(self):
     ok, data = self.cap.read()
     shape = data.shape
     im = QImage(data, shape[1], shape[0], QImage.Format_RGB888)
     pix = QPixmap.fromImage(QImage.rgbSwapped(im))
     self.label.setPixmap(pix)