1 (edited by reedd 2016-03-26 4:32:03)

Topic: How to set PNG File format options for lossless cropping?

I have some screen shots saved in PNG format that in some cases are the whole screen
rather than the desired program. Using Image Analyzer 1.37 to crop results in a
lower quality image.

Is there a way to set the PNG File format options to have no loss of quality?

Since I am not familiar with the inner workings of PNG, I downloaded the "pngcheck"
program as a way to find some way to express what is happening.

'ex02.png' is the whole screen capture
'ex02TEST1.png' is the cropped image

The simplest outputs:

>pngcheck ex02.png
OK: ex02.png (1440x900, 32-bit RGB+alpha, non-interlaced, 96.9%).

>pngcheck ex02TEST1.png
OK: ex02TEST1.png (899x757, 24-bit RGB, non-interlaced, 97.4%).

The verbose outputs:

>pngcheck -v ex02.png
File: ex02.png (160766 bytes)
  chunk IHDR at offset 0x0000c, length 13
    1440 x 900 image, 32-bit RGB+alpha, non-interlaced
  chunk sRGB at offset 0x00025, length 1
    rendering intent = perceptual
  chunk gAMA at offset 0x00032, length 4: 0.45455
  chunk cHRM at offset 0x00042, length 32
    White x = 0.3127 y = 0.329,  Red x = 0.64 y = 0.33
    Green x = 0.3 y = 0.6,  Blue x = 0.15 y = 0.06
  chunk pHYs at offset 0x0006e, length 9: 4724x4724 pixels/meter (120 dpi)
  chunk IDAT at offset 0x00083, length 65401
    zlib: deflated, 32K window, fast compression
  chunk IDAT at offset 0x10008, length 65524
  chunk IDAT at offset 0x20008, length 29666
  chunk IEND at offset 0x273f6, length 0
No errors detected in ex02.png (9 chunks, 96.9% compression).

>pngcheck -v ex02TEST1.png
File: ex02TEST1.png (53739 bytes)
  chunk IHDR at offset 0x0000c, length 13
    899 x 757 image, 24-bit RGB, non-interlaced
  chunk IDAT at offset 0x00025, length 53682
    zlib: deflated, 32K window, maximum compression
  chunk IEND at offset 0x0d1e3, length 0
No errors detected in ex02TEST1.png (3 chunks, 97.4% compression).

Is there a way to set Image Analyzer to crop without the loss of image quality?

2

Re: How to set PNG File format options for lossless cropping?

PNG uses lossless compression by default, just like ZIP.

Michael Vinther

> software developer <

Re: How to set PNG File format options for lossless cropping?

That's what I thought PNG was all about, so didn't expect any problems.

But, in practice, I found that there is degradation in the image quality due to cropping.

The normal screen shots of just the desired program, rather than the whole screen, have these characteristics:

>pngcheck ex01.png
OK: ex01.png (887x772, 32-bit RGB+alpha, non-interlaced, 98.1%).

>pngcheck -v ex01.png
File: ex01.png (50912 bytes)
  chunk IHDR at offset 0x0000c, length 13
    887 x 772 image, 32-bit RGB+alpha, non-interlaced
  chunk sRGB at offset 0x00025, length 1
    rendering intent = perceptual
  chunk gAMA at offset 0x00032, length 4: 0.45455
  chunk cHRM at offset 0x00042, length 32
    White x = 0.3127 y = 0.329,  Red x = 0.64 y = 0.33
    Green x = 0.3 y = 0.6,  Blue x = 0.15 y = 0.06
  chunk pHYs at offset 0x0006e, length 9: 4724x4724 pixels/meter (120 dpi)
  chunk IDAT at offset 0x00083, length 50761
    zlib: deflated, 32K window, fast compression
  chunk IEND at offset 0x0c6d8, length 0
No errors detected in ex01.png (7 chunks, 98.1% compression).

When I put those into LibreOffice Writer and against the advice in the manual scale them down to around 70%, they look OK.

But when I put the cropped (by Image Analyzer) images into LO Writer, and reduce to around 70% they look horrible.

So somehow the "32-bit RGB+alpha" screen shots have enough quality that Writer's scaling to 70% doesn't mess them up enough to be visible, while the cropped "24-bit RGB" images do get messed up visibly, so some quality is lost in the cropping operation.

The 1440x900 screen shots brought into LO Writer, then cropped by the unwieldy non-graphical methods in LO Writer, then reduced in LO Writer look OK.
(The cropping has to by done by typing in measurements on all four sides, so is very time consuming.)

I hadn't planned to study PNG internals just to get an image into a Word format document, but would prefer to crop them outside of LO Writer,as the manual advises:

Create images that have the exact dimensions required for the document, or use an
appropriate graphics package to scale photographs and large drawings to the required
dimensions. Do not scale images with Writer, even though Writer has tools for doing this,
because the results might not be as clear as you would like.

Re: How to set PNG File format options for lossless cropping?

And a related question would be, in Image Analyzer, in Resize, what Resampling method would give the highest quality PNG output, along with the PNG File format options?

Re: How to set PNG File format options for lossless cropping?

Some quick testing in Image Analyzer and LO Writer found that twiddling with the PNG file format options did get better quality PNGs that didn't get messed up. And, confusing myself further, XYPlorer's details on the files differed from pngcheck's, but matched the reality of my testing:

>pngcheck *.png                                                          XYPlorer Preview 
OK: ex01.png (887x772, 32-bit RGB+alpha, non-interlaced, 98.1%).         ARGB (32-bit)
OK: ex02.png (1440x900, 32-bit RGB+alpha, non-interlaced, 96.9%).        ARGB (32-bit)
OK: ex02TEST1.png (899x757, 24-bit RGB, non-interlaced, 97.4%).          RGB (24-bit)
OK: ex02TEST2.png (891x773, 24-bit RGB, non-interlaced, 97.3%).-- vs --  ARGB (32-bit)        
OK: ex02TEST3.png (893x775, 24-bit RGB, non-interlaced, 97.2%).-- vs --  ARGB (32-bit)

The 'TEST2 and 'TEST3 that XYPlorer reported as "ARGB (32-bit)" went into LO Writer and could be reduced to 70% without visible image degradation. So the IA settings changes did make a difference.

So why is pngcheck not showing me why 'TEST2 and 'TEST3 are better than 'TEST1?

6

Re: How to set PNG File format options for lossless cropping?

The pixel values are untouched by the PNG compression so if something happens with the quality in Writer then it sounds like a bug in Writer. Whether they have an alpha layer or not shouldn't matter for a screenshot.

Regarding resizing in IA I would recommend using the Wiener M=6 method for most kind of images.

Michael Vinther

> software developer <

Re: How to set PNG File format options for lossless cropping?

In IA checking the "Transparent color key" in the PNG File format options is what made 'TEST2 and 'TEST3 both show up differently in XYPlorer than pngcheck and be scaled to 70% in LO Writer without visible degradation. So that setting somehow makes the cropped output more like the original file.

What technical detail are both XYPlorer and LO Writer seeing that pngcheck does not?

(Is there an option in pngcheck to use to detect the difference that Transparent color key" makes?)

Re: How to set PNG File format options for lossless cropping?

The pngcheck -v reports for the two 'TEST files with Transparent checked, and different Scan line option set are:

C:\Utilities\pngcheck>pngcheck -v ex02TEST?.png
File: ex02TEST1.png (53739 bytes)
  chunk IHDR at offset 0x0000c, length 13
    899 x 757 image, 24-bit RGB, non-interlaced
  chunk IDAT at offset 0x00025, length 53682
    zlib: deflated, 32K window, maximum compression
  chunk IEND at offset 0x0d1e3, length 0
No errors detected in ex02TEST1.png (3 chunks, 97.4% compression).

File: ex02TEST2.png (55394 bytes)
  chunk IHDR at offset 0x0000c, length 13
    891 x 773 image, 24-bit RGB, non-interlaced
  chunk tRNS at offset 0x00025, length 6
    red = 0x00ff, green = 0x0000, blue = 0x00ff
  chunk IDAT at offset 0x00037, length 55319
    zlib: deflated, 32K window, maximum compression
  chunk IEND at offset 0x0d85a, length 0
No errors detected in ex02TEST2.png (4 chunks, 97.3% compression).

Then, as a comparison, I did the same cropping operation in the XnView program, without changing any settings, since I don't know what they do, and got an output file where pngcheck agreed with XYPLorer:

>pngcheck "ex02-cropped by XnView.png"
OK: ex02-cropped by XnView.png (890x774, 32-bit RGB+alpha, non-interlaced, 97.8%).
XYPlorer: ARGB (32-bit)

In LO Writer it scaled to 70% and looked OK, visually.

A more detailed reports shows:
>pngcheck -v "ex02-cropped by XnView.png"
File: ex02-cropped by XnView.png (60737 bytes)
  chunk IHDR at offset 0x0000c, length 13
    890 x 774 image, 32-bit RGB+alpha, non-interlaced
  chunk pHYs at offset 0x00025, length 9: 4724x4724 pixels/meter (120 dpi)
  chunk IDAT at offset 0x0003a, length 8192
    zlib: deflated, 32K window, default compression
  chunk IDAT at offset 0x02046, length 8192
  chunk IDAT at offset 0x04052, length 8192
  chunk IDAT at offset 0x0605e, length 8192
  chunk IDAT at offset 0x0806a, length 8192
  chunk IDAT at offset 0x0a076, length 8192
  chunk IDAT at offset 0x0c082, length 8192
  chunk IDAT at offset 0x0e08e, length 3231
  chunk IEND at offset 0x0ed39, length 0
No errors detected in ex02-cropped by XnView.png (11 chunks, 97.8% compression).

Again the common theme is that anything XYPlorer reports as ARGB (32-bit) scales in LO Writer to 70% looking OK.
IA with Transparent set achieves this, but reports differently in pngcheck, while XnView with default settings achieves this.

I only started to look into the details of the PNG format two days ago, and at this point have two practical ways that get the result I want, but without understanding the "why". Any explanation would be gratefully appreciated.

Re: How to set PNG File format options for lossless cropping?

Going back in and changing ex02TEST1.png from being scaled at 70% to 60% resulted in it looking as good as any of the other "good" ones. So there is something quirky about the math in LO Writer's scaling operations. Fortunately the 60% scaling is actually more what I need to get two images per page with the number of lines of text used.

So the technical issue why 'TEST1 differs from 'TEST2 and 'TEST3 hopefully now is moot, as 60% seems to let any cropping with any settings look good.