Results 1 to 2 of 2

Thread: Writing Images using QImage from 2D Array

  1. #1
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Writing Images using QImage from 2D Array

    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:

    Qt Code:
    1. QImage image;
    2. QRgb value;
    3.  
    4. // a 2d array named arr with size 50 x 50 including its values was declared earlier
    5.  
    6. for (int i=0;i<maxwidth;i++)
    7. {
    8. for (int j=0;j<maxheight;j++)
    9. {
    10. // set the rgb value based on arrs' value
    11. // set them all the same to make em grayscale?
    12. value = qRgb(arr[i][j],arr[i][j],arr[i][j]);
    13. image.setPixel(i,j,value);
    14. }
    15. }
    16.  
    17. image.save("test.jpg",".jpg");
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Writing Images using QImage from 2D Array

    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

    Qt Code:
    1. QImage image(50,50,QImage::Format_RGB32); // use the overloaded constructor
    2. QRgb value;
    3.  
    4. // a 2d array named arr with size 50 x 50 including its values was declared earlier
    5.  
    6. for (int i=0;i<maxwidth;i++)
    7. {
    8. for (int j=0;j<maxheight;j++)
    9. {
    10. // set the rgb value based on arrs' value
    11. // set them all the same to make em grayscale?
    12. value = qRgb(arr[i][j],arr[i][j],arr[i][j]);
    13. image.setPixel(i,j,value);
    14. }
    15. }
    16.  
    17. image.save("test.jpg",".jpg");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. writing CMYK-Files from QImage
    By hilby in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2006, 15:07

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.