def createPixmap(self): """Creates the pixmap shown when this label is dragged.""" font_metric = QFontMetrics(QFont()) text_size = font_metric.size(Qt.TextSingleLine, self.text) image = QImage(text_size.width() + 4, text_size.height() + 4, QImage.Format_ARGB32_Premultiplied) image.fill(qRgba(240, 240, 120, 255)) painter = QPainter() painter.begin(image) painter.setFont(QFont()) painter.setBrush(Qt.black) painter.drawText(QRect(QPoint(2, 2), text_size), Qt.AlignCenter, self.text) painter.end() return image
def drawIconWithShadow(icon, rect, p, iconMode, radius, color, offset): cache = QPixmap() pixmapName = "icon {0} {1} {2}".format(icon.cacheKey(), iconMode, rect.height()) if not QPixmapCache.find(pixmapName, cache): px = icon.pixmap(rect.size()) cache = QPixmap(px.size() + QSize(radius * 2, radius * 2)) cache.fill(Qt.transparent) cachePainter = QPainter(cache) if iconMode == QIcon.Disabled: im = px.toImage().convertToFormat(QImage.Format_ARGB32) for y in range(im.height()): scanLine = im.scanLine(y) for x in range(im.width()): pixel = scanLine intensity = qGray(pixel) scanLine = qRgba(intensity, intensity, intensity, qAlpha(pixel)) scanLine += 1 px = QPixmap.fromImage(im) # Draw shadow tmp = QImage(px.size() + QSize(radius * 2, radius * 2 + 1), QImage.Format_ARGB32_Premultiplied) tmp.fill(Qt.transparent) tmpPainter = QPainter(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_Source) tmpPainter.drawPixmap(QPoint(radius, radius), px) tmpPainter.end() # blur the alpha channel blurred = QImage(tmp.size(), QImage.Format_ARGB32_Premultiplied) blurred.fill(Qt.transparent) blurPainter = QPainter(blurred) # todo : blur image blurPainter.end() tmp = blurred # blacken the image tmpPainter.begin(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_SourceIn) tmpPainter.fillRect(tmp.rect(), color) tmpPainter.end() tmpPainter.begin(tmp) tmpPainter.setCompositionMode(QPainter.CompositionMode_SourceIn) tmpPainter.fillRect(tmp.rect(), color) tmpPainter.end() # draw the blurred drop shadow... cachePainter.drawImage(QRect(0, 0, cache.rect().width(), cache.rect().height()), tmp) # Draw the actual pixmap... cachePainter.drawPixmap(QPoint(radius, radius) + offset, px) QPixmapCache.insert(pixmapName, cache) targetRect = cache.rect() targetRect.moveCenter(rect.center()) p.drawPixmap(targetRect.topLeft() - offset, cache)
def render(self, fileName, width, height): self.setViewportSize(QSize(width, height)) fileInfo = QFileInfo(fileName) dir = QDir() dir.mkpath(fileInfo.absolutePath()) viewportSize = self.viewportSize() pageSize = self.mainFrame().contentsSize() if pageSize.isEmpty(): return False buffer = QImage(pageSize, QImage.Format_ARGB32) buffer.fill(qRgba(255, 255, 255, 0)) p = QPainter(buffer) p.setRenderHint( QPainter.Antialiasing, True) p.setRenderHint( QPainter.TextAntialiasing, True) p.setRenderHint( QPainter.SmoothPixmapTransform, True) self.setViewportSize(pageSize) self.mainFrame().render(p) p.end() self.setViewportSize(viewportSize) return buffer.save(fileName)
def __init__(self, color_map, width, height): """Creates a QImage of width by height from the given colormap.""" super(ColorBarImage, self).__init__(width, height, QImage.Format_ARGB32_Premultiplied) # Qborder - this is black all the way around # The commented out parts were for white in one corner # but didn't come out nicely for w in range(width): #self.setPixel(w, 0, qRgba(255, 255, 255, 255)) self.setPixel(w, 0, qRgba(0, 0, 0, 255)) self.setPixel(w, height - 1, qRgba(0, 0, 0, 255)) for h in range(height): self.setPixel(0, h, qRgba(0, 0, 0, 255)) self.setPixel(width - 1, h, qRgba(0, 0, 0, 0)) #self.setPixel(width - 1, h, qRgba(255, 255, 255, 255)) # Draws the color map lineby line pixel_value = 1.0 / width for w in range(width-2): color_np = color_map(w * pixel_value) color_rgba = qRgba(*[round(255 * x) for x in color_np]) for h in range(height-2): self.setPixel(w + 1, h + 1, color_rgba)
def __init__(self, color_map, width, height): """Creates a QImage of width by height from the given colormap.""" super(ColorBarImage, self).__init__(width, height, QImage.Format_ARGB32_Premultiplied) # Qborder - this is black all the way around # The commented out parts were for white in one corner # but didn't come out nicely for w in range(width): #self.setPixel(w, 0, qRgba(255, 255, 255, 255)) self.setPixel(w, 0, qRgba(0, 0, 0, 255)) self.setPixel(w, height - 1, qRgba(0, 0, 0, 255)) for h in range(height): self.setPixel(0, h, qRgba(0, 0, 0, 255)) self.setPixel(width - 1, h, qRgba(0, 0, 0, 0)) #self.setPixel(width - 1, h, qRgba(255, 255, 255, 255)) # Draws the color map lineby line pixel_value = 1.0 / width for w in range(width - 2): color_np = color_map(w * pixel_value) color_rgba = qRgba(*[round(255 * x) for x in color_np]) for h in range(height - 2): self.setPixel(w + 1, h + 1, color_rgba)
def apply_filter(self, canvas): if canvas is None: return # TODO: реализовать # canvas.clearSelection(); self.make_undo_command(canvas) im = canvas.image for y in range(im.height()): for x in range(im.width()): pixel = im.pixel(x, y) gray = qGray(pixel) alpha = qAlpha(pixel) im.setPixel(x, y, qRgba(gray, gray, gray, alpha)) canvas.edited = True canvas.update()