def applycolors(data, pal): if args.PC: for color in pal: data.append(qBlue(color)) data.append(qGreen(color)) data.append(qRed(color)) data.append(qAlpha(color)) else: for color in pal: data.append(qRed(color)) data.append(qGreen(color)) data.append(qBlue(color)) data.append(qAlpha(color)) return data
def qimageToRaster(img, fileName, xMin=0, yMax=0, mupp=1, crsWkt=None): """ Use GDAL to turn a QImage to GeoTIFF file """ driver = gdal.GetDriverByName("GTiff") ds = driver.Create(fileName, img.width(), img.height(), 4, gdal.GDT_Byte, ['ALPHA=YES']) # setup affine transform ds.SetGeoTransform([xMin, mupp, 0, yMax, 0, -mupp]) if crsWkt is not None: ds.SetProjection(str(crsWkt)) size = img.height(), img.width() raster_r = numpy.zeros(size, dtype=numpy.uint8) raster_g = numpy.zeros(size, dtype=numpy.uint8) raster_b = numpy.zeros(size, dtype=numpy.uint8) raster_a = numpy.zeros(size, dtype=numpy.uint8) for i in xrange(img.width() * img.height()): x, y = i % img.width(), i / img.width() rgb = img.pixel(x, y) raster_r[y, x], raster_g[y, x], raster_b[y, x], raster_a[y, x] = qRed( rgb), qGreen(rgb), qBlue(rgb), qAlpha(rgb) ds.GetRasterBand(1).WriteArray(raster_r) ds.GetRasterBand(2).WriteArray(raster_g) ds.GetRasterBand(3).WriteArray(raster_b) ds.GetRasterBand(4).WriteArray(raster_a) ds = None # Once we're done, close properly the dataset
def to_SHTXFs(img): data = bytearray(SHTXFs_MAGIC) width = img.width() height = img.height() data.extend(from_u16(width)) data.extend(from_u16(height)) data.append(int(math.ceil(math.log(width, 2)))) data.append(int(math.ceil(math.log(height, 2)))) pal = img.colorTable() if len(pal) < 256: pal.extend([0] * (256 - len(pal))) for color in pal: data.append(qRed(color)) data.append(qGreen(color)) data.append(qBlue(color)) data.append(qAlpha(color)) data.extend(img.constBits().asstring(width * height)) return data
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 tintImage(cls, img, tintColor): """ :type img: QImage :type tintColor: QColor """ p = QPainter(img) p.setCompositionMode(QPainter.CompositionMode_Screen) for x in range(0, img.width()): for y in range(0, img.height()): rgbColor = img.pixel(x, y) alpha = qAlpha(rgbColor) c = QColor(rgbColor) if alpha > 0: c.toHsl() l = c.lightnessF()() newColor = QColor.fromHslF(tintColor.hslHueF(), tintColor.hslSaturationF(), l) newColor.setAlpha(alpha) img.setPixel(x, y, newColor.rgba())
def blit(src, dst, x, y, masked): w = src.width() h = src.height() out = dst.copy() for i in range(w): for j in range(h): dst_pixel = dst.pixel(x + i, y + j) src_pixel = src.pixel(i, j) # If src has transparency data, we use it, otherwise, we borrow from dst. # This logic doesn't quite work for bustup/l/si4? if masked or dst_pixel == TRANSPARENT_COLOR: out_pixel = src_pixel else: out_pixel = qRgba(qRed(src_pixel), qGreen(src_pixel), qBlue(src_pixel), qAlpha(dst_pixel)) out.setPixel(x + i, y + j, out_pixel) return out
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 qimageToRaster(img, fileName, xMin=0, yMax=0, mupp=1, crsWkt=None): """ Use GDAL to turn a QImage to GeoTIFF file """ driver = gdal.GetDriverByName("GTiff") ds = driver.Create(fileName, img.width(), img.height(), 4, gdal.GDT_Byte, ['ALPHA=YES']) # setup affine transform ds.SetGeoTransform([ xMin, mupp, 0, yMax, 0, -mupp ]) if crsWkt is not None: ds.SetProjection(str(crsWkt)) size = img.height(), img.width() raster_r = numpy.zeros(size, dtype=numpy.uint8) raster_g = numpy.zeros(size, dtype=numpy.uint8) raster_b = numpy.zeros(size, dtype=numpy.uint8) raster_a = numpy.zeros(size, dtype=numpy.uint8) for i in xrange(img.width()*img.height()): x,y = i % img.width(), i / img.width() rgb = img.pixel(x,y) raster_r[y,x], raster_g[y,x], raster_b[y,x], raster_a[y,x] = qRed(rgb), qGreen(rgb), qBlue(rgb), qAlpha(rgb) ds.GetRasterBand(1).WriteArray(raster_r) ds.GetRasterBand(2).WriteArray(raster_g) ds.GetRasterBand(3).WriteArray(raster_b) ds.GetRasterBand(4).WriteArray(raster_a) ds = None # Once we're done, close properly the dataset
def colorDiff(c1, c2): redDiff = abs(qRed(c1) - qRed(c2)) greenDiff = abs(qGreen(c1) - qGreen(c2)) blueDiff = abs(qBlue(c1) - qBlue(c2)) alphaDiff = abs(qAlpha(c1) - qAlpha(c2)) return max(redDiff, greenDiff, blueDiff, alphaDiff)
def toleranceMatch(self, px1, px2, rgbTolerances): if (abs(qRed(px1) - qRed(px2))) > rgbTolerances[0] or (abs(qGreen(px1) - qGreen(px2))) > rgbTolerances[1] or (abs(qBlue(px1) - qBlue(px2))) > rgbTolerances[2]: return False if not self.mw.ui.actionIgnore_Transparent_Pixels.isChecked(): return abs(qAlpha(px1) == qAlpha(px2)) return True