Skip to content

Instantly share code, notes, and snippets.

@SaschaWillems
Created June 21, 2020 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaschaWillems/e1fc4b9a28ac8d9b5747922c52abed66 to your computer and use it in GitHub Desktop.
Save SaschaWillems/e1fc4b9a28ac8d9b5747922c52abed66 to your computer and use it in GitHub Desktop.
Convert glTF images to ktx using https://github.com/cdwfs/img2ktx
import json
import argparse
import sys
import os
import subprocess
parser = argparse.ArgumentParser(description='Convert a glTF file textures to the ktx format')
parser.add_argument('--input', type=str, help='glTF file')
parser.add_argument('--output', type=str, help='Filename for updated glTF file')
parser.add_argument('--format', type=str, help='KTX format')
args = parser.parse_args()
if args.input == None:
sys.exit("No input file specified")
if args.input != None:
output_format = args.format
else:
output_format = "RGBA"
input_file = args.input
output_file = ""
if args.output != None:
output_file = args.output
if not os.path.isfile(input_file):
sys.exit("File does not exist")
with open(input_file) as json_file:
data = json.load(json_file)
for image in data['images']:
input_image_file = image['uri']
output_image_file = os.path.splitext(input_image_file)[0] + ".ktx"
if os.path.isfile(input_image_file):
print("Converting %s" % input_image_file)
subprocess.check_output([
"img2ktx",
"-f", output_format,
"-m",
"-o", output_image_file,
input_image_file
])
image['uri'] = output_image_file
if output_file != "":
with open(output_file, 'w') as outfile:
json.dump(data, outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment