• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

save image on disk

 
Rupali Raut
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using following code to save an pixel array as an jpg image on disk

public static BufferedImage toImage(byte[] pixels, int w, int h) {
DataBuffer db = new DataBufferByte(pixels, w*h);
WritableRaster raster = Raster.createInterleavedRaster(db,
w, h, w, 1, new int[]{0}, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel cm = new ComponentColorModel(cs, false, false,
Transparency.OPAQUE ,DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, raster, false, null);
}


public static void saveImage(String filename,byte OriginalByte[], int width1,int height1)
{
BufferedImage image1 = toImage(OriginalByte, width1, height1);
try{
ImageIO.write(image1, "jpg", new File(filename));
}
catch(IOException e){}
}

I saved pixel array of 1024(32*32) bytes on disk as image.jpg and pixel array of 800(40*20) bytes as image1.jpg on disk using above code.

But the size of image1.jpg on disk is greater then image.jpg.

why this is happening?

Please help,

Thanks,

Rupali
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
JPEG is a compression format so it is quite possible that a "larger" image size on screen could be shrunk down smaller on disk than a "smaller" display size, depending on the content of the image. Without knowing exactly what the images contain we can only speculate.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPEG applies various algorithms to compress the image. Those may work better on one image than the other (they depend on the image content).

An extreme example would an image that is 1 million pixels large, but all pixels have the same color: that would be much smaller than an image that's 10000 pixels large, yet contains 10000 different pixel values.

PS: Which, as I now see, is just about the same Joe said.
[ June 26, 2008: Message edited by: Ulf Dittmer ]
 
Rupali Raut
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.....

I am studing lossless image compression.

I use a jpg image of 200 kb as input image.
I applied 3 lossless image compression techniques, decompress it,and saved them as .jpg images using above code.
All 3 files are look like original image and each image size is 120 kb.

when I save original 200 kb image as .bmp it gives 300kb bmp image.
also when I save 120 kb image as .bmp it also gives 300kb bmp image.

Also.. when I save original 200 kb image again as .jpg it gives 200kb jpg image.

and when I save 120 kb image as .jpg it gives 130kb jpg image.

then why the size is different 200 and 120 as I am using lossless compresion techniques.(ie.input pixel data is same as output pixel data)

Please help...

Thanks,

Rupali.
[ June 27, 2008: Message edited by: Rupali Raut ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic