PDA

View Full Version : Loading an Image into QImage



jstippey
3rd January 2011, 16:25
I have been trying to load an image into QImage. The original image is a "tif", so I converted to a pnm format (supported by QT). I then call up up the following code



QString fileName = "<FileName>";
QImage Image = QImage(fileName,"PNM");
int width = Image.width();
QVariant wid = width
debugLineEdit->setText(wid.toString());


The width comes out as 0. So I used the

bool Qimage::load() to verify if the image was loading and it is false. Does anyone have any idea why I wouldn't be able to load a PNM Image. Otherwise, if anyone knows a better way to access the information from a TIF format?

Thank you guys

high_flyer
3rd January 2011, 17:25
One of the most common problems in such cases is the fileName resolution.
Usually problems with the wrong path.
You can test that with QFileInfo
If the fileName is not the problem, the next is to make sure the conversion from tif to pnm is a successful one.
So try to open the converted image with a viewer that can open pnm images and see its valid.

bothorsen
3rd January 2011, 17:26
What version are you working with? Qt 4.7 doesn't support PNM files. It does support TIFF files, though, so perhaps you should try loading the original image instead.

See here: http://doc.qt.nokia.com/4.7/qimagereader.html#supportedImageFormats

jstippey
4th January 2011, 13:16
Thank you guys for taking the time to help me out. Currently, I am stuck with QT 3.3.6. However, I think they should be upgrading sometime soon to a QT 4. Don't know if it will be a 4.7 though.

-high_flyer
I thought this could have been the issue so I hard coded the process to a single file that I knew existed and was able to open up using a linux image viewing utility. The file shows up in the image viewer as a PNM file. It says it supports the format listed as PNM in the QT documentation. Unfortunately, I don't have any control over the version of QT I am using ( that would be too easy then). The picture is a 16bit grayscale image that I convert to PNM using the tifftopnm method of the netpbm program from Unix. I don't know if any of this will help out but all the evidence is pointing to the fact that the conversion was successful.

One quick question, In my previous code when I specify "PNM" as the image format. Do I need to specify the type of PNM. Or is my syntax correct?
From QT ImageI/O documentation
The different PNM formats are: PBM (P1 or P4), PGM (P2 or P5), and PPM (P3 or P6).

high_flyer
4th January 2011, 13:40
[sh*** delete by mistake...]
P.S
At worse, you will need to implement your own conversion, but if I am not mistaking the PNM formats are text based, this should be rather easy to do.

jstippey
4th January 2011, 14:36
Thanks everyone for your help. Using the QFileInfo, I found that I could see the file. The issue was that I could not specify "PNM", when doing the Image input format of PNM you should specify whether it is PBM, PGM, or PPM.