缩小图像的最短代码:
image=pdb.gimp_file_load('/tmp/bigger.png','/tmp/bigger.png')
image.scale(int(image.width*.5),int(image.height*.5))
pdb.gimp_file_save(image, image.active_layer, '/tmp/smaller.png','/tmp/smaller.png')
因此,您仍然可以将所有东西都放在一个衬垫中。在 Linux 中,这给出了(振作起来):
gimp -idf --batch-interpreter python-fu-eval -b 'image=pdb.gimp_file_load("/tmp/bigger.png","/tmp/bigger.png");image.scale(int(image.width*.5),int(image.height*.5));pdb.gimp_file_save(image, image.active_layer, "/tmp/smaller.png","/tmp/smaller.png")' -b 'pdb.gimp_quit(1)'
IIRC,由于 Windows shell 如何使用引号,您必须使用双引号来分隔 shell 参数,因此如果您在 Python 字符串周围使用单引号(未经测试),事情会更容易:
gimp -idf --batch-interpreter python-fu-eval -b "image=pdb.gimp_file_load('/tmp/bigger.png','/tmp/bigger.png');image.scale(int(image.width*.5),int(image.height*.5));pdb.gimp_file_save(image, image.active_layer, '/tmp/smaller.png','/tmp/smaller.png')" -b "pdb.gimp_quit(1)"
然而,启动 Gimp 时会产生很大的开销(尤其是在 WIndows 上),因此如果您需要处理多个文件,最好编写循环遍历目录中文件的代码。这会变得有点大,可能需要将代码保留在模块中。来自我以前作为 Windows 用户的档案:
假设你有一个像这样的batch.py文件(显然你必须改进process()函数,特别是向它传递参数:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import os,glob,sys,time
from gimpfu import *
def process(infile):
print "Processing file %s " % infile
image=pdb.gimp_file_load('/tmp/bigger.png','/tmp/bigger.png');
image.scale(int(image.width*.5),int(image.height*.5));
pdb.gimp_file_save(image, image.active_layer, '/tmp/smaller.png','/tmp/smaller.png')
pdb.gimp_image_delete(image)
def run(directory):
start=time.time()
print "Running on directory \"%s\"" % directory
for infile in glob.glob(os.path.join(directory, '*.jpg')):
process(infile)
end=time.time()
print "Finished, total processing time: %.2f seconds" % (end-start)
if __name__ == "__main__":
print "Running as __main__ with args: %s" % sys.argv
您可以将其称为(在 Windows 中):
gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"