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

后端 未结 5 1859
误落风尘
误落风尘 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:41

    There are a few ways to go through this - my preferred method is always developing a GIMP-Python plug-in. Script-fu uses GIMP's built-in scheme implementation, which is extremely poor in handling I/O (such as listing files in a directory) - a task which is absolutely trivial in Python.

    SO, since you said you don't want to save new scripts (you could do it because you can add new plug-in and scripts directories on edit->preferences->folder options so that you don't need to write on ~/.gimp*/plugins/)

    Anyway, you can open the python console under filters, and paste this snippet in the interactive prompt.

    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, gimpfu.CLIP_TO_IMAGE)
    
        pdb.gimp_file_save(img, layer, new_name, new_name)
        pdb.gimp_image_delete(img)
    

    This small function will open any image, passed as a file path, flatten it, and save it as a png with the default settings, in the same directory.

    Following the function, you can simply type:

    from glob import glob
    for filename in glob("*.xcf"):
        convert(filename)
    

    On the interactive prompt to convert all .xcf files on the current directory to .png

    (If you don't have gimp-python installed, it may be a separate package on your linux distribution, just install it using your favorite tool - for those under windows, and gimp-2.6, the instructions on this page have have to be followed - it should become easier on gimp 2.8)

    Another way, altogether, applies if your images are sequentially numbered in their filenames, such as myimage_001.xcf, mymage_002.xcf and so on. If they are so arranged you could install GIMP-GAP (gimp animation package) which allows one to apply any filters or actions in such an image sequence.

提交回复
热议问题