Printing/saving QGraphicsScene 到单色图像质量很差

Printing/saving QGraphicsScene to monochrome image very poor quality

我想要 print/save 单声道格式的 QGraphicsScene。我让场景只包含一个颜色对象(在其中一个windows中,它实际上是黑白的)。

OutputView::OutputView(QWidget *parent)
    : QGraphicsView(parent) {...}


void OutputView::saveToImage() 
{
    QImage image(scene()->sceneRect().size().toSize(), QImage::Format_Mono);
    image.fill(Qt::transparent);
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing); // seems to do nothing
    render(&painter);
    image.save("output.png");
    painter.end();

    // to test rgb, i am adding that - but I really need only mono
    // I don't know why the background is black, but since I don't intend to use the color version, it is not relevant
    QImage image1(scene()->sceneRect().size().toSize(), QImage::Format_RGB32);
    image1.fill(Qt::transparent);
    QPainter painter1(&image1);
    render(&painter1);
    image1.save("output1.png");
    painter1.end();
}

黑白图像质量很差!

我预计红色像素会变成黑色,但只有一部分会变成黑色。对于第 4 个窗格,我希望所有黑色像素在单声道输出中都被放置为黑色,但同样,只有其中一些像素做到了。

有什么方法可以在对象有颜色的地方用黑色像素制作黑白图像,而在其他地方用白色像素?

我可以轻松更改生成颜色部分的代码,以输出黑色而不是原始颜色,但这似乎并没有解决问题(第 4 个面板是黑色,但单色图像没有' t 包含所有像素)。

由于内存成本,我需要单声道...

如何将场景正确输出到 QImage 中?

Update1: 我什至尝试抖动彩色图像 - 结果很可怕(在应该更暗的区域太亮,奇怪的伪像,另外我添加了一个额外的步骤这很耗时,最糟糕的是,添加一个非常大的 rgb 图像...这正是我想要避免的,因为资源非常有限,我需要单色图像)

    QImage image(scene()->sceneRect().size().toSize(), QImage::Format_RGB32);
    image.fill(Qt::transparent);
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing); // seems to do nothing
    render(&painter);
    painter.end();
    QImage image11 = image.convertToFormat(QImage::Format_Mono,
              Qt::MonoOnly | Qt::DiffuseDither);
    image1.save("output.png");

使用内置的 qt 抖动方法,我看到渲染到单色图像使用 Qt::ThresholdDither

如果至少我可以一步对渲染应用不同的抖动,而不必先构建彩色图像,然后对新的单色图像进行抖动,那将是一个巨大的改进。记忆和时间都很重要。

虽然也许我真正需要的是一种快速遍历每个像素的方法,如果强度高于 .5 则将其设置为 1,如果低于 .5 则设置为 0?不过,这仍然意味着在创建 rgb32 图像之后需要一个单独的步骤......并且出于某种原因,这样做 "by hand" 总是比内置方法慢。

Update2:本例中绘制的项目是一个带有渐变画笔的矩形。我还将颜色分成 4 个 QGraphicsScenes:if color == color1 then color2,3,4 = Qt::white 等等。

void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
{
    QRadialGradient radialGradient(0, 0, 300, 100, 100);
    radialGradient.setColorAt(0.0, color1);
    radialGradient.setColorAt(0.27, color2);
    radialGradient.setColorAt(0.44, Qt::white);
    radialGradient.setColorAt(0.76, color3);
    radialGradient.setColorAt(1.0, color4);
    QBrush brush = QBrush(radialGradient);
    painter->setBrush(brush);
    painter->drawRect(boundingRect());
}

目前我的解决方案是:

  • 对于场景中的每个项目,如果需要将其转换为图像,请将其与所有碰撞并位于其上方的项目一起放置在场景中

  • 将场景的item矩形剪辑保存为彩图

  • 使用 Qt::OrderedDither 将图像转换为单色,这样会产生更少的瑕疵

  • 将图像放在场景中的项目位置

  • 使用默认值(Qt::ThresholdDither)将场景转换为非单色

这可能会使用较少的内存(取决于项目有多大)- 但很有可能会重复应用相同的过程,并以图像格式存储彼此重叠的区域。