使用Gimp在命令行上按x和y的百分比调整图像大小 [英] Resize image by percentage on both x and y on command line with Gimp

查看:140
本文介绍了使用Gimp在命令行上按x和y的百分比调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

AFAIK, it should be possible. I know that convert of ImageMagick makes this task trivial, but I cannot use ImageMagick, therefore I am leaning towards Gimp (on Windows).

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

I have tried with this Guile script:

(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

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

My solution has been inspired by this post:

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中,这可以(让自己振作起来):

So, you can still put everything in a one liner. In Linux, that gives (brace yourself):

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字符串(未经测试):

IIRC, due to how to the Windows shell uses quotes, you have to use double quotes to delimit shell parameters so things are easier if you use single quotes around Python strings (untested):

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上),因此如果需要p处理多个文件,最好编写循环遍历目录中文件的代码。这会变得更大,可能需要将代码保留在模块中。从我以前作为Windows用户的生活中获得的档案中:

However, there is significant overhead when starting Gimp (especially on WIndows), so if you need to process several files, you may be better off writing code that loops over files in a directory. This gets a bit bigger and may want to keep the code in a module. From my archives from a former life as a windows user:

假设您有一个这样的batch.py​​文件(显然,您必须改进process()函数) ,尤其是将参数传递给它:

Assume you have a batch.py file like this (you'll obviously have to improve the process() function, in particular pass parameters to it:

#!/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中,您将其称为:

You would call it (in Windows) as:

gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"

这篇关于使用Gimp在命令行上按x和y的百分比调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆