问题描述
作为构建过程的一部分,我需要将许多XCF(GIMP的本机格式)图像转换为PNG格式.我确定使用GIMP的批处理模式应该可以实现,但是我已经忘记了以前知道的所有脚本.
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 have forgotten all of the script-fu I used to know.
我的输入图像有多层,因此我需要批处理模式等效于合并可见层",然后是另存为PNG".另外请注意,我无法在~/.gimp*/scripts/
中安装任何内容,我需要一个自包含的命令行,或者在我的源代码树中安装脚本的方法.
My input images have multiple layers, so I need the batch mode equivalent of "merge visible layers" followed by "save as PNG". Also note that I can't install anything in ~/.gimp*/scripts/
— I need a self-contained command-line, or a way to install scripts in my source tree.
请注意,尽管这类似于此问题,我还有其他限制,我需要使用GIMP来完成此操作.我尝试了当前版本的ImageMagick,它破坏了我的测试图像.
Note that while this is similar to this question, I have the additional constraint that I need this to be done using GIMP. I tried the current version of ImageMagick and it mangles my test images.
推荐答案
有几种方法可以解决此问题-我的首选方法始终是开发GIMP-Python插件. Script-fu使用GIMP的内置方案实现,该实现在处理I/O(例如在目录中列出文件)方面非常差-这在Python中绝对是微不足道的.
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.
所以,既然您说过您不想保存新脚本(之所以可以这样做,是因为您可以在edit-> preferences-> folder选项上添加新的插件和脚本目录,因此您无需保存在〜/.gimp */plugins/)上写
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/)
无论如何,您可以在过滤器下打开python控制台,并将此代码段粘贴到交互式提示中.
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)
此小功能将打开任何图像,并将其作为文件路径传递,展平并使用默认设置将其另存为png在同一目录中.
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)
在交互式提示下,将当前目录中的所有.xcf文件转换为.png
On the interactive prompt to convert all .xcf files on the current directory to .png
(如果您未安装gimp-python,则它可能是Linux发行版上的单独软件包,只需使用您喜欢的工具进行安装-对于Windows和gimp-2.6下的工具,请参见必须遵循的页面-它应该成为在gimp 2.8上更容易)
(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)
如果图像在文件名中依次编号,例如myimage_001.xcf,mymage_002.xcf等,则完全适用另一种方法.如果安排得当,则可以安装GIMP-GAP(gimp动画程序包),该程序包可以按这种图像顺序应用任何滤镜或动作.
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.
这篇关于如何从命令行使用GIMP将XCF转换为PNG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!