Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

image format conversion - increase in file size

Thread view: 
Rupert Woodman - 07 Apr 2007 12:10 GMT
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();
       }
   }
Chris Uppal - 07 Apr 2007 13:10 GMT
> 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 - 07 Apr 2007 14:02 GMT
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.

>> 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
[quoted text clipped - 38 lines]
>
>    -- chris
glennrp@gmail.com - 07 Apr 2007 13:19 GMT
> Hi all,
>
[quoted text clipped - 15 lines]
> 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 - 07 Apr 2007 13:49 GMT
Ahh, yes, of course, that makes sense, thanks.  Storing the JPG artifacts
doesn't sound like a great idea. :-)

Thanks Glenn.

>> Hi all,
>>
[quoted text clipped - 37 lines]
>
> Glenn


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.