当前位置: 首页>>代码示例>>Python>>正文


Python QImage.setPixel方法代码示例

本文整理汇总了Python中qgis.PyQt.QtGui.QImage.setPixel方法的典型用法代码示例。如果您正苦于以下问题:Python QImage.setPixel方法的具体用法?Python QImage.setPixel怎么用?Python QImage.setPixel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在qgis.PyQt.QtGui.QImage的用法示例。


在下文中一共展示了QImage.setPixel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: updateMask

# 需要导入模块: from qgis.PyQt.QtGui import QImage [as 别名]
# 或者: from qgis.PyQt.QtGui.QImage import setPixel [as 别名]
def updateMask(control_image_path, rendered_image_path, mask_image_path):
    control_image = imageFromPath(control_image_path)
    if not control_image:
        error('Could not read control image {}'.format(control_image_path))

    rendered_image = imageFromPath(rendered_image_path)
    if not rendered_image:
        error('Could not read rendered image {}'.format(rendered_image_path))
    if not rendered_image.width() == control_image.width() or not rendered_image.height() == control_image.height():
        print ('Size mismatch - control image is {}x{}, rendered image is {}x{}'.format(control_image.width(),
                                                                                        control_image.height(),
                                                                                        rendered_image.width(),
                                                                                        rendered_image.height()))

    max_width = min(rendered_image.width(), control_image.width())
    max_height = min(rendered_image.height(), control_image.height())

    #read current mask, if it exist
    mask_image = imageFromPath(mask_image_path)
    if mask_image.isNull():
        print 'Mask image does not exist, creating {}'.format(mask_image_path)
        mask_image = QImage(control_image.width(), control_image.height(), QImage.Format_ARGB32)
        mask_image.fill(QColor(0, 0, 0))

    #loop through pixels in rendered image and compare
    mismatch_count = 0
    linebytes = max_width * 4
    for y in xrange(max_height):
        control_scanline = control_image.constScanLine(y).asstring(linebytes)
        rendered_scanline = rendered_image.constScanLine(y).asstring(linebytes)
        mask_scanline = mask_image.scanLine(y).asstring(linebytes)

        for x in xrange(max_width):
            currentTolerance = qRed(struct.unpack('I', mask_scanline[x * 4:x * 4 + 4])[0])

            if currentTolerance == 255:
                #ignore pixel
                continue

            expected_rgb = struct.unpack('I', control_scanline[x * 4:x * 4 + 4])[0]
            rendered_rgb = struct.unpack('I', rendered_scanline[x * 4:x * 4 + 4])[0]
            difference = colorDiff(expected_rgb, rendered_rgb)

            if difference > currentTolerance:
                #update mask image
                mask_image.setPixel(x, y, qRgb(difference, difference, difference))
                mismatch_count += 1

    if mismatch_count:
        #update mask
        mask_image.save(mask_image_path, "png")
        print 'Updated {} pixels in {}'.format(mismatch_count, mask_image_path)
    else:
        print 'No mismatches in {}'.format(mask_image_path)
开发者ID:PeterTFS,项目名称:QGIS,代码行数:56,代码来源:generate_test_mask_image.py


注:本文中的qgis.PyQt.QtGui.QImage.setPixel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。