def data(self, index, role): if not index.isValid(): return QVariant() elif role != Qt.DisplayRole: return QVariant() return QVariant(qGray(self.modelImage.pixel(index.column(), index.row())))
def toGray(image): w, h = (image.width(), image.height()) for x in xrange(w): for y in xrange(h): pixel = image.pixel(x, y) gray = qGray(pixel) alpha = qAlpha(pixel) image.setPixel(x, y, qRgba(gray, gray, gray, alpha)) return image
def grayscale_pixmap(pixmap): from PyQt4.QtGui import qGray, qRgb image = pixmap.toImage() width = pixmap.width() height = pixmap.height() for i in range(0, width): for j in range(0, height): col = image.pixel(i, j) gray = qGray(col) image.setPixel(i, j, qRgb(gray, gray, gray)) pixmap = pixmap.fromImage(image) return pixmap
def __get_input_vector(img_path): width, height = IMG_SIZE image = QImage(width, height, QImage.Format_RGB32) if image.load(img_path): vector = [] get_value = lambda x, y: 1 - (qGray(image.pixel(x, y)) / 255.0) for y in range(height): for x in range(width): vector.append(get_value(x, y)) return vector else: raise Exception('Can not load file, bad format: %s' % img_path)
def desaterate(image, feedback=None): p = 100.0 / image.height() if not image.isGrayscale(): tem_image = QImage(image.width(), image.height(), QImage.Format_RGB32) for imy in range(image.height()): for imx in range(image.width()): lightness = qGray(image.pixel(imx, imy)) tem_image.setPixel(imx, imy, (lightness << 16) + (lightness << 8) + lightness) if feedback: feed = feedback(text = tr('Desaturate the Image. Press ESC to Cancel.'), progress = p * imy) if not(feed): break if feedback and feed: image = tem_image if feedback: feedback() return image
def desaterate(image, feedback=None): p = 100.0 / image.height() if not image.isGrayscale(): tem_image = QImage(image.width(), image.height(), QImage.Format_RGB32) for imy in range(image.height()): for imx in range(image.width()): lightness = qGray(image.pixel(imx, imy)) tem_image.setPixel(imx, imy, (lightness << 16) + (lightness << 8) + lightness) if feedback: feed = feedback( text=tr('Desaturate the Image. Press ESC to Cancel.'), progress=p * imy) if not (feed): break if feedback and feed: image = tem_image if feedback: feedback() return image
def __init__(self, image_file=None, default_wh=[1024.0, 1024.0], progress_handler = None): self.clear() self.image = None if progress_handler: self.ph = progress_handler else: self.ph = lambda i: None self.ph._message = '' self.ph._maxval = 0 if image_file and os.path.exists(image_file): # image tracing shall consist of 256 index colors ranked by luma img = QImage(image_file) if not img.isGrayscale() and QMessageBox("Image is not in grayscale", "Convert RGB image to grayscale?", QMessageBox.Question, QMessageBox.Yes, QMessageBox.No, 0).exec_() == QMessageBox.Yes: self.ph._message = 'Converting image to grayscale' self.ph._maxval = img.height() for y in range(img.height()): for x in range(img.width()): lightness = qGray(img.pixel(x, y)) img.setPixel(x, y, (lightness<<16) + (lightness<<8) + lightness) self.ph(y) colortable = [QColor(i, i, i).rgb() for i in xrange(256)] self.image = img.convertToFormat(QImage.Format_Indexed8, colortable) w, h = float(self.image.width()), float(self.image.height()) self.dpi = img.dotsPerMeterX() / 1000.0 * 25.4 else: w, h = default_wh self.dpi = 96.0 self.wh = [w, h] self.x1, self.y1, self.x2, self.y2 = [i/max(w, h) for i in [-w, -h, w, h]] self.dx = self.x2 - self.x1 self.dy = self.y2 - self.y1 # Resolution field in SVG units per pixel self.scale = self.dx / w
def LoadImageButtonClicked(self): self.filePath = QFileDialog.getOpenFileName(self, u"选择图片文件", "", "Images (*.bmp)") if self.filePath == "": QMessageBox.warning(self, u"发生错误", u"没有选择可以识别的文件!!") return self.srcImage = QImage(self.filePath) self.grayImage = QImage(self.srcImage.size(), QImage.Format_Indexed8) for i in range(256): self.grayImage.setColor(i, qRgb(i, i, i)) for i in range(self.srcImage.width()): for j in range(self.srcImage.height()): temp = qGray(self.srcImage.pixel(i, j)) self.grayImage.setPixel(i, j, temp) self.srcImage = QImage(self.grayImage) self.resultImage = QImage(self.grayImage) self.imageLabel.setPixmap(QPixmap(self.srcImage))
def desaterate(image, feedback=None): p = 100.0 / image.height() msg = tr('Desaturate the Image. Press ESC to Cancel.') pixel = image.pixel cancel = False if not image.isGrayscale(): tem_image = QImage(image.width(), image.height(), QImage.Format_Indexed8) tem_image.setColorTable(GRAYSCALE_COLORTABLE) width = range(image.width()) for imy in range(image.height()): for imx in width: lightness = qGray(pixel(imx, imy)) tem_image.setPixel(imx, imy, lightness) if feedback and not feedback(text=msg, progress=p * imy): cancel = True break if not cancel: image = tem_image if feedback: feedback() return image
def desaterate(image): p = 100.0 / image.height() msg = tr('Desaturate the Image. Press ESC to Cancel.') pixel = image.pixel event.CANCEL = False if not image.isGrayscale(): tem_image = QImage(image.width(), image.height(), QImage.Format_Indexed8) tem_image.setColorTable(GRAYSCALE_COLORTABLE) width = range(image.width()) for imy in range(image.height()): for imx in width: lightness = qGray(pixel(imx, imy)) tem_image.setPixel(imx, imy, lightness) event.emit(event.APP_STATUS, text=msg, progress=p * imy) if event.CANCEL: break if not event.CANCEL: image = tem_image event.emit(event.APP_STATUS) return image
def drawIconWithShadow(cls, icon, rect, painter, iconMode, dipRadius=3, color=QColor(0, 0, 0, 130), dipOffset=QPoint(1, -2)): """ :type icon: QIcon :type rect: QRect :type painter: QPainter :type iconMode: QIcon.Mode :type dipRadius: int :type color: QColor :type dipOffset: QPoint """ cache = QPixmap() pixmapName = "icon {0} {1} {2}".format(icon.cacheKey(), iconMode, rect.height()) if not QPixmapCache.find(pixmapName, cache): # High-dpi support: The in parameters (rect, radius, offset) are in # device-independent pixels. The call to QIcon.pixmap() below might # return a high-dpi pixmap, which will in that case have a devicePixelRatio # different than 1. The shadow drawing calculations are done in device # pixels. from math import ceil px = icon.pixmap(rect.size()) devicePixelRatio = int(ceil(cls.pixmapDevicePixelRatio(px))) radius = dipRadius * devicePixelRatio offset = dipOffset * devicePixelRatio # result = QPoint, like dipOffset 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(0, im.height()): scanLine = abs(im.scanLine(y)) for x in range(0, 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(QRect(radius, radius, px.width(), px.height()), px) tmpPainter.end() # blur the alpha channel blurred = QImage(tmp.size(), QImage.Format_ARGB32_Premultiplied) blurred.fill(Qt.transparent) blurPainter = QPainter(blurred) # qt_blurImage is not available in PyQt4, skip it # qt_blurImage(&blurPainter, tmp, radius, false, true) 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(QRect(QPoint(radius, radius) + offset, QSize(px.width(), px.height())), px) import PyQt4 if StrictVersion(PyQt4.QtCore.__version__).version[0] == 5: cache.setDevicePixelRatio(devicePixelRatio) QPixmapCache.insert(pixmapName, cache) targetRect = cache.rect() targetRect.setSize(targetRect.size() / cls.pixmapDevicePixelRatio(cache)) targetRect.moveCenter(rect.center() - dipOffset) painter.drawPixmap(targetRect, cache)
def getGrayPixel(image): w, h = (image.width( ), image.height( )) pixels = [qGray(image.pixel(x, y)) for x, y in zip(xrange(w), xrange(h))] return pixels
def getGrayPixel(image): w, h = (image.width(), image.height()) pixels = [qGray(image.pixel(x, y)) for x, y in zip(xrange(w), xrange(h))] return pixels