Example #1
0
def get_scaled_pixmap(path):
    if type(path) != str:
        image = path.convert("RGB")
        data = image.tobytes("raw", "RGB")
        image = QImage(data, image.size[0], image.size[1],
                       QImage.Format_RGB888)
    else:
        image = QImage(path)
        if not image.width():
            # 如果QImage打不开用PIL打开强转
            image = Image.open(path)
            image = image.convert("RGB")
            data = image.tobytes("raw", "RGB")
            image = QImage(data, image.size[0], image.size[1],
                           QImage.Format_RGB888)
    width = image.width()
    height = image.height()
    # 窄的图片取上方,宽的图片取中间,保持和神经网络一样的剪切方式
    if width < height:
        rect = QRect(0, 0, width, width)
    else:
        rect = QRect(int(width / 2 - height / 2), 0, height, height)
    image = image.copy(rect)
    return QPixmap(image.scaled(256, 256))
Example #2
0
    def run(self):
        try:
            while True:
                if (self.input_frame.data is not None) and (self.input_frame.not_in_use):
                    self.input_frame.not_in_use = False
                    if self._filter_enable:
                        kernel = (self._strength/10+5, self._strength/20+5)
                        sigma = self._strength/10+5
                        rate = self._rate/50+1
                        contrast = (self._rate+10)/50+1
                        brightness = self._strength
                        self.output_frame = self._filter(
                            self.input_frame.data,
                            kernel=kernel,
                            sigma=sigma,
                            rate=rate,
                            contrast=contrast,
                            brightness=brightness
                        ).astype('uint8')

                        image = self.output_frame
                        if self._use_mac_os:
                            if len(image.shape) != 3:
                                image = np.repeat(image[:,:,np.newaxis], 3, axis=2)

                            image = QImage(image,
                                           self.WIDTH,
                                           self.HEIGHT,
                                           self.WIDTH*self.CHANNEL,
                                           QImage.Format_RGB888)
                        else:
                            image = Image.fromarray(image)
                            if image.mode != 'RGB':
                                image = image.convert('RGB')
                            image = ImageQt(image)

                        pixmap = QPixmap.fromImage(image)
                        pixmap = pixmap.scaled(
                            640, 480, QtCore.Qt.KeepAspectRatio)
                        self._img_lab.setPixmap(pixmap)
                        del image
                        del self.input_frame.data

                    else:
                        self.output_frame = self.input_frame.data

                        if self._use_mac_os:
                            if len(self.output_frame.shape) != 3:
                                self.output_frame = np.repeat(self.output_frame[:,:,np.newaxis], 3, axis=2)
                            
                            image = QImage(self.output_frame,
                                           self.WIDTH,
                                           self.HEIGHT,
                                           self.WIDTH*self.CHANNEL,
                                           QImage.Format_RGB888)
                        else:                   
                            self.output_frame = self.input_frame.data
                            image = Image.fromarray(self.output_frame)
                            image = ImageQt(image)

                        pixmap = QPixmap.fromImage(image)
                        pixmap = pixmap.scaled(
                            640, 480, QtCore.Qt.KeepAspectRatio)
                        self._img_lab.setPixmap(pixmap)
                        del image
                        del self.input_frame.data
                    self.input_frame.not_in_use = True
        finally:
            pass