PDA

View Full Version : Writing Images using QImage from 2D Array



kaydknight
29th December 2007, 03:00
Hi I have a very simple question, suppose that I have a 2D 50 x 50 array consisting of say, pixel values (0-255), and I want to save this as image, how do I actually do it?

I've tried:


QImage image;
QRgb value;

// a 2d array named arr with size 50 x 50 including its values was declared earlier

for (int i=0;i<maxwidth;i++)
{
for (int j=0;j<maxheight;j++)
{
// set the rgb value based on arrs' value
// set them all the same to make em grayscale?
value = qRgb(arr[i][j],arr[i][j],arr[i][j]);
image.setPixel(i,j,value);
}
}

image.save("test.jpg",".jpg");

I suspect there is more than just setting the pixel values using QImage::setPixel. Am I missing something else? Thanks in advance.

kaydknight
29th December 2007, 03:06
Opps, I already got the answer, haha a simple mistake, but just to share, I should use the overloaded constructor of QImage to specify the width, height and format


QImage image(50,50,QImage::Format_RGB32); // use the overloaded constructor
QRgb value;

// a 2d array named arr with size 50 x 50 including its values was declared earlier

for (int i=0;i<maxwidth;i++)
{
for (int j=0;j<maxheight;j++)
{
// set the rgb value based on arrs' value
// set them all the same to make em grayscale?
value = qRgb(arr[i][j],arr[i][j],arr[i][j]);
image.setPixel(i,j,value);
}
}

image.save("test.jpg",".jpg");