将QGraphicsScene打印/保存为单色图像质量非常差

时间:2015-04-23 16:33:16

标签: c++ image qt monochrome

我想以Mono格式打印/保存QGraphicsScene。我让场景只包含一个颜色对象(在其中一个窗口中,它实际上是黑色和白色)。

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();
}

黑白图像的质量很差!

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

enter image description here

有没有办法用黑色像素制作黑白图像,其中对象有颜色,而其他地方有白色?

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

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

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

更新1:我甚至试图抖动彩色图像 - 结果很可怕(在应该更暗的区域太亮,怪异的工件,加上我添加一个耗时的额外步骤,最糟糕的是,添加一个非常大的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");

enter image description here

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

如果至少我可以在渲染中应用不同的抖动,只需一步,无需首先构建彩色图像然后抖动到新的单色图像,这将是一个巨大的进步。记忆和时间都非常重要。

虽然也许我真正需要的是快速浏览每个像素并将其强度设置为1(如果其强度高于.5)并将其设置为0(如果低于此值)?这仍然意味着在创建rgb32图像之后的一个单独的步骤...并且由于某种原因“手动”执行它总是比内置方法慢。

Update2:此示例中绘制的项目是带有渐变画笔的矩形。我还将颜色分为4个QGraphicsScenes:如果color == color1则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());
}

1 个答案:

答案 0 :(得分:0)

现在我的解决方案是:

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

  • 将场景的项目矩形剪辑保存为彩色图像

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

  • 放置图像代替场景中的项目

  • 使用默认(Qt::ThresholdDither

  • 将场景转换为单色

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