使用 Gimp在命令行上按x和y上的百分比调整图像大小



AFAIK,这应该是可能的。 我知道 ImageMagick 的convert使这项任务变得微不足道,但我无法使用 ImageMagick,因此我倾向于 Gimp(在 Windows 上(。

我已经尝试过这个 Guile 脚本:

(define (resize-image filename new-filename scale)
  (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
         (drawable (car (gimp-image-get-active-layer image)))
         (width (gimp-image-width image))
         (height (gimp-image-height image)))
    (gimp-image-scale-full image (* width scale) (* height scale) INTERPOLATION-CUBIC)
    (gimp-file-save RUN-NONINTERACTIVE image drawable new-filename new-filename)
    (gimp-image-delete image)))

我运行:

gimp-2.8 -i -b "(resize-image "image.jpg" "image-small.jpg" 0.5)" -b "(gimp-quit 0)"

但我收到此错误:

(gimp-2.8:22244): LibGimpBase-WARNING **: gimp-2.8: gimp_wire_read(): error
(gimp-2.8:22244): LibGimpBase-WARNING **: gimp-2.8: gimp_wire_read(): error
batch command experienced an execution error:
Error: ( : 1) *: argument 1 must be: number

我的解决方案受到这篇文章的启发:

http://warunapw.blogspot.it/2010/01/batch-resize-images-in-gimp.html

缩小

图像的最短代码:

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)"

最新更新