Page 1 of 1
Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-14T23:43:08-07:00
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!
Re: Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-15T00:17:26-07:00
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.
Re: Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-15T00:19:57-07:00
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.
Re: Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-15T00:20:55-07:00
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?
Re: Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-15T02:37:24-07:00
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.
Re: Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-15T12:26:33-07:00
by enderandrew
Sorry, I thought you meant instant messaging when you first said IM. I will download the latest version of ImageMagick.
Re: Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-15T14:23:00-07:00
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.
Re: Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-15T17:31:29-07:00
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
}
Re: Batch convert .tga textures, preserve alpha channel
Posted: 2017-01-15T18:23:29-07:00
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