How to add watermark of one QImage with Qt ?



  • 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


  • Moderators

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



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


  • Qt Champions 2018

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



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



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



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



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