Results 1 to 8 of 8

Thread: save Qimage in a file

  1. #1
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default save Qimage in a file

    hi,
    l'm downloading a file ( image.png) from an instrument using GPIB...l get the file as ASCII, save it in a file. But then when l try to load in Qimage, l get a error message from Libpng lirary. but l can open the saved image on the disk with Infrview and other picture viewer.


    Qt Code:
    1. QFile file("out.png");
    2. if (!file.open(QIODevice::WriteOnly))
    3. return 0;
    4. QTextStream out(&file);
    5. out << image;
    6. file.close();
    7. myImage->load("out.png");
    To copy to clipboard, switch view to plain text mode 

    should l convert the Qstring image to an other format before to save in the file ?

    regards,

    Michael
    Last edited by wysota; 18th March 2011 at 22:37. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: save Qimage in a file

    Are you sure it is a png? Maybe the format guessing of your picture viewers is stronger.

    Have a look into the file and see if the header really looks like png.

    Try saving the file in your picture viewer as png and load that from your program. Can you load any other png file?

    Joh

  3. #3
    Join Date
    Nov 2010
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: save Qimage in a file

    Does your image variable include the correct PNG header? It looks like youre trying to read in image values and possibly not including the header. If thats the case, it might be easier to manually parse the file and modify the pixel values, using QImage::setPixel or QImage::scanLine.

  4. #4
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: save Qimage in a file

    thanks guys for yours answers.

    l checked the file, there is the png tag...and l actually opened it with Infranview and resaved it as PNG, then visually compared ( in wordpad) both files...they are the same format.
    When l try to load the first png file with qimage.load(), it fails... but it is successful if l load the second file ( the one resave with infranview).

    I'm just wondering if l should save the file with a specific format ( uint, string ..), maybe Infranview can detect the format while Qimage can not, it should be a specific format...

    any idea about that ?

    regards,

    Michael

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: save Qimage in a file

    l get the file as ASCII
    This sounds problematic. Different machines have different ways of encoding text; most programs will attempt to correct for such differences by translating between them on the fly. For example, line endings are typically encoded as a single <LF> under Linux, while Windows uses <CR><LF>. So one thought is that there's some sort of ASCII translation that's hosing an otherwise good file. This would possibly explain why you can open it with another program, save it (in the local format) and it works.

    Try handling your data as binary rather than text.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: save Qimage in a file

    The original code tries to write an inherently binary format, through a QTextStream, to a file named "out.png" and expects it not to get mangled. This is broken is several ways.

    You want to write using the QIODevice::write() or writeData() function and a QByteArray containing the original PNG data.

    Edit: I re-read the original post and changed the post entirely

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: save Qimage in a file

    I thought that but disregarded it when I read "but l can open the saved image on the disk with Infrview and other picture viewer", so obviously the picture get saved unmangled.

    Unless of course I read the original post completely wrong.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: save Qimage in a file

    Fragile at best:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QFile in("tawny.png");
    9. if (in.open(QIODevice::ReadOnly)) {
    10. QByteArray ba = in.readAll();
    11. in.close();
    12.  
    13. QFile out("tawny_out.png");
    14. if (out.open(QIODevice::WriteOnly)) {
    15. QTextStream s(&out);
    16. s << ba;
    17. out.close();
    18. }
    19. }
    20.  
    21.  
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    With this image in:
    tawny.jpg
    (I cannot upload the output file: the forum thinks it isn't valid)
    It has produced a broken header in the output:
    Qt Code:
    1. chrisw@newton /tmp/simple_example $ file *.png
    2. tawny_out.png: data
    3. tawny.png: PNG image data, 612 x 816, 8-bit/color RGB, non-interlaced
    4.  
    5. chrisw@newton /tmp/simple_example $ identify tawny*.png
    6. tawny.png PNG 612x816 612x816+0+0 8-bit DirectClass 750KBB 0.000u 0:00.010
    7. identify: improper image header `tawny_out.png' @ error/png.c/ReadPNGImage/2842.
    To copy to clipboard, switch view to plain text mode 
    The file is broken at the very first byte of the 8-byte header by UTF-8 encoding of that byte:
    Qt Code:
    1. chrisw@newton /tmp/simple_example $ od -tx1 -tc -N 8 tawny.png
    2. 0000000 89 50 4e 47 0d 0a 1a 0a
    3. 211 P N G \r \n 032 \n
    4. 0000010
    5. chrisw@newton /tmp/simple_example $ od -tx1 -tc -N 8 tawny_out.png
    6. 0000000 c2 89 50 4e 47 0d 0a 1a
    7. 302 211 P N G \r \n 032
    8. 0000010
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QImage->save(); returns 0
    By ramzes13 in forum Newbie
    Replies: 2
    Last Post: 22nd September 2010, 14:49
  2. Qimage::save returns false in release
    By CCTeam in forum Qt Programming
    Replies: 5
    Last Post: 26th May 2010, 17:35
  3. Can't save QImage
    By hvengel in forum Qt Programming
    Replies: 6
    Last Post: 14th June 2008, 21:07
  4. QImage : trouble with save() method
    By krivenok in forum Qt Programming
    Replies: 12
    Last Post: 3rd May 2006, 20:55
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.