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!
Batch convert .tga textures, preserve alpha channel
-
- Posts: 4
- Joined: 2017-01-14T23:39:19-07:00
- Authentication code: 1151
- 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
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.
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.
-
- Posts: 4
- Joined: 2017-01-14T23:39:19-07:00
- Authentication code: 1151
Re: Batch convert .tga textures, preserve alpha channel
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.
- 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
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?
You will then need to decide which OS you want to use for scripting?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Batch convert .tga textures, preserve alpha channel
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.
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
-
- Posts: 4
- Joined: 2017-01-14T23:39:19-07:00
- Authentication code: 1151
Re: Batch convert .tga textures, preserve alpha channel
Sorry, I thought you meant instant messaging when you first said IM. I will download the latest version of ImageMagick.
- 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
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.
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.
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
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.
-
- Posts: 4
- Joined: 2017-01-14T23:39:19-07:00
- Authentication code: 1151
Re: Batch convert .tga textures, preserve alpha channel
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
}
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
}
- 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
In bash, you can create a variable as follows:
make a new empty folder (new_folder) to hold your output
See http://www.imagemagick.org/script/escape.php
alternate is
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
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
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