好吧,经过多次尝试和错误,我终于想出了如何仅使用 GIMP 将一种文件格式转换为另一种格式。
这是转换成PNG的Script-Fu脚本:
(
let* ((filename "{{filename}}")
(output "{{output}}")
(image (car (gimp-file-load 1 filename filename)))
(drawable (car (gimp-image-get-active-layer image))))
(file-png-save-defaults 1 image drawable output output)
)
其中{{filename}} 是需要转换的输入文件(例如jpeg 文件),{{output}} 是您需要的输出文件(可以只是相同的文件名,但带有PNG 扩展名)
如何运行它:可能还可以改进
gimp -i -n -f -d --batch "{{one-line script-fu}}"
您可以在GIMP online documentation 中找到有关命令行选项的更多信息。
需要改的地方是{{one-line script-fu}},而且必须是……单行!您可以可能使用 cmd 在一个文件中完成所有这些操作(如果您使用 Windows),但对我来说使用 Python 更容易,所以这里是它的脚本:
import subprocess, os
def convert_to_png(file_dds):
#Loads the command to run gimp cli (second code block)
#Note: remove "{{one-line script-fu}}" and leave one space after the --batch
with open("gimp-convert.bat", "r") as f:
main_script = f.read()
#Prepares the Script-Fu script to be run, replacing necessary file names and makes it one-line (the firs code block)
with open("gimp-convert-png.fu", "r") as f:
script = f.read().replace("\n", " ").replace("{{filename}}", file_dds) \
.replace("{{output}}", file_dds[:-3]+"PNG").replace("\\", "\\\\").replace("\"", "\\\"")
subprocess.run(main_script + " \"" + script + "\" --batch \"(gimp-quit 1)\"",
cwd = os.getcwd(),
shell = True)
你应该把你的文件转换成PNG!
我的纹理高档项目需要这个,下面的所有代码都可以找到here。
使用 GIMP 2.10 测试