Solved

Saving PNG: Specify bit-depth and resolution

Posted on 2004-03-21
9
2,476 Views
Last Modified: 2007-12-19
Hello,
I'm trying to figure out how to save images. I read in files, create a cropped image and write it out, but the file-size of the result is larger than the original!

This turns out to be because the original bit depth is changed (1 to 24). I see no apparent way of specifying the bit depth of the written image. Also, the resolution changes from 300 dpi to 150, which is a bad thing in this case. I currently use

ImageIO.write(img, "png", file);

to write the image where img is a BufferedImage and file is a File.

I have no problems with downloading packages from Sun or elsewhere.

Certainly, I'd be interested in having the possibility of specifying custom bit depths and resolutions, but a solution to this specific "bit depth: 1 and resolution: 300 dpi"-problem is appreciated and 'enough' for getting the points. :-)

Martin
0
Comment
Question by:martinag
  • 5
  • 2
  • 2
9 Comments
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10644202
we just talked about that in this thread:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20925844.html
and concluded that it was better to use this png encoder:
http://catcode.com/pngencoder/
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10644261
> the original bit depth is changed (1 to 24)
so this is a 2 colors image?
you might be able to reduce the depth to 1 byte using a BufferedImage of type BufferedImage.TYPE_BYTE_INDEXED


0
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 250 total points
ID: 10644421
you can indeed reduce the depth of your image using the BufferedImage type:

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Label;
import java.io.File;

class ImageDepthTest {
 
  public static void main(String[] args) throws Exception {
   
    /* Uncomment one of these lines */
    //int imageType = BufferedImage.TYPE_BYTE_INDEXED;
    //int imageType = BufferedImage.TYPE_BYTE_GRAY;
    int imageType = BufferedImage.TYPE_BYTE_BINARY;

    BufferedImage readImage = ImageIO.read(new File("myimage.png"));
    BufferedImage newImage = new BufferedImage(readImage.getWidth(), readImage.getHeight(), imageType);
    java.awt.Graphics2D destG = newImage.createGraphics();
    destG.drawImage(readImage, 0, 0, new Label());
   
    ImageIO.write(newImage, "png", new File("myimage_2.png"));

  }
 
}
0
 
LVL 4

Author Comment

by:martinag
ID: 10644522
GrandSchtroumpf,
Thank you so much for all your efforts. Turns out I was reading the images sugin RGB. No wonder.... (blushes)

Using your code I get the bit depth 1, but the resolution is still changed from 300 to 150. The size of the files, however, are almost identical. Now, I'm not much of an imaging expert, but could it be that the outputed resolution is indeed 300, only that the software I use to check for some reason reports 150? But the same software says that the originals are 300 dpi (which they *are*). It seems we're near now, but this resolution thing confuses me. Do you know of anything? Thank you!

(The new images are sometimes a little larger, some times a little smaller, but that's fine.)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10644570
this is quite strange.
i don't know what the dpi has to do with the image itself.
dpi is for screens, printers and scanners.
0
 
LVL 4

Author Comment

by:martinag
ID: 10644598
GrandSchtroumpf,
I've now used another, commercial (and thus better ;-) nah, but at least I trust it more) program to check the resolution, and guess what -- it's 300 dpi. Also, after pngcrushing the output (the originals were pngcrushed) all (four) files turn out smaller in file size, so it seems we've got a winning strategy here. ;-)

Thank you very much for your help. The points are all yours.

Hope you'll get a great week,
Martin
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10644687
>>
this is quite strange.
i don't know what the dpi has to do with the image itself.
>>

The png can contain a chunk called 'pHYs' specifying the resolution (in pixels per metre [!]) such that aspect ratios can be adjusted on rendering
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10644694
>>specifying the resolution

More precisely, this is the actual size of the pixels
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10645152
thanks for the info CEHJ.
i always wondered why the GIMP asked for the dpi resolution the first time you run it...
thanks for the points martin.  i'll try to have a good week.
hava a great week too. :°)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
basic hardware to learn oop advanced design patterns 3 74
Modeling a class in java 5 32
@SBGen Method 3 25
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now