Browse Source

convert scripts to python for portability

main
Andrew Cassidy 2 months ago
parent
commit
8b5ed4e24b
8 changed files with 105 additions and 57 deletions
  1. +2
    -0
      .gitignore
  2. +11
    -7
      Scripts/README.md
  3. +0
    -36
      Scripts/convert2dds
  4. +30
    -0
      Scripts/dds.py
  5. +0
    -14
      Scripts/dds2png
  6. +22
    -0
      Scripts/ddsck.py
  7. +26
    -0
      Scripts/ddscompress.py
  8. +14
    -0
      Scripts/ddsdecompress.py

+ 2
- 0
.gitignore View File

@ -59,4 +59,6 @@ sysinfo.txt
# Crashlytics generated file
crashlytics-build.properties
# Python files
**/__pycache__

+ 11
- 7
Scripts/README.md View File

@ -1,19 +1,23 @@
# Scripts
Bash scripts for interacting with files
Python scripts for interacting with files
## Installation
All files require bash to be installed (default location of `/bin/bash`).
All files require python3 to be installed.
`convert2dds` and `dds2png` require [ImageMagick](https://imagemagick.org/index.php) and [Nvidia Texture Tools](https://github.com/HeliumProject/NvidiaTextureTools) to be installed and available in your `$PATH`.
`ddscompress`, `ddsdecompress` and `ddsck` require [ImageMagick](https://imagemagick.org/index.php) and [Nvidia Texture Tools](https://github.com/HeliumProject/NvidiaTextureTools) to be installed and available in your `$PATH`.
## Usage
### convert2dds
### ddscompress.py
converts any number of files to `.dds` format, automatically choosing dxt1 or dxt5 depending on if the source image has anything in its alpha channel. Designed to be used with a glob, e.g: `convert2dds *.png`
Converts any number of files to `.dds` format, automatically choosing dxt1 or dxt5 depending on if the source image has anything in its alpha channel. Designed to be used with a glob, e.g: `ddscompress.py *.png`
### dds2png
### ddsdecompress.py
converts any number of files to `.png` from `.dds`. Designed to be used with a glob, e.g: `dds2png *.dds`
Converts any number of files to `.png` from `.dds`. Designed to be used with a glob, e.g: `ddsdecompress.py *.dds`
### ddsck.py
Checks any number of dds files for common issues, including formats not supported by KSP, and DXT5 textures that don't use the alpha channel. Designed to be used with a glob, e.g: `ddsck.py *.dds`

+ 0
- 36
Scripts/convert2dds View File

@ -1,36 +0,0 @@
#!/bin/bash
mkdir -p /tmp/dds > /dev/null
tool="nvcompress"
for file in "$@"
do
echo "$file"
dirname=$(dirname "$file")
basename=$(basename "$file")
extname="${file#${file%.*}}"
filename=$(basename "$basename" "$extname")
convert -flip "$file" "/tmp/dds/$basename"
alpha=$(convert "$file" -resize 1x1 -format "%[fx:int(255*a+.5)]" info:-)
if [ "$tool" == "crunch" ]
then
if [ $alpha -lt 255 ]
then
format="-dxt5"
else
format="-dxt1"
fi
crunch -quiet -file "/tmp/dds/$basename" -fileformat dds $format && rm $file
else
if [ $alpha -lt 255 ]
then
format="-bc3"
else
format="-bc1"
fi
nvcompress $format "/tmp/dds/$basename" "$dirname/$filename.dds" && rm $file
fi
done

+ 30
- 0
Scripts/dds.py View File

@ -0,0 +1,30 @@
import subprocess
import re
def alpha(file):
result = subprocess.run(["convert", file, "-resize", "1x1", "-format", "%[fx:int(255*a+.5)]", "info:-"], capture_output=True)
result.check_returncode()
return int(result.stdout)
def flip(file, output):
result = subprocess.run(['convert', '-flip', file, output], capture_output=True)
result.check_returncode()
def nvcompress(format, file, output):
result = subprocess.run(['nvcompress', format, file, output], capture_output=True)
result.check_returncode()
print(result)
def nvdecompress(file, output):
result = subprocess.run(['nvdecompress', '-format', 'png', file, output], capture_output=True)
result.check_returncode()
def nvinfo(file):
result = subprocess.run(['nvddsinfo', file], capture_output=True)
result.check_returncode()
info = {
"format": re.search(r"FourCC: '(.{4})'", str(result.stdout)).group(1)
}
return info

+ 0
- 14
Scripts/dds2png View File

@ -1,14 +0,0 @@
#!/bin/bash
mkdir -p /tmp/dds > /dev/null
for file in "$@"
do
echo "$file"
dirname=$(dirname "$file")
basename=$(basename "$file")
extname="${file#${file%.*}}"
filename=$(basename "$basename" "$extname")
convert -flip "$file" "$dirname/$filename.png" && rm $file
done

+ 22
- 0
Scripts/ddsck.py View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
import sys
import os
import os.path
import dds
for argv in sys.argv[1:]:
file = os.path.abspath(argv)
output = os.path.splitext(file)[0] + ".png"
info = dds.nvinfo(file)
format = info["format"]
alpha = dds.alpha(file)
if format == "DXT1":
pass
elif format == "DXT5":
if alpha > 254:
print(f'[{argv}]: Image is DXT5 but has no alpha channel')
else:
print(f'[{argv}]: incompatible format')

+ 26
- 0
Scripts/ddscompress.py View File

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import sys
import os
import os.path
import tempfile
import dds
with tempfile.TemporaryDirectory(prefix="dds") as tempDir:
for argv in sys.argv[1:]:
print(f'[{argv}]: converting to dds')
file = os.path.abspath(argv)
tmpOutput = os.path.join(tempDir, os.path.basename(file))
output = os.path.splitext(file)[0] + ".dds"
alpha = dds.alpha(file)
dds.flip(file, tmpOutput)
print(alpha)
if alpha < 255:
format = "-bc3"
else:
format = "-bc1"
print(format)
dds.nvcompress(format, tmpOutput, output)
os.remove(file)

+ 14
- 0
Scripts/ddsdecompress.py View File

@ -0,0 +1,14 @@
#!/usr/bin/env python3
import sys
import os
import os.path
import dds
for argv in sys.argv[1:]:
print(f'[{argv}]: converting from dds')
file = os.path.abspath(argv)
output = os.path.splitext(file)[0] + ".png"
dds.nvdecompress(file, output)
os.remove(file)

Loading…
Cancel
Save