0

How can I convert an array of bytes to a PNG image (not a JPEG)? I know that the process requires a conversion to BufferedImage as a step.

I encountered this problem while coding for steganography.

Magnus Lind Oxlund
  • 304
  • 1
  • 6
  • 19
  • Are your bytes grayscale or RGB24? – Nayuki Apr 03 '16 at 18:11
  • Which language are you working with ? Java has good file image conversion and writing Libraries. – Arif Burhan Apr 03 '16 at 18:14
  • Can you please verify you're working with Java? You mentioned BufferedImage. And have you actually searched for byte array to BufferedImage? I'd be surprised if you didn't get any results. https://stackoverflow.com/questions/12705385/how-to-convert-a-byte-to-a-bufferedimage-in-java – Reti43 Apr 03 '16 at 18:15
  • Yes i am using java. I have been able to convert bytes array to BufferedImage but not been able to save it as png. The size of my bytes array is 1316890 and width = 1024 and height = 768. The image which i have used is http://vignette3.wikia.nocookie.net/pokemon/images/b/b4/Pokemon_Anime.png/revision/latest?cb=20110118234235 – Abhishek Kumar Apr 04 '16 at 04:29

1 Answers1

1

Let's say you have an array of bytes having length = (image width * image height * 3). First we pack the data into a BufferedImage:

import java.awt.BufferedImage;
byte[] b = (...);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        int off = (y * width + x) * 3;
        int pix = (b[off] & 0xFF) << 16;  // Red component
        pix |= (b[off + 1] & 0xFF) << 8;  // Green component
        pix |= (b[off + 2] & 0xFF) << 0;  // Blue component
        img.setRGB(x, y, pix);
    }
}

And then we write the PNG image file:

import javax.imageio.ImageIO;
ImageIO.write(img, "png", new File("output.png"));
Nayuki
  • 17,911
  • 6
  • 53
  • 80
  • I tried your code but it is not stopping. All details i have is : The size of my bytes array is 1316890 and width = 1024 and height = 768. The image which i have used is http://vignette3.wikia.nocookie.net/pokemon/images/b/b4/Pokemon_Anime.png/revision/latest?cb=20110118234235 – Abhishek Kumar Apr 04 '16 at 04:31
  • My loops do terminate properly. What do you mean it is not stopping? Please give me information about what code you are running and what behavior you observe – Nayuki Apr 04 '16 at 07:06
  • Now i corrected my code a bit and i am getting "ArrayIndexOutOfBound" Exception. The width and height obtained from BufferedImage of initial image is 1024 and 768 respectively and when i converted that BufferedImage to bytes array, the size of array is 1316890. Now i want to convert the byte array back to png image. On using your code i am getting the Exception as i mentioned above. – Abhishek Kumar Apr 04 '16 at 08:15
  • Why is your array 1316890 bytes? How is that related to the dimensions of 1024×768? – Nayuki Apr 04 '16 at 16:02
  • With a bit of debugging, i have reached the error which i was exactly getting from my own code. Nayuki mam it would be a great help if you could have a look at my new posted question http://stackoverflow.com/questions/36407866/converting-byte-array-to-png . It would be a great help. Thankyou – Abhishek Kumar Apr 04 '16 at 16:23
  • Please it would be a great help. – Abhishek Kumar Apr 04 '16 at 17:06