View Full Version : save Qimage in a file

18th March 2011, 20:32
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.

QFile file("out.png");
if (!file.open(QIODevice::WriteOnly))
return 0;
QTextStream out(&file);
out << image;

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



20th March 2011, 01:29
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 (http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header).

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


20th March 2011, 20:33
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.

20th March 2011, 23:09
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 ?



20th March 2011, 23:25
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.

20th March 2011, 23:53
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 :)

21st March 2011, 00:13
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.

21st March 2011, 00:42
Fragile at best:

#include <QtGui>
#include <QDebug>

int main(int argc, char *argv[])
QApplication app(argc, argv);

QFile in("tawny.png");
if (in.open(QIODevice::ReadOnly)) {
QByteArray ba = in.readAll();

QFile out("tawny_out.png");
if (out.open(QIODevice::WriteOnly)) {
QTextStream s(&out);
s << ba;

return app.exec();

With this image in:
(I cannot upload the output file: the forum thinks it isn't valid)
It has produced a broken header in the output:

chrisw@newton /tmp/simple_example $ file *.png
tawny_out.png: data
tawny.png: PNG image data, 612 x 816, 8-bit/color RGB, non-interlaced

chrisw@newton /tmp/simple_example $ identify tawny*.png
tawny.png PNG 612x816 612x816+0+0 8-bit DirectClass 750KBB 0.000u 0:00.010
identify: improper image header `tawny_out.png' @ error/png.c/ReadPNGImage/2842.

The file is broken at the very first byte of the 8-byte header by UTF-8 encoding of that byte:

chrisw@newton /tmp/simple_example $ od -tx1 -tc -N 8 tawny.png
0000000 89 50 4e 47 0d 0a 1a 0a
211 P N G \r \n 032 \n
chrisw@newton /tmp/simple_example $ od -tx1 -tc -N 8 tawny_out.png
0000000 c2 89 50 4e 47 0d 0a 1a
302 211 P N G \r \n 032