首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Gimp在命令行上按百分比调整图像大小

使用Gimp在命令行上按百分比调整图像大小
EN

Stack Overflow用户
提问于 2017-03-19 23:04:15
回答 1查看 3.7K关注 0票数 4

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

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

代码语言:javascript
复制
(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)))

我运行的是:

代码语言:javascript
复制
gimp-2.8 -i -b "(resize-image \"image.jpg\" \"image-small.jpg\" 0.5)" -b "(gimp-quit 0)"

但是我得到了这个错误:

代码语言:javascript
复制
(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

EN

回答 1

Stack Overflow用户

发布于 2017-03-20 05:03:30

缩小图像的最短代码:

代码语言:javascript
复制
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中,这给了(振作起来):

代码语言:javascript
复制
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字符串(未经测试)两边使用单引号,事情会更容易:

代码语言:javascript
复制
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()函数,特别是将参数传递给它:

代码语言:javascript
复制
#!/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中)将其称为:

代码语言:javascript
复制
gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42888125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档