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 add watermark of one QImage with Qt ?
Forum Updated to NodeBB v4.3 + New Features

How to add watermark of one QImage with Qt ?

Scheduled Pinned Locked Moved Solved General and Desktop
qpainterqimagewatermark
8 Posts 4 Posters 4.7k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mounipanditi
    wrote on last edited by mounipanditi
    #1

    I am trying to draw a watermark using QPainter ontop of Image and by reading QPainter i have managed to write the below code. But i am getting error messages on console

    QImage image("/root/Desktop/rat.jpg");
    QPainter painter(&image);
    painter.setPen(QPen(Qt::green));
    painter.setFont(QFont("Times", 72, QFont::Bold));
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    QPoint point = QPoint( 150, 1250 );
    painter.drawText( point, "!..Happy New Year..!" );
    image.save("/u01/QtPractice/water/images/new_text.png");
    

    Error:
    Qt: Session management error: None of the authentication protocols specified are supported
    libpng warning: iCCP: known incorrect sRGB profile
    libpng warning: Interlace handling should be turned on when using png_read_image
    QPainter::setPen: Painter not active
    QPainter::setFont: Painter not active
    QPainter::setCompositionMode: Painter not active
    QPainter::end: Painter not active, aborted

    Help to solve this,

    Thanks in advance,
    Mounika.P

    raven-worxR jsulmJ joeQJ 3 Replies Last reply
    0
    • M mounipanditi

      I am trying to draw a watermark using QPainter ontop of Image and by reading QPainter i have managed to write the below code. But i am getting error messages on console

      QImage image("/root/Desktop/rat.jpg");
      QPainter painter(&image);
      painter.setPen(QPen(Qt::green));
      painter.setFont(QFont("Times", 72, QFont::Bold));
      painter.setCompositionMode(QPainter::CompositionMode_Source);
      QPoint point = QPoint( 150, 1250 );
      painter.drawText( point, "!..Happy New Year..!" );
      image.save("/u01/QtPractice/water/images/new_text.png");
      

      Error:
      Qt: Session management error: None of the authentication protocols specified are supported
      libpng warning: iCCP: known incorrect sRGB profile
      libpng warning: Interlace handling should be turned on when using png_read_image
      QPainter::setPen: Painter not active
      QPainter::setFont: Painter not active
      QPainter::setCompositionMode: Painter not active
      QPainter::end: Painter not active, aborted

      Help to solve this,

      Thanks in advance,
      Mounika.P

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @mounipanditi said in Painter not active:

      /root/Desktop/rat.jpg

      is this file really a JPG file? According to the error message it thinks it is a PNG?
      If it is a JPG file make sure you've also deployed the Qt JPG image plugin.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      M 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @mounipanditi said in Painter not active:

        /root/Desktop/rat.jpg

        is this file really a JPG file? According to the error message it thinks it is a PNG?
        If it is a JPG file make sure you've also deployed the Qt JPG image plugin.

        M Offline
        M Offline
        mounipanditi
        wrote on last edited by
        #3

        @raven-worx

        Hi raven-worx, my bad a small change in the code as of your prediction it is PNG image only thanks for correcting. The reason is i am having images with same name both with PNG and JPG.

        Can you please help me to create a water mark on image...!

        1 Reply Last reply
        0
        • M mounipanditi

          I am trying to draw a watermark using QPainter ontop of Image and by reading QPainter i have managed to write the below code. But i am getting error messages on console

          QImage image("/root/Desktop/rat.jpg");
          QPainter painter(&image);
          painter.setPen(QPen(Qt::green));
          painter.setFont(QFont("Times", 72, QFont::Bold));
          painter.setCompositionMode(QPainter::CompositionMode_Source);
          QPoint point = QPoint( 150, 1250 );
          painter.drawText( point, "!..Happy New Year..!" );
          image.save("/u01/QtPractice/water/images/new_text.png");
          

          Error:
          Qt: Session management error: None of the authentication protocols specified are supported
          libpng warning: iCCP: known incorrect sRGB profile
          libpng warning: Interlace handling should be turned on when using png_read_image
          QPainter::setPen: Painter not active
          QPainter::setFont: Painter not active
          QPainter::setCompositionMode: Painter not active
          QPainter::end: Painter not active, aborted

          Help to solve this,

          Thanks in advance,
          Mounika.P

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mounipanditi Not related to your question: why on earth are you working as root on Linux?!

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M mounipanditi

            I am trying to draw a watermark using QPainter ontop of Image and by reading QPainter i have managed to write the below code. But i am getting error messages on console

            QImage image("/root/Desktop/rat.jpg");
            QPainter painter(&image);
            painter.setPen(QPen(Qt::green));
            painter.setFont(QFont("Times", 72, QFont::Bold));
            painter.setCompositionMode(QPainter::CompositionMode_Source);
            QPoint point = QPoint( 150, 1250 );
            painter.drawText( point, "!..Happy New Year..!" );
            image.save("/u01/QtPractice/water/images/new_text.png");
            

            Error:
            Qt: Session management error: None of the authentication protocols specified are supported
            libpng warning: iCCP: known incorrect sRGB profile
            libpng warning: Interlace handling should be turned on when using png_read_image
            QPainter::setPen: Painter not active
            QPainter::setFont: Painter not active
            QPainter::setCompositionMode: Painter not active
            QPainter::end: Painter not active, aborted

            Help to solve this,

            Thanks in advance,
            Mounika.P

            joeQJ Offline
            joeQJ Offline
            joeQ
            wrote on last edited by joeQ
            #5

            @mounipanditi Hi, friend, you will know how to do it if you learned opencv.

            Here, i writen a demo with qt now. shared for you. Just code snippet. not the all

            Test Image

            The image is vaild for one mouth.

                /** ... */
            
                QString str = "/Users/XXX/work/imgs/M_01.jpg";
                if(img.load(str)){
                    QImage mark(img.width()/2,img.height()/2,QImage::Format_RGB32);
            
                    QPainter painter(&mark);
                    painter.fillRect(0,0,mark.width(),mark.height(),Qt::yellow);
                    QFont ft = painter.font();
                    ft.setPixelSize(40);
                    painter.setFont(ft);
                    painter.drawText(0,0,mark.width(),mark.height(),Qt::AlignCenter,"Qt");
            
                    QRgb rgbSrc,rgbMark;
                    int r,g,b;
                    float alpha = 0.6, beta = 1- alpha;
            
                    for(int x = 0; x < mark.width(); x++){
                        for(int y = 0; y < mark.height(); y++){
                            rgbSrc = img.pixel(x,y);
                            rgbMark = mark.pixel(x,y);
                            /** note:  */
                            r = int(qRed(rgbSrc) * alpha + qRed(rgbMark) * beta);
                            g = int(qGreen(rgbSrc) * alpha + qGreen(rgbMark) * beta);
                            b = int(qBlue(rgbSrc) * alpha + qBlue(rgbMark) * beta);
            
                            r = (0 <= r && r <= 255) ? r : 0;
                            g = (0 <= g && g <= 255) ? g : 0;
                            b = (0 <= b && b <= 255) ? b : 0;
            
                            img.setPixel(x,y,qRgb(r,g,b));
                        }
                    }
            
                    if(img.save("out.jpg")){
                        qDebug("Save Ok!");
                    }
                }else{
                    qDebug("img load failed!");
                }
            
                /** ... */
            

            Maybe you should change the titel to "How to add watermark of one QImage with Qt ?", it will help others has the problem like you.

            Just do it!

            M 1 Reply Last reply
            2
            • joeQJ joeQ

              @mounipanditi Hi, friend, you will know how to do it if you learned opencv.

              Here, i writen a demo with qt now. shared for you. Just code snippet. not the all

              Test Image

              The image is vaild for one mouth.

                  /** ... */
              
                  QString str = "/Users/XXX/work/imgs/M_01.jpg";
                  if(img.load(str)){
                      QImage mark(img.width()/2,img.height()/2,QImage::Format_RGB32);
              
                      QPainter painter(&mark);
                      painter.fillRect(0,0,mark.width(),mark.height(),Qt::yellow);
                      QFont ft = painter.font();
                      ft.setPixelSize(40);
                      painter.setFont(ft);
                      painter.drawText(0,0,mark.width(),mark.height(),Qt::AlignCenter,"Qt");
              
                      QRgb rgbSrc,rgbMark;
                      int r,g,b;
                      float alpha = 0.6, beta = 1- alpha;
              
                      for(int x = 0; x < mark.width(); x++){
                          for(int y = 0; y < mark.height(); y++){
                              rgbSrc = img.pixel(x,y);
                              rgbMark = mark.pixel(x,y);
                              /** note:  */
                              r = int(qRed(rgbSrc) * alpha + qRed(rgbMark) * beta);
                              g = int(qGreen(rgbSrc) * alpha + qGreen(rgbMark) * beta);
                              b = int(qBlue(rgbSrc) * alpha + qBlue(rgbMark) * beta);
              
                              r = (0 <= r && r <= 255) ? r : 0;
                              g = (0 <= g && g <= 255) ? g : 0;
                              b = (0 <= b && b <= 255) ? b : 0;
              
                              img.setPixel(x,y,qRgb(r,g,b));
                          }
                      }
              
                      if(img.save("out.jpg")){
                          qDebug("Save Ok!");
                      }
                  }else{
                      qDebug("img load failed!");
                  }
              
                  /** ... */
              

              Maybe you should change the titel to "How to add watermark of one QImage with Qt ?", it will help others has the problem like you.

              M Offline
              M Offline
              mounipanditi
              wrote on last edited by
              #6

              Dear friend @joeQ,

                           Thankyou for your reply.The snippet which you have posted was working fine.
              

              Is there any possibility to remove the rectangle area around the watermark.

              And i will change the titile as you have stated.

              joeQJ 1 Reply Last reply
              0
              • M mounipanditi

                Dear friend @joeQ,

                             Thankyou for your reply.The snippet which you have posted was working fine.
                

                Is there any possibility to remove the rectangle area around the watermark.

                And i will change the titile as you have stated.

                joeQJ Offline
                joeQJ Offline
                joeQ
                wrote on last edited by
                #7

                @mounipanditi

                did you read the code careful ? The code snippet is not complex. Yuo should read the code snippet and to leare them. why use this way.

                Just do it!

                M 1 Reply Last reply
                1
                • joeQJ joeQ

                  @mounipanditi

                  did you read the code careful ? The code snippet is not complex. Yuo should read the code snippet and to leare them. why use this way.

                  M Offline
                  M Offline
                  mounipanditi
                  wrote on last edited by
                  #8

                  @joeQ
                  As per your suggesttion i'll make changes in the code on my own.Once again thanks for your help.

                  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