Batch convert .tga textures, preserve alpha channel

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
enderandrew
Posts: 4
Joined: 2017-01-14T23:39:19-07:00
Authentication code: 1151

Batch convert .tga textures, preserve alpha channel

Post by enderandrew »

At the very least, I'm hoping to batch covert .tga textures (for a game mod) into a .png file while preserving the alpha channel (and hoping I save space). I need to be able to batch covert tons of textures at once.

Ideally, what I'd like to do is use ImageMagick to identify if the image has an alpha channel (identify -format '%[channels]' foo.tga), and if it is rgba, then convert to png and preserve the alpha channel. If it is rgb, then convert to jpg and save tons of space.

Is there any way to do this with a batch script?

Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch convert .tga textures, preserve alpha channel

Post by fmw42 »

You will have to identify each image separately. But you can convert all at once if you use mogrify rather than convert. See http://www.imagemagick.org/Usage/basics/#mogrify.

But if you need a conditional based upon identify results, you might as well just write a script loop to do the identify, then the conditional and then the convert.

Please always provide your IM version and platform, since syntax may vary, especially with scripting.

I would suggest you first test if IM can convert tga with transparency to png.
enderandrew
Posts: 4
Joined: 2017-01-14T23:39:19-07:00
Authentication code: 1151

Re: Batch convert .tga textures, preserve alpha channel

Post by enderandrew »

I'm doing this on a Windows 10 machine, but these days you can also run a Linux subsystem and bash on Windows 10. So the script can be bash, powershell, visual basic, you name it. I'm not picky.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch convert .tga textures, preserve alpha channel

Post by fmw42 »

What is your IM version? If not current, then some features may not be available.

You will then need to decide which OS you want to use for scripting?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch convert .tga textures, preserve alpha channel

Post by snibgo »

See http://www.imagemagick.org/script/escape.php

Some useful escapes: %A is True or False according to whether the image has an alpha channel. But an image with an alpha channel may be fully opaque, so you might prefer %[opaque]. %m gives the format: TGA, JPEG etc.
snibgo's IM pages: im.snibgo.com
enderandrew
Posts: 4
Joined: 2017-01-14T23:39:19-07:00
Authentication code: 1151

Re: Batch convert .tga textures, preserve alpha channel

Post by enderandrew »

Sorry, I thought you meant instant messaging when you first said IM. I will download the latest version of ImageMagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch convert .tga textures, preserve alpha channel

Post by fmw42 »

I have been able to convert a transparent image to tga format and then convert that tga image to png. The png does show the same transparency. So that works. So if you want to convert many tga images and they are all in one folder, then you can use mogrify to do that.

Suppose all your images are in folder1. Create a new empty folder2 to hold the png results.

Code: Select all

cd path2/folder1
mogrify -path path2/folder 2 -format png *.tga
That should convert all tga files in folder1 and create new png files in folder2.

If you have to decide on output format between png and jpg depending upon the existence of transparency, then you will need to script a loop over each image, figure out which images have transparency, choose your output format from a condition and then use convert to change image formats.
enderandrew
Posts: 4
Joined: 2017-01-14T23:39:19-07:00
Authentication code: 1151

Re: Batch convert .tga textures, preserve alpha channel

Post by enderandrew »

How do I get an the escape code? From identify?

And I don't care which OS (since I can run bash, powershell, VB, basic .bat files, etc.)

I'm assuming this should be a basic script with pseudo code of :

For every_file_in_directory {
Identify alpha
if alpha then convert to PNG with alpha preservered
else convert to JPG
}
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch convert .tga textures, preserve alpha channel

Post by fmw42 »

In bash, you can create a variable as follows:

make a new empty folder (new_folder) to hold your output

Code: Select all

cd path2/your_current_folder
list=`ls *.tga`
for img in $list; do
name=`convert $img -format "%t" info:`
is_opaque=`convert image -format "%[opaque]" info:`
if [ "$is_opaque" = "true" ]; then
convert $img path2/newfolder/${name}.jpg
else
convert $img path2/new_folder/PNG32:${name}.png
fi
done
See http://www.imagemagick.org/script/escape.php

alternate is

Code: Select all

cd path2/your_current_folder
list=$(ls *.tga}
for img in $list; do
name=$(convert $img -format "%t" info:)
is_opaque=$(convert image -format "%[opaque]" info:)
if [ "$is_opaque" = "true" ]; then
convert $img path2/newfolder/${name}.jpg
else
convert $img path2/new_folder/PNG32:${name}.png
fi
done

If any of your files have spaces in the names, then enclose the outputs in double quotes.

Note the above uses back-ticks not single quotes.

You may want to specify the -quality for the jpg compression. See http://www.imagemagick.org/script/comma ... hp#quality

See also PNG32: at http://www.imagemagick.org/Usage/formats/#png_formats
Post Reply