英文:
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->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));
painter.drawText(newImage.rect(), Qt::AlignBottom | Qt::AlignRight, dateTime);
painter.end();
newImage.save("D:/newImage.jpg");
qInfo() << "saved image!!!" << '\n';
});
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("Arial", pointSize));
Where D
is the height of some reference image that looks good when the font is rendered with a point size of 20.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论