Batch convert to transparency?

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

Moderators: XnTriq, helmut, xnview

Post Reply
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Batch convert to transparency?

Post by cday »

@XnTriq:

You are the transparency expert, and I have little knowledge of (and normally little need for) transparency...

Looking at your helpful post a while back Way to produce a transparent canvas? in which both methods supported by XnView require a user step to select the transparency colour, I presume there is no way to batch convert images to transparency using XnView software?
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Batch convert to transparency?

Post by XnTriq »

Hi cday (-:

Do you have a particular use case in mind?
  1. XnView MP: Use mask as alpha (ToolsBatch convert...Actions » Add actionImageAdd mask: mask.png)
    Supported output formats: BMP, ICO, PNG, TGA, …
    This method basically works like the FilterPhotoMasque add-on: You have to prepare a (grayscale) mask which is then “superimposed” on the input images.
  2. XnView Classic: Set the transparency value to palette entry (ToolsOptions...Read/WriteWriteGIF/PNG)
    Supported output formats: GIF, PNG
    The color depth of the input images has to be (reduced to) 8-bit (256 colors) or less for this method to work. You then need to specify the index number of the color you'd like to be (fully) transparent.
  3. XnView Classic: Add alpha (ToolsBatch Processing...Batch Processing...TransformationsImage)
    Supported output formats: BMP, ICO, PNG, TGA, …
    This command adds a uniform alpha channel to an image:
    • 0 = 100% black (R0/G0/B0) mask = fully transparent
    • 1 through 254 = gray mask = semi-transparent (translucent)
    • 255 = 100% white (R255/G255/B255) mask = fully opaque
Related info:
PS@all: Please let me know if I forgot about something!
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Batch convert to transparency?

Post by cday »

Thank you for your customary detailed response: not only possible, but a choice of methods...

My considered use case arises directly from my freely-admitted perfectionist personality trait: I need to prepare a file containing copies of a series of letters in which carefully selected text is highlighted; I could of course easily use the highlighting tool in Adobe Acrobat, but I noticed that it can leave gaps between the lines marked. I thought it might look better if the passages to be marked were highlighted by a single coloured block...

I'd done some tests using layers in Paint.NET which I've worked with a little recently, but then wondered if starting with a transparent foreground image of the letter might produce a better result, or have other advantages. Having converted a letter and now done a further test I'm not sure if the result is actually better.

After posting yesterday, as an afterthought I checked the NConvert help file and found that it might be possible to create a batch file that would convert scans to transparent in a single operation. That could be useful if the need arose regularly, although that isn't my present use case. At a quick look the following options support transparency:

Code: Select all

-transparent value: Transparency index (GIF/PNG)
-transpcolor red green blue: Transparency color (GIF/PNG)

-add_alpha value  : Add alpha channel (24bits)
-set_alpha        : Add alpha by using 'transparent color' (32bits)
I did a quick test with still limited understanding of transparency, I need to invest some time in getting a better understanding one day, and tested the following basic code:

Code: Select all

nconvert -32bits -colors 256 -transpcolor 255 255 255 *.PNG
That seemed to work well for the letters I tested initially which were black and white scans (so no need to use 256 colours really) but failed when I tested a colour scan: the problem seemed to be that when the image was converted to paletted colour, nominally white canvas wasn't converted to pure white even when I reduced the colour depth right down to 8 colours. It seemed to be necessary to determine the actual paletted colour in a test conversion so that that value could be set, the actual value depending on the bit depth valued entered. Even when the background colour was 255 255 255, the paletted value wasn't pure white. That problem doesn't arise with the GUI solutions you posted, I presume.

I imagine that there are probably alternative, and possibly better, NConvert solutions using the available code options above.

In suitable circumstances possibly another way of batch converting images to have transparency, and possibly better as a script that can be reused directly anytime...
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Batch convert to transparency?

Post by XnTriq »

:!:   TION · LISTS AHEAD · CAUTION · LISTS AHEAD · CAUTION · LISTS AHEAD · CAU   :-P
cday wrote:That problem doesn't arise with the GUI solutions you posted, I presume.
Makes no difference. That's the crux of the matter: You'd need a way to tell XnView (or nConvert) to generate a commonSuperPalette” for a batch of files. As a workaround, I use GIF animation software for reducing the colors to a global palette, and then I export the frames to individual images.
Some programs also allow you to map the colors to a predefined palette.

While formats like GIF are limited to single-color transparency, …
  • 2-color palette (1-bit) with single-color transparency
  • 4-color palette (2-bit) with single-color transparency
  • 8-color palette (3-bit) with single-color transparency
  • 16-color palette (4-bit) with single-color transparency
  • 32-color palette (5-bit) with single-color transparency
  • 64-color palette (6-bit) with single-color transparency
  • 128-color palette (7-bit) with single-color transparency
  • 256-color palette (8-bit) with single-color transparency
… PNG is much more versatile:
  • 2-color palette (1-bit) with single-color transparency
  • 4-color palette (2-bit) with single-color transparency
  • 16-color palette (4-bit) with single-color transparency
  • 256-color palette (8-bit) with single-color transparency
  • Grayscale (8-bit) with single-color transparency
  • Grayscale (8-bit) with alpha-channel transparency
  • Truecolor (24-bit) with single-color transparency
  • Truecolor (24-bit) with alpha-channel transparency
I'll have to check if nConvert's transpcolor option can set a single color to transparent in truecolor PNGs. (That's not possible in XnView.)

Since we're dealing with letters, sharing them is probably out of the question, but perhaps you could provide one or more samples with similar specs?
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Batch convert to transparency?

Post by cday »

cday wrote: That [NConvert command line] seemed to work well for the letters I tested initially which were black and white scans ... but failed when I tested a colour scan: the problem seemed to be that when the image was converted to paletted colour, nominally white canvas wasn't converted to pure white even when I reduced the colour depth right down to 8 colours. It seemed to be necessary to determine the actual paletted colour in a test conversion so that that value could be set, the actual value depending on the bit depth valued entered. Even when the background colour was 255 255 255, the paletted value wasn't pure white. That problem doesn't arise with the GUI solutions you posted, I presume.
XnTriq wrote:
cday wrote:That problem doesn't arise with the GUI solutions you posted, I presume.
No difference. That's the crux of the matter: You'd need a way to tell XnView (or nConvert) to generate a common palette for a batch of files. As a workaround, I use GIF animation software…
When a colour image is converted to paletted colour, each colour present in the source image is converted to one or other of the [reduced number] of colours in the palette according to some algorithm, which may or may not be the same algorithm used in other software applications?

Considering the case of converting scans of documents which have a nominally white background, the exact R G B values of the nominally white backgrounds might differ slightly, but one would expect them all to be mapped to the "white" colour in the platette created?

With the GUI solutions it is possible to select the "white" colour in the palette as the colour to be used for transparency, without knowing the actual R G B values of that palette colour, and all the nominally white background images should normally be successfully converted?

With NConvert [sic :wink:] successful conversion seemed to require the actual "white" palette R G B value to be entered in the code, but that value could only be found be found by examining an image that had been [unsuccessfuly] processed... Further, the actual "white" palette colour produced by the algorithm used by NConvert seemed to vary depending on the number of colours in the palette that was created.

So that is a difference between NConvert and GUI solutions? All based on very brief tests, though, and the NConvert conversion algorithm could in principle produce different "white" palette values depending on the actual rather than nominal number of colours in the source image, although I imagine that probably isn't the case.

Regarding test files, I don't think that there was anything at all special about the small number of files I used. I had a number of letters scanned using a multifuntion device in the library, with which I have some familiarity, but wanted to use the sheet feeder and was feeling a bit lazy so I asked a member of staff to scan them in colour to JPEGs. When I checked them later, I found that they had actually been scanned to black and white (possibly because the scanner had correctly detected that there was no colour in those letters and overridden the colour setting, but I'm not sure about that).

I therefore tested those files first with good results once I got the right code; considering the discussion above, possibly the -colors 256 setting was required to obtain success with the R G B 255 255 255 setting I entered, just luck possibly. Because I wanted to be able to add colour highlighting to some images, I then converted them to 24-bit (in any one of three possible programs, I don't recall which one) and tested those without initial success, then additionally tried using a Levels adjustment (in XnView MP) to whiten the background of an image slightly, and started monitoring the actual background R G B using the XnView Classic 'Display Colour Information' tool.

Using those files, but any other would probably do, I then traced the problem I was having to the apparent fact that the nominally "white" palette colour created by the NConvert code wasn't 255 255 255, the actual value possibly depended on the number of palette colours selected, and it seemed to be necessary to enter the exact R G B value to obtain a successful conversion to transparency.

All rather provisional and based on only a small number of tests, but once the details have been confirmed an NConvert batch file looks like a potentially useful one-step way of performing regular transparency conversions as directly as possible...
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Batch convert to transparency?

Post by XnTriq »

cday wrote:When a colour image is converted to paletted colour, each colour present in the source image is converted to one or other of the [reduced number] of colours in the palette according to some algorithm, which may or may not be the same algorithm used in other software applications?
Exactly :-) The process is called color quantization. Ximagic Quantizer gives you a taste of a wide variety of methods.
cday wrote:Considering the case of converting scans of documents which have a nominally white background, the exact R G B values of the nominally white backgrounds might differ slightly, but one would expect them all to be mapped to the "white" colour in the platette created?
That's true if you're converting to binary (1-bit monochrome). With higher color depths (2- to 8-bit), the resulting palette might contain several “whitish” colors, depending on factors like color quantization (Standard or LIBIMAGEQUANT) and dithering (None or Floyd–Steinberg or Bayer) method etc.
Some programs (like Photoshop) allow the user to specify colors (e.g. pure white) which have to be included in the palette.

In XnView, we have to find the color's palette index, …
  1. ViewDisplay Colour Information
  2. ImageEdit Palette...
… which is then entered into the field for Set the transparency value to palette entry (ToolsOptions...Read/WriteWriteGIF/PNG).

As you've already pointed out, nConvert allows the user to specify the transparent color by palette index or by RGB value:

Image
logo.gif
nconvert.exe -transparent 31 logo.gif
nconvert.exe -transparent 31 logo.gif
transparent.gif (9.65 KiB) Viewed 4838 times
nconvert.exe -transpcolor 255 255 255 logo.gif
nconvert.exe -transpcolor 255 255 255 logo.gif
transpcolor.gif (9.65 KiB) Viewed 4838 times
I did a binary compare with AptDiff and FC: transparent.gif and transpcolor.gif are absolutely identical.
User avatar
XnTriq
Moderator & Librarian
Posts: 6336
Joined: Sun Sep 25, 2005 3:00 am
Location: Ref Desk

Re: Batch convert to transparency?

Post by XnTriq »

Addendum regarding global palette…
Photoshop User Guide (Convert an image to another color mode → [url=http://helpx.adobe.com/photoshop/using/converting-color-modes.html#conversion_options_for_indexed_color_images]Conversion options for indexed-color images[/url]) wrote:Master (Perceptual)
Creates a custom palette by giving priority to colors for which the human eye has greater sensitivity. Applies when you have multiple documents open; takes all open documents into account.
Master (Selective)
Creates a color table similar to the Perceptual color table, but favoring broad areas of color and the preservation of web colors. This option usually produces images with the greatest color integrity. Applies when you have multiple documents open; takes all open documents into account.
Master (Adaptive)
Creates a palette by sampling the colors from the spectrum appearing most commonly in the image. For example, an RGB image with only the colors green and blue produces a palette made primarily of greens and blues. Most images concentrate colors in particular areas of the spectrum. To control a palette more precisely, first select a part of the image containing the colors you want to emphasize. Photoshop weights the conversion toward these colors. Applies when you have multiple documents open; takes all open documents into account.
… and transparency in PNG:
Post Reply