def loadFromFile(self): if self.sharedMemory.isAttached(): self.detach() self.ui.label.setText(self.tr("Select an image file")) fileName, _ = QFileDialog.getOpenFileName( None, "", "", self.tr("Images (*.png *.xpm *.jpg)")) image = QImage() if not image.load(fileName): self.ui.label.setText( self.tr( "Selected file is not an image, please select another.")) return self.ui.label.setPixmap(QPixmap.fromImage(image)) buffer = QBuffer() buffer.open(QBuffer.ReadWrite) out = QDataStream(buffer) out << image size = buffer.size() if not self.sharedMemory.create(size): self.ui.label.setText( self.tr("Unable to create shared memory segment.")) return self.sharedMemory.lock() size = min(self.sharedMemory.size(), size) self.sharedMemory.data()[:size] = buffer.data()[:size] self.sharedMemory.unlock()
def convert_svg_to_bitmap(source, target): svg_renderer = QSvgRenderer(source) height = svg_renderer.defaultSize().height() width = svg_renderer.defaultSize().width() new_image = QImage(width, height, QImage.Format_ARGB32) painter = QPainter(new_image) svg_renderer.render(painter) new_image.save(target) painter.end()
def get_image_height(image_path): """ Returns the height of the image stored in given file path :param image_path: str :return: float """ if not image_path or not os.path.isfile(image_path): return 0 image = QImage(image_path) if not image or image.isNull(): return 0 return image.height()
def create_empty_image(output=None, resolution_x=1920, resolution_y=1080, background_color=None): """ Creates an empty image and stores it in the given path :param output: str :param resolution_x: int :param resolution_y: int :param background_color: list(int, int, int) :return: str or QImage """ if background_color is None: background_color = [0, 0, 0] pixmap = QPixmap(resolution_x, resolution_y) pixmap.fill(QColor(*background_color)) if output: output_path = path_utils.clean_path(output) output_dir = os.path.dirname(output_path) if os.access(output_dir, os.W_OK): pixmap.save(output_path) return output_path return QImage(pixmap)
def saveScreenshot(self, clipboard=False, fileName='screenshot.png', picType='png'): fullWindow = QRect(0, 0, self.width() - 1, self.height() - 1) selected = QRect(self.selectedArea) if selected.left() < 0: selected.setLeft(0) if selected.right() >= self.width(): selected.setRight(self.width() - 1) if selected.top() < 0: selected.setTop(0) if selected.bottom() >= self.height(): selected.setBottom(self.height() - 1) source = (fullWindow & selected) source.setTopLeft( QPoint(source.topLeft().x() * self.scale, source.topLeft().y() * self.scale)) source.setBottomRight( QPoint(source.bottomRight().x() * self.scale, source.bottomRight().y() * self.scale)) image = self.screenPixel.copy(source) image = self.grab().copy(source) image = image.toImage() # if clipboard: # QApplication.clipboard().setImage(image, QClipboard.Mode.Clipboard) # else: # image.save(fileName) self.target_img = image self.screen_shot_grabed.emit(QImage(image))
def runAnimation(): Col = MainWindow.MainController() # 开机动画 splash = QSplashScreen() scale = 0.5 mgnWidth = int(Data.getWindowWidth() * scale) mgnHeight = int(Data.getWindowHeight() * scale) size = QSize(mgnWidth, mgnHeight) splash.show() for name in os.listdir(filePath + r"\res\ZeusDesign\startup_seq"): path = filePath + r"\res\ZeusDesign\startup_seq\\" + name image = QImage(path) pixmap = QPixmap.fromImage(image.scaled(size, Qt.IgnoreAspectRatio)) splash.setPixmap(pixmap) time.sleep(0.01) splash.finish(Col.view)
def run(self): """ Overrides base QRunnable run function This is the starting point for the thread """ try: if self._path: image = QImage(str(self._path)) self.signals.triggered.emit(image) except Exception as e: LOGGER.error('Cannot load thumbnail image!')
def calculate_width_scalar(readme_dir, img_nodes, target_width): max_image_width = -1 for img in img_nodes: src_attr = img.attrib.get("src", "") if src_attr.startswith("data:image"): continue src_path = os.path.abspath(os.path.join(readme_dir, src_attr)) max_image_width = max(QImage(src_path).width(), max_image_width) if max_image_width > 0: return float(target_width) / max_image_width else: return 1.0
def populateScene(self): self.scene = QGraphicsScene() image = QImage(":/qt4logo.png") # Populate scene xx = 0 nitems = 0 for i in range(-11000, 11000, 110): xx += 1 yy = 0 for j in range(-7000, 7000, 70): yy += 1 x = (i + 11000) / 22000.0 y = (j + 7000) / 14000.0 color = QColor( image.pixel(int(image.width() * x), int(image.height() * y))) item = Chip(color, xx, yy) item.setPos(QPointF(i, j)) self.scene.addItem(item) nitems += 1
def set_background_image(self, value, fit_to_window=False, mirror_x=False, mirror_y=False): if not value: return value = str(value) if not (value and os.path.exists(value)): print('background image not found: {}'.format(value)) return self._background_image_path = value self._fit_image_to_window = fit_to_window # Load image and mirror it vertically self._background_image = QImage(value).mirrored(mirror_x, mirror_y) if not fit_to_window: width = self._background_image.width() height = self._background_image.height() self.scene().set_size(width, height) self.fit_scene_content()
def base64_to_image(base64_string, image_format='PNG'): """ Converts base64 to QImage :param base64_string: str :param image_format: str :return: QImage """ try: ba = QByteArray.fromBase64(base64_string) image = QImage.fromData(ba, image_format) return image except Exception: return None
def embeded_image_scale_to_width(src_path, width_scalar): qimage = QImage(src_path) new_width = width_scalar * qimage.width() qimage = qimage.scaledToWidth(new_width, Qt.SmoothTransformation) data = QByteArray() buffer = QBuffer(data) buffer.open(QIODevice.WriteOnly) qimage.save(buffer, "JPG") base64_data = data.toBase64().data().decode('ascii') return "data:image/jpeg;base64,{base64_data}".format( base64_data=base64_data)
def create_image(width, height, mode): """ Creates a new QImage with the given resolution and mode :param width: int :param height: int :param mode: str :return: QImage """ mode = mode_to_qformat(mode) if width <= 0 or height <= 0: raise Exception( 'Resolution for new image is negative or zero (X: {}, Y: {})'. format(width, height)) new_image = QImage(width, height, mode) return new_image
def loadFromMemory(self): if not self.sharedMemory.attach(): self.ui.label.setText( self.tr("Unable to attach to shared memory segment.\n" "Load an image first.")) return buffer = QBuffer() _in = QDataStream(buffer) image = QImage() self.sharedMemory.lock() buffer.setData(self.sharedMemory.data()) buffer.open(QBuffer.ReadOnly) _in >> image self.sharedMemory.unlock() self.sharedMemory.detach() self.ui.label.setPixmap(QPixmap.fromImage(image))
def handleNetworkData(self, reply: QNetworkReply) -> None: img = QImage() tp = reply.request().attribute(QNetworkRequest.User) if not reply.error(): if not img.load(reply, ""): img = QImage() reply.deleteLater() self.m_tilePixmaps[QPointH(tp)] = (self.m_emptyTile if img.isNull() else QPixmap.fromImage(img)) self.updated.emit(self.tileRect(tp)) # purge unused spaces bound = self.m_tilesRect.adjusted(-2, -2, 2, 2) self.m_tilePixmaps = { tp: pixmap for tp, pixmap in self.m_tilePixmaps.items() if bound.contains(tp) } self.download()
class GridBackgroundImageView(GridView, object): """ View with image background drawing support """ def __init__(self, parent=None): super(GridBackgroundImageView, self).__init__(parent=parent) self._background_image = None self._background_image_path = None self._fit_image_to_window = False self.setBackgroundBrush(QBrush(QColor(70, 70, 70, 255))) def get_background_image(self): return self._background_image def set_background_image(self, value, fit_to_window=False, mirror_x=False, mirror_y=False): if not value: return value = str(value) if not (value and os.path.exists(value)): print('background image not found: {}'.format(value)) return self._background_image_path = value self._fit_image_to_window = fit_to_window # Load image and mirror it vertically self._background_image = QImage(value).mirrored(mirror_x, mirror_y) if not fit_to_window: width = self._background_image.width() height = self._background_image.height() self.scene().set_size(width, height) self.fit_scene_content() background_image = property(get_background_image, set_background_image) # def resizeEvent(self, event): # TODO: tpoveda: This is not working when an image is not loaded # if self._fit_image_to_window: # self.scene().set_size(self.rect().width(), self.rect().height()) # else: # self.fit_scene_content() # super(GridBackgroundImageView, self).resizeEvent(event) def drawBackground(self, painter, rect): """ Override method to draw view custom background image """ if not self.background_image: return super(GridBackgroundImageView, self).drawBackground(painter, rect) super(GridBackgroundImageView, self).drawBackground(painter, rect) painter.drawImage(self.sceneRect(), self.background_image, QRectF(self.background_image.rect()))