How to adapt the font size to the resolution of my image?
-
Hello guys, 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'; });
-
Hello guys, 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'; });
@lincoln said in How to adapt the font size to the resolution of my image?:
QImage
Now you hard-coded the font size. You can set coefficient which is determined by
the width or height of your image.
painter.setFont(QFont("Arial", coef * newImage.width() Or coef * newImage.height()));Note that your image may need to be scaled as well for your widget.
I use pixel size in font. Yours is not pixel size and you may need a global and consistent usage of font unit.
QFont("Arial", 20 ) 20 is point size, not pixel size. -
@lincoln said in How to adapt the font size to the resolution of my image?:
QImage
Now you hard-coded the font size. You can set coefficient which is determined by
the width or height of your image.
painter.setFont(QFont("Arial", coef * newImage.width() Or coef * newImage.height()));Note that your image may need to be scaled as well for your widget.
I use pixel size in font. Yours is not pixel size and you may need a global and consistent usage of font unit.
QFont("Arial", 20 ) 20 is point size, not pixel size.