Automator or Applescript using png files with alpha channel
I have hundred of png files with alpha channel.
I want to suppress alpha channel.
How can i do it using Automator or Applescript ?
Thank you very much.
I have hundred of png files with alpha channel.
I want to suppress alpha channel.
How can i do it using Automator or Applescript ?
Thank you very much.
Some notes to clarify:
The preferred method to remove transparency is:
convert input-photo.png -background white -alpha remove -alpha off output-photo.png
Use the background color of your choice: white, black, any solid color, pattern, etc. For instance, to put a checkerboard image under the background:
composite -compose Dst_Over -tile pattern:checkerboard input-photo.png output-photo.png
The reason to choose a background is that otherwise you are stuck with whatever arbitrary background present in the source image.
Flattening the image will also work for a single image:
convert input-photo.png -background white -flatten -alpha off output-photo.png
However, -flatten will not work with multiple images as you would typically do with batch processing using mogrify. So you would want to use:
mogrify -background white -alpha remove -alpha off -path /Users/Me/Desktop/OutPhotos/ /Users/Me/Desktop/InPhotos/*png
Again, use the background of your choice.
You can use the free command line image processing tool ImageMagick in your Applescripts or Automator workflows, as well as from a Terminal shell.
You can download ImageMagick from http://www.imagemagick.org, but Cactuslab has simplified installation by putting together an installer package. It’s available at http://cactuslab.com/imagemagick/. Download the package and double-click on it. Follow the instructions to install. It will install ImageMagick into /opt/ImageMagick and add it to your $PATH by creating a file in /etc/paths.d/. Restart your computer for the changes to take effect.
The two ImageMagick commands we’re concerned with are “convert” and “mogrify”. Convert is more efficient for multiple and piped operations on the same image file, and mogrify is more efficient for batch processing. Generally speaking, convert will output a file separate from the input file. Unless a path is specified, mogrify will overwrite the input file with the output file. An important distinction.
You can perform various operations on the alpha channel using arguments after either the convert or mogrify command. Two of the available arguments are:
-alpha off - Disables the image's transparency channel. Does not delete or change the existing data, just turns off the use of that data.
-alpha remove - Composite the image over the background color.
Also of use are the -flatten and -background options:
-flatten - overlay all the image layers into a final image and may be used to underlay an opaque color to remove transparency from an image.
-background - sets the background color.
Start off using the convert command with a single image to see the effect and adjust to your liking. Once you’ve achieved the desired outcome, then use the mogrify command to batch process the chosen images.
Before getting into how to use Automator or Applescript in your workflow, use Terminal and the command line to see the effect on a single image. Copy one image to your ~/Desktop. In Terminal change the directory to ~/Desktop by typing the following command and pressing the Enter key:
cd ~/Desktop
Then choose the option you are looking for, -alpha remove for instance, type the following command and press the Enter key:
convert input-photo.png -alpha remove output-photo.png
You can check the alpha channel (transparency) and background in the Preview app, go View > Show Image Background from the menu bar.
Once you’re ready to batch proces, place all the photos you want to convert with the same command into one folder. Copy the folder to your ~/Desktop. Let’s assume you’ve labeled the folder “InPhotos”. It’s prudent to manipulate copies in case something goes amiss. In that event you can copy the folder with the originals again and start over. Create a new empty folder on your ~/Desktop and call it “OutPhotos”. Let’s also assume your home directory is called “Me”. The following command will process the photos from the InPhotos folder and put them in the OutPhotos folder:
mogrify -alpha remove -path /Users/me/Desktop/OutPhotos/ /Users/me/Desktop/InPhotos/*png
According to Apple Technical Note TN2065:
"when you use just a command name instead of a complete path, the shell uses a list of directories (known as your PATH) to try and find the complete path to the command. For security and portability reasons, do shell script ignores the configuration files that an interactive shell would read"
So, you need to use the full path to to ImageMagick commands, unlike in the shell where you can just use the command name.
To batch process using Automator, use the “Run Shell Script” action (note: retain the single space at the beginning of the last line):
/opt/ImageMagick/bin/mogrify \
-alpha remove \
-path /Users/Me/Desktop/OutPhotos/ \
/Users/Me/Desktop/InPhotos/*png
To batch process using Script Editor (Applescript), use the “do shell script” command:
do shell script "/opt/ImageMagick/bin/mogrify -alpha remove -path /Users/pd/Desktop/OutPhotos/ /Users/pd/Desktop/InPhotos/*png"
Further info on ImageMagick:
http://www.imagemagick.org/script/command-line-options.php#alpha
http://www.imagemagick.org/Usage/masking/#remove
http://www.imagemagick.org/index.php
http://www.imagemagick.org/script/command-line-tools.php
http://www.imagemagick.org/script/command-line-options.php
Examples:
The original PNG image:
-alpha off:
-alpha remove:
-background black -flatten:
-background blue -flatten:
-channel alpha -evaluate Divide 2:
-channel alpha -evaluate Divide 2 -background black -flatten:
/*png above should really be /*.png
Btw, worth mentioning again is that mogrify will overwrite source images if not using -path to write to a different folder. That means you don't have to write to a different folder. You can choose to use the source folder (a copy of the original) to write 'in-place':
mogrify -background white -alpha remove -alpha off /Users/Me/Desktop/InOutPhotos/*.png
Automator or Applescript using png files with alpha channel