如何根据我的图像分辨率调整字体大小?

huangapple go评论65阅读模式
英文:

How to adapt the font size to the resolution of my image?

问题

我有以下问题。
我正在尝试在我的图像上放置一个带有日期和时间的水印,我已经成功地将数据放在图像中,但我的问题是:如何使绘制在图像中的文本或字体的大小适应图像的分辨率?因为在一些图像中,我给它的大小看起来很好,即20,但在分辨率较高的图像中,它看起来非常小:我在这里留下我的代码,先谢谢。

QObject::connect(ui->pushButton_3, &QPushButton::clicked, this, [&](){

    QString fileName{"D:/images/IMG_20201231_004236.jpg"};
    QFileInfo fInfo{fileName};
    auto dateTime = fInfo.metadataChangeTime().toString("dd/MM/yyyy h:m");

    QImage newImage{fileName};

    if(newImage.isNull()){
        qInfo() << "Error!!";
        return;
    }
    QPainter painter{&newImage};
    painter.setOpacity(0.5);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::SolidPattern);
    painter.setFont(QFont("Arial", 20 * (newImage.width() / 1920)));  // Adjust font size based on image resolution
    painter.drawText(newImage.rect(), Qt::AlignBottom | Qt::AlignRight, dateTime);
    painter.end();
    newImage.save("D:/newImage.jpg");
    qInfo() << "saved image!!!" << '\n';
});
英文:

I have the following question.
I am trying to put a watermark on my images with date and time, I have managed to place the data in the image, but my question is: how can I make the size of the text or font that is painted in the image adapt? to the resolution of the image, since in some images it looks good with the size I gave it, which is 20, but in higher resolution images it looks very small: I leave my code here, thanks in advance.

QObject::connect(ui-&gt;pushButton_3, &amp;QPushButton::clicked, this, [&amp;](){


    QString fileName{&quot;D:/images/IMG_20201231_004236.jpg&quot;};
    QFileInfo fInfo{fileName};
    auto dateTime = fInfo.metadataChangeTime().toString(&quot;dd/MM/yyyy h:m&quot;);

    QImage newImage{fileName};


    if(newImage.isNull()){
      qInfo() &lt;&lt; &quot;Error!!&quot;;
      return;
    }
    QPainter painter{&amp;newImage};
    painter.setOpacity(0.5);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::SolidPattern);
    painter.setFont(QFont(&quot;Arial&quot;, 20));
    painter.drawText(newImage.rect(), Qt::AlignBottom | Qt::AlignRight, dateTime);
    painter.end();
    newImage.save(&quot;D:/newImage.jpg&quot;);
    qInfo() &lt;&lt; &quot;saved image!!!&quot; &lt;&lt; &#39;\n&#39;;


  });

I have not tried a solution yet, since I do not have much experience in Qt and c++, so I come to this forum, thanks

答案1

得分: 2

int pointSize = (20 * newImage.height()) / D;
painter.setFont(QFont("Arial", pointSize));

英文:

Something like this:

int pointSize = (20 * newImage.height()) / D;
painter.setFont(QFont(&quot;Arial&quot;, pointSize));

Where D is the height of some reference image that looks good when the font is rendered with a point size of 20.

huangapple
  • 本文由 发表于 2023年4月17日 10:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定