image format conversion - increase in file size

Discussion in 'Java' started by Rupert Woodman, Apr 7, 2007.

  1. 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();
    }
    }
     
    Rupert Woodman, Apr 7, 2007
    #1
    1. Advertising

  2. Rupert Woodman

    Chris Uppal Guest

    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
     
    Chris Uppal, Apr 7, 2007
    #2
    1. Advertising

  3. Rupert Woodman

    Guest

    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
     
    , Apr 7, 2007
    #3
  4. Ahh, yes, of course, that makes sense, thanks. Storing the JPG artifacts
    doesn't sound like a great idea. :)

    Thanks Glenn.


    <> wrote in message
    news:...
    > 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
    >
     
    Rupert Woodman, Apr 7, 2007
    #4
  5. 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" <-THIS.org> wrote in message
    news:46179466$0$760$...
    > 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
    >
    >
     
    Rupert Woodman, Apr 7, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    855
    Ray in HK
    Jun 10, 2005
  2. Raja
    Replies:
    12
    Views:
    24,412
    John Harrison
    Jun 21, 2004
  3. Sameer

    file read -- increase the size of an array

    Sameer, Sep 16, 2003, in forum: C Programming
    Replies:
    14
    Views:
    705
    Eric Sosman
    Sep 17, 2003
  4. durumdara
    Replies:
    0
    Views:
    506
    durumdara
    Oct 16, 2006
  5. Fredrik Lundh
    Replies:
    0
    Views:
    409
    Fredrik Lundh
    Oct 16, 2006
Loading...

Share This Page