PNG Resizing Transparency Issue

Ask for help and post your question on how to use XnView MP.

Moderators: XnTriq, xnview

User avatar
XnTriq
Moderator & Librarian
Posts: 5936
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: PNG Resizing Transparency Issue

Post by XnTriq »

nintendomaniac64 wrote: Fri Jul 09, 2021 6:28 amI've attached an example of one of the offending PNGs
We're dealing with a truecolor PNG with a (single color) tRNS chunk.

I was able to batch-convert 0.png using Greenfish Icon Editor Pro v3.6 (Windows + Linux) to XnView-compatible 32-bit BMP, PNG, TGA and TIF. Transparency was retained.
User avatar
nintendomaniac64
Posts: 48
Joined: Sun Aug 05, 2007 7:17 pm

Re: PNG Resizing Transparency Issue

Post by nintendomaniac64 »

XnTriq wrote: Fri Jul 09, 2021 2:30 pm I was able to batch-convert 0.png using Greenfish Icon Editor Pro v3.6 (Windows + Linux) to XnView-compatible 32-bit BMP, PNG, TGA and TIF. Transparency was retained.
On both Linux and Windows, the batch conversion function does not allow me to select folders which is a big part of the issue here - the source PNGs are already sorted into a slew of folders and subfolders, and I'd really prefer to retain that folder arrangement as manually recreating that arrangement would be a major pain due to just how many folders and subfolders there are (I'm dealing with thousands of images here).

I've included a second "1.png" problematic image (note that it looks similar, but if you more directly compare it to 0.png then you'll see that it is different) inside of the attached ZIP archive that contains the following simplified version of folder arrangement that I'm dealing with:

/images/folder_a/0.png
/images/folder_b/1.png

So from there I need to be able to simply select the "images" folder and be able to not only batch-convert 0.png and 1.png but have the resulting images be output to the same folder arrangement and/or to the source folders, once again resulting in something like:

/images/folder_a/0.tif
/images/folder_b/1.tif
You do not have the required permissions to view the files attached to this post.
User avatar
XnTriq
Moderator & Librarian
Posts: 5936
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: PNG Resizing Transparency Issue

Post by XnTriq »

@Pierre: Is there any chance you'll implement this function?
http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-3.7 wrote:The following code transforms grayscale images of less than 8 to 8 bits, changes paletted images to RGB, and adds a full alpha channel if there is transparency information in a tRNS chunk. This is most useful on grayscale images with bit depths of 2 or 4 or if there is a multiple-image viewing application that wishes to treat all images in the same way.

Code: Select all

if (color_type == PNG_COLOR_TYPE_PALETTE)
	png_set_palette_to_rgb(png_ptr);

if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
	png_set_gray_1_2_4_to_8(png_ptr);

if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
	png_set_tRNS_to_alpha(png_ptr);
These three functions are actually aliases for png_set_expand(), added in libpng version 1.0.4, with the function names expanded to improve code readability. In some future version they may actually do different things.
User avatar
xnview
Author of XnView
Posts: 36805
Joined: Mon Oct 13, 2003 7:31 am
Location: France

Re: PNG Resizing Transparency Issue

Post by xnview »

XnTriq wrote: Sat Jul 10, 2021 1:30 am @Pierre: Is there any chance you'll implement this function?
:bugconfirmed: Thanks to your detailed description I can reproduce the problem.
Pierre.
User avatar
XnTriq
Moderator & Librarian
Posts: 5936
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: PNG Resizing Transparency Issue

Post by XnTriq »

xnview wrote: Mon Jul 12, 2021 9:36 am:bugconfirmed: Thanks to your detailed description I can reproduce the problem.
Looking forward to v0.98.5 :-D
User avatar
nintendomaniac64
Posts: 48
Joined: Sun Aug 05, 2007 7:17 pm

Re: PNG Resizing Transparency Issue

Post by nintendomaniac64 »

In the meantime, my Linux guru friend at least came up with the following command to effectively do the same, albeit in a single-threaded manner (though in my case I have two main folders, so I was able to at least saturate two cores by running one instance per folder):

Code: Select all

find /path/to/directory -iname \*.png -exec cwebp -lossless -m 6 {} -o {}.webp \;

That however makes the resulting images end in .png.webp rather than just .webp so to achieve that instead there's also the following alternate command (note that it requires one to first install the "parallel" package through your package manager and/or "apt get" or the like):

Code: Select all

find ~/Desktop/test -iname \*.png -print0 | parallel -0 --nice 19 cwebp -lossless -m 6 {} -o {.}.webp \;

Interestingly, despite the lossless compression being set to maximum (6), the resulting file sizes are still a bit larger than what XnView MP creates when similarly set to lossless maximum compression, and this was tested with a 24bit image without transparency. So it's not quite as ideal then, but it'll at least work for me as a stop-gap solution in the meantime.
User avatar
XnTriq
Moderator & Librarian
Posts: 5936
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: PNG Resizing Transparency Issue

Post by XnTriq »

XnTriq wrote: Fri Jun 18, 2021 3:45 amDue to a limitation in XnView's library, an additional action (ImageChange color depth32 bits) is neccessary during batch conversion (prior to ImageResize) when you're dealing with “cheap alpha”. This extra step will ensure that transparency is preserved in all input files upon resizing.
This workaround is no longer needed in the latest release (v0.99.0) :-D

Tested on Windows 10 Pro 20H2 64-bit with these files:

truecolor+alpha.png
truecolor+tRNS.png
paletted+tRNS.png
grayscale+alpha.png

There's still a problem with binary+tRNS.png, though:

binary+tRNS.png
You do not have the required permissions to view the files attached to this post.