3

I am using a common format all over application for images as png.Any jpg image uploaded still gets saved as png using code as below.

java.awt.image.BufferedImage bufferedImage = ImageIO.read(jpgImagePAth);

 if(!IsExtensionPng(jpgImagePath)){
        ImageIO.write(bufferedImage, "png", new File(pptFolder, justNamePng));
  }

But this preserves alpha even though it was not there in the jpg so makes a 2MB Image 7MB and 6MB to 16MB . Is there anyway to save png without maintaining the alpha ?

The reason I need to conver to PNG is that later on when I add text on image it looses the actual resolution. I already tried loseless JPEG which didnt fix it.

Pit Digger
  • 9,618
  • 23
  • 78
  • 122
  • The "uploaded images", how would you use them? Depending on this answer I maybe can suggest something... – Martijn Courteaux May 18 '11 at 17:41
  • jpgImagePAth Thats where jpg image is already uploaded. – Pit Digger May 18 '11 at 17:44
  • 1
    I mean, once they are uploaded and stored on the hdd. Is this for a desktop application? If so, explain a bit. Are you going to serve them over internet? How many images are we talking about? Would you use them a lot? – Martijn Courteaux May 18 '11 at 17:51
  • This is a web application.Yes I will be serving over internet. 800KB images becomes 1.5MB after saving as png . – Pit Digger May 18 '11 at 18:32

2 Answers2

2

It's not the alpha channel that is causing the file size to grow, it's the file type. JPG uses lossy compression; PNG is lossless compression. In other words, JPG is throwing out some data to reduce the size of the file. That's why you get to choose a "quality" level when saving to JPG - that determines how much is thrown out.

How do you know you're getting the alpha channel anyway? If you still want PNG and want to be sure you're dropping the alpha channel, set the image type to BufferedImage.TYPE_RGB, e.g.

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_RGB);

You'll have to get the graphics object associated with your new BufferedImage and copy the jpg onto it, then write it out. This question isn't quite the same as yours but has sample code you may find useful.

Paul

Community
  • 1
  • 1
Paul
  • 19,704
  • 14
  • 78
  • 96
  • JPG image are not transparent. So when user uploads JPG i dont want to use alpha I think – Pit Digger May 18 '11 at 17:42
  • +1 because the `BufferedImage` type seems the most likely culprit if there is an alpha channel being stored. Sounds like OP probably has a good reason for choosing PNG. Storage space is cheap, but it does seem silly to store an unused alpha channel. – eaj May 18 '11 at 17:45
  • Yes, I know JPG images are not transparent. I was wondering why you thought the PNGs you were writing contained transparency information. – Paul May 18 '11 at 17:47
  • I mean whatever additional information is added when saving as png. Is there anyway I can turn that off ? – Pit Digger May 18 '11 at 18:01
  • 1
    No, this answer explains that PNGs will ALWAYS be larger then the exact same image as a JPG. If you want to use PNG, you will have to be okay will the larger file sizes. – Jesse Webb May 18 '11 at 19:15
1

I don't know exactly in what situation you are. But I should keep JPEG JPEG. The only advantage of converting JPEG to PNG is wasting hdd space.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 1
    I guess it was in the edit that was made after your answer, but preventing distortion artifacts around letters is a definite advantage of PNG compared to JPEG. – Olaf May 18 '11 at 17:36
  • 2
    @Olaf: Yes that is definitely true. But that is an advantage of PNG, not an advantage of converting from JPEG to PNG. – Martijn Courteaux May 18 '11 at 17:40