Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to save images in a different format



  • Hi everyone!
    my problem is that i am using 'save(const QString&, const char*)' function to save the image in another format but it doesn't work properly !
    the difference between the original and the save image is obviouse but the extention still the same!!
    i have a problem on my computer or i didn't understand what the 'save' function does??


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What code are you using ?
    What version of Qt are you using ?
    What OS are you running ?
    What compiler are you using ?
    What image format do you have as input ?
    What image format are you trying to have as output ?
    What is the exact error you have ?
    What output are you expecting ?
    What output are you getting ?



  • i am programming in C++ language , Qt 4.11.1 , on Windows 7, i am using Qt 5.14.2 MinGW 64 bit, input format (png, jpg, jpeg, gif), just(jpeg) as output, i did not have error or either warning , images are being saved succecfully but its not changing the format !


  • Lifetime Qt Champion

    @kosai Please show your code



  • void Mwindow::NvlElem(void)
    {
        QDir::setCurrent(m_ImgDir);//setting the Directory file
        if(!Line->text().isEmpty())
        {
            //if user write name of object
            //List->addItem(Line->text());
            List->addItem(Line->text());
            //**
            if(List->count() == 1)
                List->setCurrentRow(0);
            return;
        }
    
        //get Image
        QString ImgDir = QFileDialog::getOpenFileName(this,
                                                     "Ouvrir un fichier",
                                                     QString(),
                                                     "Images (*.png *.gif *.jpg *.jpeg)");
        //--Display Img on Tab
        ImgPixmap->load(ImgDir);
        ImgWin->setPixmap(*ImgPixmap);
        //--save Img in Items
        Image->load(ImgDir);
        QString ImgExt(ImgDir.right(4)), RImgName, ImgName;
        if(ImgExt[0] != ".")
            ImgExt = ImgDir.right(5);
        //get extention
        //--i did not find a better way to get extention !
        for (int i(ImgDir.length()); i >= 0; i--) {
            if(ImgDir[i] == "/")
                break;
            RImgName += ImgDir[i];
        }
        for(int i(RImgName.length()-1); i >= 0; i--) {
            if(RImgName[i] == ".")
                break;
            ImgName += RImgName[i];
        }
        //--here is the prblm !!!!
        if(Image->save(ImgName+ImgExt, "png"))
        {
            List->addItem(ImgName);
        }
        //*
        if(List->count() == 1)
            List->setCurrentRow(0);
    //*****tring another way  using QImageWriter but it doesnt work either!
        ImgWriter->setFileName(ImgName);
        Line->setText(ImgWriter->fileName());
    }
    

  • Lifetime Qt Champion

    The format is 'PNG', not 'png'.
    The extension can be retrieved via QFileInfo and there is no need to create a QImage with new.



  • ok!
    i tried "PNG" its the same problem !


  • Lifetime Qt Champion

    @kosai In your first post you wrote: "but the extention still the same". This is a different problem than "but its not changing the format". So, did you make sure the format was indeed not changed?



  • the problem is a little bit bizzard !
    it's like changing bytes into the desired format --> if you put saved image with the original you will distinguish the diffrence !
    but unlike if you go to the file on the desktop the extention doesn't change at all !!
    i hope this helps to understand my problem?


  • Lifetime Qt Champion

    @kosai said in How to save images in a different format:

    i hope this helps to understand my problem?

    Not really.
    What exactly is the problem:

    • Wrong file extension?
    • File is not stored as png?

    Please be more clear.
    If the file extension is not what you want then check your code and use @Christian-Ehrlicher suggested to get file extension.


  • Moderators

    @kosai said in How to save images in a different format:

    ile on the desktop the extention doesn't change at all !!

    why should the extension change ? you extract it from the select file

    QString ImgExt(ImgDir.right(4)), RImgName, ImgName;
        if(ImgExt[0] != ".")
            ImgExt = ImgDir.right(5);
    

    and than append it again to the filename when you save it

    Image->save(ImgName+ImgExt, "png")
    

    without changing it in-between, of course it will have the same extension.



  • yeah! you are right i shouldn't put the original extention with the name !
    thank you everyone !


  • Lifetime Qt Champion

    Another issue in your code, why are you forcing the output to be PNG and yet allow other extensions ? That does not really make sense. Personally I would be very surprised to find PNG while opening for example a .bmp file.


Log in to reply