#!/usr/bin/env python3 import os import subprocess import glob TASKS = [ ("_tmp/kra_tunes_simple_black/drop_shadow_kra_set", "_tmp/kra_tunes_simple_black/drop_shadow_png_kra_set"), ("_tmp/kra_tunes_simple_red/drop_shadow_kra_set", "_tmp/kra_tunes_simple_red/drop_shadow_png_kra_set"), ("_tmp/kra_tunes_simple_black/outer_glow_kra_set", "_tmp/kra_tunes_simple_black/outer_glow_png_kra_set"), ("_tmp/kra_tunes_simple_red/outer_glow_kra_set", "_tmp/kra_tunes_simple_red/outer_glow_png_kra_set"), ("_tmp/kra_tunes_simple_black/inner_glow_kra_set", "_tmp/kra_tunes_simple_black/inner_glow_png_kra_set"), ("_tmp/kra_tunes_simple_red/inner_glow_kra_set", "_tmp/kra_tunes_simple_red/inner_glow_png_kra_set"), ("_tmp/kra_tunes_simple_black/bevel_emboss_kra_set", "_tmp/kra_tunes_simple_black/bevel_emboss_png_kra_set"), ("_tmp/kra_tunes_simple_red/bevel_emboss_kra_set", "_tmp/kra_tunes_simple_red/bevel_emboss_png_kra_set"), ] def main(): # Check if Krita exists try: subprocess.run(["krita", "--version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except FileNotFoundError: print("Error: 'krita' executable not found in your PATH.") print("Please install Krita or run this script in an environment where Krita is available.") return for kra_dir, png_dir in TASKS: if not os.path.exists(kra_dir): print(f"Directory {kra_dir} does not exist. Skipping.") continue kra_files = glob.glob(os.path.join(kra_dir, "*.kra")) if not kra_files: print(f"No .kra files found in {kra_dir}. Skipping.") continue os.makedirs(png_dir, exist_ok=True) print(f"Found {len(kra_files)} KRA files in {kra_dir}. Exporting to {png_dir}...") count = 0 for kra_path in kra_files: filename = os.path.basename(kra_path) png_name = filename.replace(".kra", ".png") png_path = os.path.join(png_dir, png_name) if os.path.exists(png_path): continue print(f" Exporting {filename} -> {png_name}...") cmd = ["krita", kra_path, "--export", "--export-filename", png_path] success = False for attempt in range(2): try: res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) if res.returncode == 0: count += 1 success = True break else: print(f" FAILED to export {filename}: {res.stderr.decode('utf-8', 'ignore')}") break except subprocess.TimeoutExpired: print(f" TIMEOUT on {filename} (attempt {attempt+1}), retrying...") # Kill any lingering krita processes just in case os.system("killall -9 krita 2>/dev/null") if not success: print(f" Skipping {filename} after failure/timeout.") print(f"Done with {kra_dir}. Exported {count} new files.") if __name__ == "__main__": main()