Can Qt Convert jpg images to the BMP format?



  • Can Qt Convert jpg images to the BMP format? If possible ,please give a little code,thanks .

    tried

    QImage('C:\\0dbcaadd144059fd76c6388a.jpg').save('C:\\2.jpg',"BMP")
    

    but just return False, anyone know why ?

    Googled, but not find an answer .


  • Moderators

    QPixmap pix("path-to/image.jpg");
    pix.save("path-to/image.bmp");
    

    Also see this


  • Lifetime Qt Champion

    Hi,

    Something like:

    QImage myJpgImage("path_to_jpeg.jpg");
    qDebug() << myJpgImage.save("path_to_bitmap.bmp");
    

    ?

    More information in the QImage documentation here and here

    [edit: it's been a long time since synchronous posting raven-worx :D]



  • tried
    @
    QImage('C:\0dbcaadd144059fd76c6388a.jpg').save('C:\2.jpg',"BMP")
    @
    but just return False, anyone know why ?
    [quote author="SGaist" date="1421848773"]Hi,

    Something like:

    @
    QImage myJpgImage("path_to_jpeg.jpg");
    qDebug() << myJpgImage.save("path_to_bitmap.bmp");
    @

    ?

    More information in the QImage documentation "here":http://doc.qt.io/qt-5/qimage.html#reading-and-writing-image-files and "here":http://doc.qt.io/qt-5/qimage.html#save

    [edit: it's been a long time since synchronous posting raven-worx :D][/quote]



  • Have you tried it ? save() just return false
    [quote author="raven-worx" date="1421848703"]@
    QPixmap pix("path-to/image.jpg");
    pix.save("path-to/image.bmp");
    @

    Also see "this":http://doc.qt.io/qt-5/qtimageformats-index.html[/quote]


  • Moderators

    [quote author="SGaist" date="1421848773"]
    [edit: it's been a long time since synchronous posting raven-worx :D]
    [/quote]
    yep! i was quite busy the last year :)

    [quote author="redstoneleo" date="1421850868"]Have you tried it ? save() just return false[/quote]
    more than once, yes :)

    ok lets try something else then. What does the following return?
    @
    QImageWriter::supportedImageFormats();
    @
    If the list contains "BMP" you should check if you are allowed to read/write the image files from/to the path you've specified.

    Also you should avoid something like this:
    @
    QImage('C:\0dbcaadd144059fd76c6388a.jpg').save('C:\2.jpg',"BMP")
    @
    Qt automatically checks the file extension and chooses the right "codec" to write the image.

    Edit: I am also not quite sure if it is allowed to write to "C:" in all cases (e.g. when you don't run the application as Administrator for example)


  • Lifetime Qt Champion

    To add to raven-worx, the file extension is the first thing checked, if Qt doesn't find a corresponding encoder, it's use the second parameter as a hint.

    Also, you can use unix's forward slash notation with Qt, so you don't take the risk of messing your path because you forgot a double backslash.



  • The reason why it didn't work before is I changed the image's suffix from jpg to bmp subjectively,then use QImage::​save() to convert format to bmp, thus it faild, after correct this mistake ,it worked now!

    BTW,for
    @bool QImage::​save(const QString & fileName, const char * format = 0, int quality = -1) const@
    If format is 0, QImage will attempt to guess the format by looking at fileName's suffix,so format is prior to fileName's suffix.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.