Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to adapt the font size to the resolution of my image?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 366 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • lincolnL Offline
    lincolnL Offline
    lincoln
    wrote on last edited by
    #1

    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';
    
    
      });
    
    
    JoeCFDJ 1 Reply Last reply
    0
    • lincolnL lincoln

      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';
      
      
        });
      
      
      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      @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.

      lincolnL 1 Reply Last reply
      1
      • JoeCFDJ JoeCFD

        @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.

        lincolnL Offline
        lincolnL Offline
        lincoln
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved