How to convert XCF to PNG using GIMP from the command-line?

后端 未结 5 1868
误落风尘
误落风尘 2021-02-01 03:02

As part of my build process I need to convert a number of XCF (GIMP\'s native format) images into PNG format. I\'m sure this should be possible using GIMP\'s batch mode, but I h

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 03:39

    Here is my solution utilizing GIMP, GIMP's python-fu and bash. Note that GIMP's python-fu can only be run under a gimp process, not under plain python.

    #!/bin/bash
    set -e
    
    while getopts df: OPTION "$@"; do
        case $OPTION in
        d)
            set -x
            ;;
        f)
            XCFFILE=${OPTARG}
            ;;
        esac
    done
    
    if [[ -z "$XCFFILE" ]]; then
        echo "usage: `basename $0` [-d] -f "
        exit 1
    fi
    
    # Start gimp with python-fu batch-interpreter
    gimp -i --batch-interpreter=python-fu-eval -b - << EOF
    import gimpfu
    
    def convert(filename):
        img = pdb.gimp_file_load(filename, filename)
        new_name = filename.rsplit(".",1)[0] + ".png"
        layer = pdb.gimp_image_merge_visible_layers(img, 1)
    
        pdb.gimp_file_save(img, layer, new_name, new_name)
        pdb.gimp_image_delete(img)
    
    convert('${XCFFILE}')
    
    pdb.gimp_quit(1)
    EOF
    

提交回复
热议问题