469,309 Members | 2,783 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,309 developers. It's quick & easy.

How to specify COMPRESSION level when saving (loseless) PNGs ?

I have been using something like:

public void SaveJPG(Image Image, string FileName, long
QualityLevel_0_100, long ColorDepthLevel)
{
ImageCodecInfo ImageCodecInfoJPG = GetEncoderInfo("image/jpeg");
EncoderParameters EP = new EncoderParameters(2);
EP.Param(0) = new EncoderParameter(Encoder.Quality,
QualityLevel_0_100);
EP.Param(1) = new EncoderParameter(Encoder.ColorDepth,
ColorDepthLevel);
Image.Save(FileName, ImageCodecInfoJPG, EP);
}
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?

My PNG are ways too big (relative to similar jpg's). Can anyone tell
me what am I doing wrong. The following code (invented) is not
working. What's the right way to do it?

public void SavePNG(Image Image, string FileName, long
QualityLevel_0_100, long ColorDepthLevel)
{
ImageCodecInfo ImageCodecInfoPNG = GetEncoderInfo("image/png");

EncoderParameters EP = new EncoderParameters(2);
EP.Param(0) = new EncoderParameter(Encoder.Compression, 100l -
QualityLevel_0_100);
EP.Param(1) = new EncoderParameter(Encoder.ColorDepth,
ColorDepthLevel);
Image.Save(FileName, ImageCodecInfoPNG, EP);
}
-P

Aug 29 '07 #1
7 16053
"pamela fluente" <pa***********@libero.itwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>I have been using something like:

public void SaveJPG(Image Image, string FileName, long
QualityLevel_0_100, long ColorDepthLevel)
{
ImageCodecInfo ImageCodecInfoJPG = GetEncoderInfo("image/jpeg");
EncoderParameters EP = new EncoderParameters(2);
EP.Param(0) = new EncoderParameter(Encoder.Quality,
QualityLevel_0_100);
EP.Param(1) = new EncoderParameter(Encoder.ColorDepth,
ColorDepthLevel);
Image.Save(FileName, ImageCodecInfoJPG, EP);
}
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?
No
My PNG are ways too big (relative to similar jpg's). Can anyone tell
me what am I doing wrong. The following code (invented) is not
working. What's the right way to do it?
Your PNGs are so big because nothing is LOST in the compression. That's why
Jpeg is able to compress so well. Specifying the amount of information
you're willing to LOSE for a smaller size, gives it a lot more flexibility.

Just out of curiousity, what would you think the "compression level" you
want out of PNG to do? It can't use less information than the exact colors
of every pixel compressed as well as its algorithm allows. So what else is
there for it to trade off?

Aug 29 '07 #2
pamela fluente wrote:
[...]
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?

My PNG are ways too big (relative to similar jpg's).
You should not expect to be able to control the compression level in the
way that you are doing.

PNG does support a variety of parameters for controlling how the
compression is done. But different parameters will result in better
results for different input images.

JPEG's compression level is essentially a control over how much
information to throw away. Higher quality discards less information,
lower quality discards more. The more information you throw out, the
smaller the file. .NET provides a simple 0 to 100 parameter to control
how much information is discarded.

PNG's parameters (assuming you have an implementation that provides
access to them) control varying ways that the algorithm can deal with
the input data. A given set of parameters will not always produce the
smallest files; for a given input image, the smallest file is obtained
with a specific set of parameters, but a different input image may
require a different set of parameters.

The only way to optimize (make the smallest) the output of PNG
compression is to try a wide variety of parameters and pick the best
results. This means compressing the image multiple times, and depending
on how optimal you want to get, the number of times could be very large.

You should not, in any case, expect the size of a PNG-compressed file to
approximate the size of a JPEG-compressed file. The two algorithms
serve very different purposes, and aren't really comparable. JPEG
throws out data, which makes it a lot easier to get very small files.
But because it throws out data, it works best with images for which you
won't notice the loss of data, like photographs or elaborate drawings.

For basic images, like bitmaps with text or simple drawings, compressing
using JPEG can produce very unlikable results. The image will have
"artifacts" all over it, causing clean lines to get blurry or distorted.
On the other hand, for simple images like this, PNG's compression,
even a default mode, can often do a pretty good job.

Whether it can do a better job than JPEG depends a lot on the input data
and how you are using each compression technique. But generally
speaking, JPEG isn't going to provide anywhere near the equivalent
quality for simple images like those PNG is well-suited for, especially
when it's used in a way that produces a file significantly smaller than PNG.

Now, all that said...as far as I know, .NET's support for PNG offers
basically no options for controlling the parameters for PNG. So you
don't really have a choice, if you're going to use .NET's PNG support.
There are other options though.

Here's a command-line utility that does allow for control of various
parameters:
http://advsys.net/ken/util/pngout.htm

Here's a .NET program that uses that tool, but via a GUI:
http://brh.numbera.com/software/pnggauntlet/

Here's a web site full of useful information about PNG:
http://www.libpng.org/pub/png/

Here's that web site's introduction of PNG to beginners:
http://www.libpng.org/pub/png/pngintro.html

Hope that helps.

Pete
Aug 29 '07 #3
<"pedrito" <pixbypedrito at yahoo.com>wrote:

<snip>
Just out of curiousity, what would you think the "compression level" you
want out of PNG to do? It can't use less information than the exact colors
of every pixel compressed as well as its algorithm allows. So what else is
there for it to trade off?
Well, potentially time spent doing compression. Most zip tools allow
you to express a preference as to how "hard" you want them to try to
compress, but it's still lossless.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 29 '07 #4
On 29 Ago, 20:48, "pedrito" <pixbypedrito at yahoo.comwrote:
"pamela fluente" <pamelaflue...@libero.itwrote in message

news:11**********************@19g2000hsx.googlegro ups.com...
I have been using something like:
public void SaveJPG(Image Image, string FileName, long
QualityLevel_0_100, long ColorDepthLevel)
{
ImageCodecInfo ImageCodecInfoJPG = GetEncoderInfo("image/jpeg");
EncoderParameters EP = new EncoderParameters(2);
EP.Param(0) = new EncoderParameter(Encoder.Quality,
QualityLevel_0_100);
EP.Param(1) = new EncoderParameter(Encoder.ColorDepth,
ColorDepthLevel);
Image.Save(FileName, ImageCodecInfoJPG, EP);
}
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?

No
My PNG are ways too big (relative to similar jpg's). Can anyone tell
me what am I doing wrong. The following code (invented) is not
working. What's the right way to do it?

Your PNGs are so big because nothing is LOST in the compression. That's why
Jpeg is able to compress so well. Specifying the amount of information
you're willing to LOSE for a smaller size, gives it a lot more flexibility.

Just out of curiousity, what would you think the "compression level" you
want out of PNG to do? It can't use less information than the exact colors
of every pixel compressed as well as its algorithm allows. So what else is
there for it to trade off?- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -
I do not know exactly. I have seen that usually when there is a
"compression", one can specify
a "compression level". So I was wondering whether there are parameters
that need to be adjusted
to control somehow the size. I could not find them

-P

Aug 31 '07 #5
On 29 Ago, 20:59, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
pamela fluente wrote:
[...]
to save JPG with some give given quality. I expected to do the same
with PNG. I know its a loseless format, but I expect to be able to
control the compression level. Is that right or no ?
My PNG are ways too big (relative to similar jpg's).

You should not expect to be able to control the compression level in the
way that you are doing.

PNG does support a variety of parameters for controlling how the
compression is done. But different parameters will result in better
results for different input images.

JPEG's compression level is essentially a control over how much
information to throw away. Higher quality discards less information,
lower quality discards more. The more information you throw out, the
smaller the file. .NET provides a simple 0 to 100 parameter to control
how much information is discarded.

PNG's parameters (assuming you have an implementation that provides
access to them) control varying ways that the algorithm can deal with
the input data. A given set of parameters will not always produce the
smallest files; for a given input image, the smallest file is obtained
with a specific set of parameters, but a different input image may
require a different set of parameters.

The only way to optimize (make the smallest) the output of PNG
compression is to try a wide variety of parameters and pick the best
results. This means compressing the image multiple times, and depending
on how optimal you want to get, the number of times could be very large.

You should not, in any case, expect the size of a PNG-compressed file to
approximate the size of a JPEG-compressed file. The two algorithms
serve very different purposes, and aren't really comparable. JPEG
throws out data, which makes it a lot easier to get very small files.
But because it throws out data, it works best with images for which you
won't notice the loss of data, like photographs or elaborate drawings.

For basic images, like bitmaps with text or simple drawings, compressing
using JPEG can produce very unlikable results. The image will have
"artifacts" all over it, causing clean lines to get blurry or distorted.
On the other hand, for simple images like this, PNG's compression,
even a default mode, can often do a pretty good job.

Whether it can do a better job than JPEG depends a lot on the input data
and how you are using each compression technique. But generally
speaking, JPEG isn't going to provide anywhere near the equivalent
quality for simple images like those PNG is well-suited for, especially
when it's used in a way that produces a file significantly smaller than PNG.

Now, all that said...as far as I know, .NET's support for PNG offers
basically no options for controlling the parameters for PNG. So you
don't really have a choice, if you're going to use .NET's PNG support.
There are other options though.

Here's a command-line utility that does allow for control of various
parameters:http://advsys.net/ken/util/pngout.htm

Here's a .NET program that uses that tool, but via a GUI:http://brh.numbera.com/software/pnggauntlet/

Here's a web site full of useful information about PNG:http://www.libpng.org/pub/png/

Here's that web site's introduction of PNG to beginners:http://www.libpng.org/pub/png/pngintro.html

Hope that helps.

Pete
Tha's all very interesting. But back to my specific issue, what about
within .NET ?

Can I adjust the encoder parameters? And if yes, how? Is there any
example ?

-P

Aug 31 '07 #6
pamela fluente wrote:
Tha's all very interesting. But back to my specific issue, what about
within .NET ?

Can I adjust the encoder parameters? And if yes, how? Is there any
example ?
As I wrote in my post, which you quoted in its entirety:
>Now, all that said...as far as I know, .NET's support for PNG offers
basically no options for controlling the parameters for PNG. So you
don't really have a choice, if you're going to use .NET's PNG support.
Was there something about that statement that confused you?
Aug 31 '07 #7
On 31 Ago, 18:11, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
pamela fluente wrote:
Tha's all very interesting. But back to my specific issue, what about
within .NET ?
Can I adjust the encoder parameters? And if yes, how? Is there any
example ?

As I wrote in my post, which you quoted in its entirety:
Now, all that said...as far as I know, .NET's support for PNG offers
basically no options for controlling the parameters for PNG. So you
don't really have a choice, if you're going to use .NET's PNG support.

Was there something about that statement that confused you?
Yes I understood that. But was a kind of denial :-)

Just wanted to make sure whether, shamefully, there is NO HOPE for
..NET programmers :-((( to output more reasonable PNGs.
-P :-)) Thanks a lot !

Sep 3 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Michael Kennedy [UB] | last post: by
1 post views Thread by M Keeton | last post: by
6 posts views Thread by Chad A. Beckner | last post: by
2 posts views Thread by darrel | last post: by
reply views Thread by Tony K | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.