File size increases

Am actually reading image from a jpg file and create a stream and write it to a new jpg file format.The code’s below :

juce::File Imagefile(T("D\Water lilies.jpg")); 
juce::File file(T("D\new.JPG")); 
juce::Image* pImage = juce::ImageFileFormat::loadFrom (Imagefile) ; 

juce::FileOutputStream FileOutputStream (file); 

juce::JPEGImageFormat* pJPEGImageFormat = new juce::JPEGImageFormat(); 
pJPEGImageFormat->setQuality(1.0);
pJPEGImageFormat->writeImageToStream(*pImage,FileOutputStream);

The “Water lilies.jpg” file has the size 82kb.
I tried with four Cases,

case1 : with out setting any quality

“new.JPG” file is created has the size 205kb.

case2 : with quality “1.0”

“new.JPG” file is created has the size 205kb.

case3 : with quality “0.0”

“new.JPG” file is created has the size 6kb and image is distorted, might be quality issue. ( i can go with that)

case4 : with quality negative value
( with assumption that the juce documents says that if negative quality is given it takes default quality )

“new.JPG” file is created has the size 205kb.

Am making an assumption that it could be because of the compression algorithm used in “juce::JPEGImageFormat” but am not sure about.

Ok… And what exactly is the problem? All of that sounds absolutely what you’d expect…

You do realise that the quality can be any value between 0 and 1, right?

I don’t want the file size to change, i ve tried values from 0 to 1 but there is always a change in size, is there anyway i can maintain the same size, if i dont modify the image

I don’t think you can do that. The jpeg filter, which does a lossy conversion, is applied every time you save ther image, adding a little bit of quality loss to the output. Even if the file size increases, I think you loose some information.

@jules

wat should i do to keep the file size intact.
i dont want to increase my file size… please help

You can’t expect to decompress and recompress using a lossy codec without changing the file size! It’s just not possible!

It might be possible to read a quality level from a jpeg file, and use that as the level at which you re-save it, but I’ve not written anything to do so. Any jpeg experts know if the format contains something like that? Still wouldn’t be the same size, of course, but it’ll be close.

Or, if you’re not going for speed, a quick hack would be to just repeatedly try saving with different quality settings, and see what size they produce - then you could home in on the value that produces the nearest size to the original. I’d have thought that within 3-4 attempts a binary chop search would get pretty close.