Link to home
Start Free TrialLog in
Avatar of martinag
martinag

asked on

Saving PNG: Specify bit-depth and resolution

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
Avatar of GrandSchtroumpf
GrandSchtroumpf

we just talked about that in this thread:
https://www.experts-exchange.com/questions/20925844/Saving-a-newly-Created-Image.html
and concluded that it was better to use this png encoder:
http://catcode.com/pngencoder/
> 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


ASKER CERTIFIED SOLUTION
Avatar of GrandSchtroumpf
GrandSchtroumpf

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of martinag

ASKER

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.)
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.
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
Avatar of CEHJ
>>
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
>>specifying the resolution

More precisely, this is the actual size of the pixels
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. :°)