For this tutorial, we’ll be writing a Windows command-line batch script to extract an alpha mask from a PNG image using ImageMagick.

If you don’t know already, PNG images don’t contain a traditional alpha mask in its own channel, instead, the transparent pixels are, in a way, part of the image itself.

Below is the source image on the left and the output Alpha Mask on the right.

Extract an Alpha Mask from a PNG image
Detecting ImageMagick

The following section provides a rigorous way to detect ImageMagick installed on any Windows computer.

While you could specify a static location for ImageMagick, this will mean that everytime you update or reinstall ImageMagick, the script would have to be updated to point to the new location, as IM put the version number within the actual folder name.

Over time, this would get tiresome over many scripts, so I developed a way to do verbosely check for ImageMagick’s install folder, and set it as a variable.

We also have to check whether or not the user is on a 64 or 32bit system. The one and only downside to this checker is if the user has installed the 32bit version of ImageMagick on a 64bit system.

REM Rigorous check for mogrify.
REM Search for mogrify in the progfiles W6432 Tree (The x64 C:\Program Files\ folder on a 64bit machine)

IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (GOTO X64) ELSE GOTO X86

REM Take the highway to the danger zone [we didn't detect any architecture]
GOTO ARCHERR

:X86
ECHO Windows 32bit detected
SET PF=%ProgramFiles%

GOTO CONT1

:X64
ECHO Windows 64bit detected
ECHO(
SET PF64=%ProgramFiles(x86)%
SET PF=%ProgramW6432%

GOTO CONT1

:CONT1
REM Check that mogrify exists at all.
REM NOTE the second SET and the IF statement nested in the FOR loop.
REM We're assuming that the user has installed the 32bit version of IM on their 32bit machine, and 64bit IM on their 64bit machine, duh

REM Push into the current directory and start the FOR loop
REM then check IF the name is equal to "mogrify.exe"
REM Finally if one is found, SET MOGEXIST to TRUE, grab its directory and SET it as IMDIR and GOTO CONT2, the next step, breaking out of the loop
ECHO Searching for mogrify.exe in %PF% . . .
ECHO(

PUSHD "%PF%" &&(
 FOR /R "." %%i IN (*.exe) DO (
 IF /i "%%~nxi"=="mogrify.exe" (
 SET MOGEXIST=TRUE
 SET IMDIR=%%~dpi
 GOTO:CONT2
 )
 )
)

REM Old SET
REM SET IMDIR=C:\Program Files\ImageMagick-6.8.7-Q16

REM Flowed from mogrify check loop, this means we didn't find her
GOTO NOMOG

:CONT2
REM Now broken out of mogrify check loop, pop back out of %PFFolder%
POPD

REM MOGEXIST will only be defined if mogrify.exe is found in a subfolder of %PFFolder% (aka %PF% aka %ProgramFiles%).
REM Otherwise, it will skip our next SETMOG function and goto NOMOG error block
If Defined MOGEXIST (GOTO SETMOG) else (
 GOTO NOMOG)
Extract Alpha

The actual conversion code is quite simple.

We’ll use SET to create macro-like variables for both the convert.exe (%APP%) and the option parameters (%OPTS%) that are used to extract the alpha mask.

:SETMOG

SET APP=convert.exe
SET OPTS=-alpha extract

"%IMDIR%\%APP%" "input.png" %OPTS% "output_alpha.jpg"
Drag ’n Drop version

The following code is designed for dragging and dropping PNG files onto the script to be processed instead of defining file locations.

One of the caveats of Windows command-line is that there can only be a total of 9 files dragged and dropped in this way, and the reasons for this are not documented from MS very well.

With drag ‘n drop scripts, generally speaking, “0” (zero) is the file or location where the batch script was launched from.

1-9 will be the actual files dropped onto the script.

We’ll quickly break down the “%~dpnx1” string.

D = Drive

P = Path

N = fileName

X = eXtension

1 = The first file drag ‘n dropped.

The cool thing about this format is you can mix and match the parameters.

so “%~nx1” will just be the filename + extension of the first file, or “%~dp1” will just be the drive and path of the first file.

Also, you will notice that the output file is missing the “X” from the string. This is so that we can put a suffix on the name, and define our own extension.

:SETMOG

SET APP=convert.exe
SET OPTS=-alpha extract

"%IMDIR%\%APP%" "%~dpnx1" %OPTS% "%~dpn1_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx2" %OPTS% "%~dpn2_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx3" %OPTS% "%~dpn3_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx4" %OPTS% "%~dpn4_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx5" %OPTS% "%~dpn5_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx6" %OPTS% "%~dpn6_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx7" %OPTS% "%~dpn7_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx8" %OPTS% "%~dpn8_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx9" %OPTS% "%~dpn9_alpha.jpg"
Bonus: Extract Alpha keeping Alpha

Here’s a bit of a bonus to this script.

In some rarer cases, you may want to keep the alpha information while still creating a standard RGB image from the alpha pixels of the source PNG.

Basically, this will “copy” the alpha pixels to both RGB and A channels, resulting in an R8G8B8A8 image. There is only a small amount of image extensions that support an alpha channel. For ease of use, and because this bonus script has the most use in 3D design, we’ll use TGA as our output file type.

:SETMOG

SET APP=convert.exe
SET OPTS=-alpha extract -alpha on

"%IMDIR%\%APP%" "input.png" %OPTS% "output.tga"
Putting it all together

Extract Alpha code

@ECHO OFF

@SETLOCAL enableextensions
@CD /d "%~dp0"

REM We need to dynamically find mogrify.exe and use its location as a variable
REM This will future proof this converter so long as IM don't change the name of "mogrify.exe"

REM We also need to check what architecture the user has, and whereabouts IM is installed.

REM Rigorous check for mogrify.
REM Search for mogrify in the progfiles W6432 Tree (The x64 C:\Program Files\ folder on a 64bit machine)

IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (GOTO X64) ELSE GOTO X86

REM Take the highway to the danger zone [we didn't detect any architecture]
GOTO ARCHERR

:X86
ECHO Windows 32bit detected
SET PF=%ProgramFiles%

GOTO CONT1

:X64
ECHO Windows 64bit detected
ECHO(
SET PF64=%ProgramFiles(x86)%
SET PF=%ProgramW6432%

GOTO CONT1

:CONT1
REM Check that mogrify exists at all.
REM NOTE the second SET and the IF statement nested in the FOR loop.
REM We're assuming that the user has installed the 32bit version of IM on their 32bit machine, and 64bit IM on their 64bit machine, duh

REM Push into the current directory and start the FOR loop
REM then check IF the name is equal to "mogrify.exe"
REM Finally if one is found, SET MOGEXIST to TRUE, grab its directory and SET it as IMDIR and GOTO CONT2, the next step, breaking out of the loop
ECHO Searching for mogrify.exe in %PF% . . .
ECHO(

PUSHD "%PF%" &&(
 FOR /R "." %%i IN (*.exe) DO (
 IF /i "%%~nxi"=="mogrify.exe" (
 SET MOGEXIST=TRUE
 SET IMDIR=%%~dpi
 GOTO:CONT2
 )
 )
)

REM Old SET
REM SET IMDIR=C:\Program Files\ImageMagick-6.8.7-Q16

REM Flowed from mogrify check loop, this means we didn't find her
GOTO NOMOG

:CONT2
REM Now broken out of mogrify check loop, pop back out of %PFFolder%
POPD

REM MOGEXIST will only be defined if mogrify.exe is found in a subfolder of %PFFolder% (aka %PF% aka %ProgramFiles%).
REM Otherwise, it will skip our next SETMOG function and goto NOMOG error block
If Defined MOGEXIST (GOTO SETMOG) else (
 GOTO NOMOG)

:SETMOG

SET APP=convert.exe
SET OPTS=-alpha extract

"%IMDIR%\%APP%" "input.png" %OPTS% "output_alpha.jpg"

:NOMOG
REM DONE
PAUSE
EXIT

Drag ‘n Drop code

@ECHO OFF

@SETLOCAL enableextensions
@CD /d "%~dp0"

REM We need to dynamically find mogrify.exe and use its location as a variable
REM This will future proof this converter so long as IM don't change the name of "mogrify.exe"

REM We also need to check what architecture the user has, and whereabouts IM is installed.

REM Rigorous check for mogrify.
REM Search for mogrify in the progfiles W6432 Tree (The x64 C:\Program Files\ folder on a 64bit machine)

IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (GOTO X64) ELSE GOTO X86

REM Take the highway to the danger zone [we didn't detect any architecture]
GOTO ARCHERR

:X86
ECHO Windows 32bit detected
SET PF=%ProgramFiles%

GOTO CONT1

:X64
ECHO Windows 64bit detected
ECHO(
SET PF64=%ProgramFiles(x86)%
SET PF=%ProgramW6432%

GOTO CONT1

:CONT1
REM Check that mogrify exists at all.
REM NOTE the second SET and the IF statement nested in the FOR loop.
REM We're assuming that the user has installed the 32bit version of IM on their 32bit machine, and 64bit IM on their 64bit machine, duh

REM Push into the current directory and start the FOR loop
REM then check IF the name is equal to "mogrify.exe"
REM Finally if one is found, SET MOGEXIST to TRUE, grab its directory and SET it as IMDIR and GOTO CONT2, the next step, breaking out of the loop
ECHO Searching for mogrify.exe in %PF% . . .
ECHO(

PUSHD "%PF%" &&(
 FOR /R "." %%i IN (*.exe) DO (
 IF /i "%%~nxi"=="mogrify.exe" (
 SET MOGEXIST=TRUE
 SET IMDIR=%%~dpi
 GOTO:CONT2
 )
 )
)

REM Old SET
REM SET IMDIR=C:\Program Files\ImageMagick-6.8.7-Q16

REM Flowed from mogrify check loop, this means we didn't find her
GOTO NOMOG

:CONT2
REM Now broken out of mogrify check loop, pop back out of %PFFolder%
POPD

REM MOGEXIST will only be defined if mogrify.exe is found in a subfolder of %PFFolder% (aka %PF% aka %ProgramFiles%).
REM Otherwise, it will skip our next SETMOG function and goto NOMOG error block
If Defined MOGEXIST (GOTO SETMOG) else (
 GOTO NOMOG)

:SETMOG

SET APP=convert.exe
SET OPTS=-alpha extract

"%IMDIR%\%APP%" "%~dpnx1" %OPTS% "%~dpn1_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx2" %OPTS% "%~dpn2_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx3" %OPTS% "%~dpn3_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx4" %OPTS% "%~dpn4_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx5" %OPTS% "%~dpn5_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx6" %OPTS% "%~dpn6_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx7" %OPTS% "%~dpn7_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx8" %OPTS% "%~dpn8_alpha.jpg"
"%IMDIR%\%APP%" "%~dpnx9" %OPTS% "%~dpn9_alpha.jpg"

:NOMOG
REM DONE
PAUSE
EXIT

Bonus: Extract Alpha keeping Alpha code

@ECHO OFF

@SETLOCAL enableextensions
@CD /d "%~dp0"

REM We need to dynamically find mogrify.exe and use its location as a variable
REM This will future proof this converter so long as IM don't change the name of "mogrify.exe"

REM We also need to check what architecture the user has, and whereabouts IM is installed.

REM Rigorous check for mogrify.
REM Search for mogrify in the progfiles W6432 Tree (The x64 C:\Program Files\ folder on a 64bit machine)

IF "%PROCESSOR_ARCHITECTURE%"=="AMD64" (GOTO X64) ELSE GOTO X86

REM Take the highway to the danger zone [we didn't detect any architecture]
GOTO ARCHERR

:X86
ECHO Windows 32bit detected
SET PF=%ProgramFiles%

GOTO CONT1

:X64
ECHO Windows 64bit detected
ECHO(
SET PF64=%ProgramFiles(x86)%
SET PF=%ProgramW6432%

GOTO CONT1

:CONT1
REM Check that mogrify exists at all.
REM NOTE the second SET and the IF statement nested in the FOR loop.
REM We're assuming that the user has installed the 32bit version of IM on their 32bit machine, and 64bit IM on their 64bit machine, duh

REM Push into the current directory and start the FOR loop
REM then check IF the name is equal to "mogrify.exe"
REM Finally if one is found, SET MOGEXIST to TRUE, grab its directory and SET it as IMDIR and GOTO CONT2, the next step, breaking out of the loop
ECHO Searching for mogrify.exe in %PF% . . .
ECHO(

PUSHD "%PF%" &&(
 FOR /R "." %%i IN (*.exe) DO (
 IF /i "%%~nxi"=="mogrify.exe" (
 SET MOGEXIST=TRUE
 SET IMDIR=%%~dpi
 GOTO:CONT2
 )
 )
)

REM Old SET
REM SET IMDIR=C:\Program Files\ImageMagick-6.8.7-Q16

REM Flowed from mogrify check loop, this means we didn't find her
GOTO NOMOG

:CONT2
REM Now broken out of mogrify check loop, pop back out of %PFFolder%
POPD

REM MOGEXIST will only be defined if mogrify.exe is found in a subfolder of %PFFolder% (aka %PF% aka %ProgramFiles%).
REM Otherwise, it will skip our next SETMOG function and goto NOMOG error block
If Defined MOGEXIST (GOTO SETMOG) else (
 GOTO NOMOG)

:SETMOG

SET APP=convert.exe
SET OPTS=-alpha extract -alpha on

"%IMDIR%\%APP%" "input.png" %OPTS% "output.tga"

:NOMOG
REM DONE
PAUSE
EXIT