image format conversion - increase in file size

R

Rupert Woodman

Hi all,

I have an application which reads a JPG image and converts it to PNG format,
using the code shown below. This is the simplest illustration I can come up
with (i.e. it's not production code!).
The problem I have is that the initial JPG image is about 1 meg, the PNG
file is about 4.5 meg. Both have the same colour depth, so I'm wondering
why the size difference, and probably more importantly, how to decrease the
size of the resulting PNG.
Applications which I have used which write a PNG file generally give a
slider to say how much compression to apply, so I kind of thought there
would be something which would allow me (as a programmer) to decide how much
compression to use (resulting in longer compression times), but I can't see
anything.

Looking through the Java2D documentation, it kind of assumes you're pretty
au fait with image terminology, which I'm not!
Could anyone explain to me why the increase in size, and what I can do about
reducing the PNG file size (and what the drawbacks of doing so are)?

Many thanks.

My code:

public void convert()
{
String inputFilename="c:/temp/input.jpg";
String outputFilename="c:/temp/output.png";

BufferedImage bi;
try {
bi = ImageIO.read(new File(inputFilename));
ImageIO.write(bi, "png", new File(outputFilename));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
C

Chris Uppal

Rupert said:
The problem I have is that the initial JPG image is about 1 meg, the PNG
file is about 4.5 meg. Both have the same colour depth, so I'm wondering
why the size difference, and probably more importantly, how to decrease
the size of the resulting PNG.

Seems reasonable. JPEG compression is significantly more effective than PNG
compression for many types of image, so you'd expect the JPEG-encoded version
to be smaller.

Note that part of the reason for that extra effectiveness is that JPEG is a
lossy compression scheme, and so decompressing a JPEG and recompressing it with
PNG will get you the worst of both worlds -- not only will you not get the most
effective compression, but your final image will /also/ suffer from the image
artefacts ("ringing") caused by JPEG's lossy nature.

The bottom line is that if the original images were suitable for JPEG
compression (which normally means that they were natural photographic images,
or a good imitation), then /leave/ them as JPEG -- don't mess with the data at
all. If the original images were not suitable for JPEG (e.g. scanned text, or
non-photorealistic computer-generated pictures) then don't encode them as JPEG
in the first place.

I also suspect (but I'm not sure) that the subtle distortions caused by JPEG
encoding/decoding make it /harder/ for PNG to work well. It seems plausible
that a picture which is suitable for PNG will end up smaller if directly
encoded as PNG, than if the raw image were converted to JPEG, then back, then
into PNG.

-- chris
 
G

glennrp

Hi all,

I have an application which reads a JPG image and converts it to PNG format,
using the code shown below. This is the simplest illustration I can come up
with (i.e. it's not production code!).
The problem I have is that the initial JPG image is about 1 meg, the PNG
file is about 4.5 meg. Both have the same colour depth, so I'm wondering
why the size difference, and probably more importantly, how to decrease the
size of the resulting PNG.
Applications which I have used which write a PNG file generally give a
slider to say how much compression to apply, so I kind of thought there
would be something which would allow me (as a programmer) to decide how much
compression to use (resulting in longer compression times), but I can't see
anything.

Looking through the Java2D documentation, it kind of assumes you're pretty
au fait with image terminology, which I'm not!
Could anyone explain to me why the increase in size, and what I can do about
reducing the PNG file size (and what the drawbacks of doing so are)?

JPEG is a much better format for photos. PNG is larger because it is
lossless. When you convert a JPEG to PNG, you waste filespace by
faithfully reproducing the JPEG artifacts in the PNG.

So the best answer is, for photos, use JPEG not PNG.

If you must use PNG, you could reduce the filesize by limiting the
number of colors. 1000 colors might not look too bad, while 256
colors would look like a GIF.

You can also use the "pngcrush" or "optipng" program to optimize the
filesize.

Glenn
 
R

Rupert Woodman

Ahh, yes, of course, that makes sense, thanks. Storing the JPG artifacts
doesn't sound like a great idea. :)

Thanks Glenn.
 
R

Rupert Woodman

Thanks for the detailed response Chris.
The issue you mention about the JPEG distotions sounds entirely feasible to
me I'll have a play around with an uncompressed image at some point and see
if I can validate this.

Thanks very much

Rgds

Rupert.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top