Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > image format conversion - increase in file size

Reply
Thread Tools

image format conversion - increase in file size

 
 
Rupert Woodman
Guest
Posts: n/a
 
      04-07-2007

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();
}
}


 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      04-07-2007
Rupert Woodman wrote:

> 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


 
Reply With Quote
 
 
 
 
glennrp@gmail.com
Guest
Posts: n/a
 
      04-07-2007
On Apr 7, 7:10 am, "Rupert Woodman" <NoEmail@com> wrote:
> 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

 
Reply With Quote
 
Rupert Woodman
Guest
Posts: n/a
 
      04-07-2007

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

Thanks Glenn.


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Apr 7, 7:10 am, "Rupert Woodman" <NoEmail@com> wrote:
>> 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
>



 
Reply With Quote
 
Rupert Woodman
Guest
Posts: n/a
 
      04-07-2007

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.

"Chris Uppal" <(E-Mail Removed)-THIS.org> wrote in message
news:46179466$0$760$(E-Mail Removed)...
> Rupert Woodman wrote:
>
>> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
How to increase buffer size of a file ? durumdara Python 0 10-16-2006 02:41 PM
Log File size increase, Help needed, Java newbie. mapnolo@gmail.com Java 1 06-10-2005 10:55 AM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM
file read -- increase the size of an array Sameer C Programming 14 09-17-2003 05:09 PM



Advertisments