首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用命令行中的gimpfu运行python脚本?

如何使用命令行中的gimpfu运行python脚本?
EN

Stack Overflow用户
提问于 2017-06-08 08:06:12
回答 2查看 12.7K关注 0票数 7

我正在尝试使用gimp 2.8.22函数将pdf转换为jpeg,并且我希望使用windows cmd中的gimpfu库(我已经安装python 3.6.1)使用python脚本来实现这一点。

现在,我不想用示例脚本来做这件事:

代码语言:javascript
复制
#!/usr/bin/env python

# Hello World in GIMP Python

from gimpfu import *

def hello_world(initstr, font, size, color) :
    # First do a quick sanity check on the font
    if font == 'Comic Sans MS' :
        initstr = "Comic Sans? Are you sure?"

    # Make a new image. Size 10x10 for now -- we'll resize later.
    img = gimp.Image(1, 1, RGB)

    # Save the current foreground color:
    pdb.gimp_context_push()

    # Set the text color
    gimp.set_foreground(color)

    # Create a new text layer (-1 for the layer means create a new layer)
    layer = pdb.gimp_text_fontname(img, None, 0, 0, initstr, 10,
                                   True, size, PIXELS, font)

    # Resize the image to the size of the layer
    img.resize(layer.width, layer.height, 0, 0)

    # Background layer.
    # Can't add this first because we don't know the size of the text layer.
    background = gimp.Layer(img, "Background", layer.width, layer.height,
                            RGB_IMAGE, 100, NORMAL_MODE)
    background.fill(BACKGROUND_FILL)
    img.add_layer(background, 1)

    # Create a new image window
    gimp.Display(img)
    # Show the new image window
    gimp.displays_flush()

    # Restore the old foreground color:
    pdb.gimp_context_pop()

register(
    "python_fu_hello_world",
    "Hello world image",
    "Create a new image with your text string",
    "Akkana Peck",
    "Akkana Peck",
    "2010",
    "Hello world (Py)...",
    "",      # Create a new image, don't work on an existing one
    [
        (PF_STRING, "string", "Text string", 'Hello, world!'),
        (PF_FONT, "font", "Font face", "Sans"),
        (PF_SPINNER, "size", "Font size", 50, (1, 3000, 1)),
        (PF_COLOR, "color", "Text color", (1.0, 0.0, 0.0))
    ],
    [],
    hello_world, menu="<Image>/File/Create")

main()

我试图像这样从cmd运行脚本:

代码语言:javascript
复制
gimp-2.8 --no-interface --batch '(python_fu_hello_world RUN-NONINTERACTIVE "Hello" Arial 50 red)' -b '(gimp-quit 1)'

然而,无论我做什么,我总是收到相同的错误消息:

( gimp-2.8: 1020):LibGimpBase-警告**:gimp-2.8: gimp_wire_read():error

编辑:好的,谢谢。我省略了接口语句,我还尝试了一个最简单的例子来找出错误的地方:

代码语言:javascript
复制
#!/usr/bin/env python

# Hello World in GIMP Python

from gimpfu import *

def hello_world():
 gimp.message("Hello, GIMP world!\n")

register(
 "hello_world",
 'A simple Python-Fu "Hello, World" plug-in',
 'When run this plug-in prints "Hello, GIMP world!" in a dialog box.',
 "Tony Podlaski",
 "Tony Podlaski 2017. MIT License",
 "2017",
 "Hello World",
 "",
 [],
 [],
 hello_world,
 menu="<Image>/Filters/HelloWorld",
)

main()

当我从Gimp本身运行它时,这个脚本实际上可以工作,但是当我试图从cmd运行它时,Gimp会打开另一个cmd,上面写着:Error: ( : 1) eval: unbound variable: hello_world

有人知道我在这里错过了什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-08 12:16:59

要运行python脚本,不需要将它注册为插件。IMHO你甚至应该避免这一点,这不必要地污染了Gimp的菜单和过程名称空间。下面是一个示例:

批处理脚本(保存为batch.py):

代码语言: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(infile, infile, run_mode=RUN_NONINTERACTIVE)
        drawable = image.active_layer

        print "File %s loaded OK" % infile
        pdb.plug_in_photocopy(image, drawable,8.,0.8,0.2,0.2)
        pdb.plug_in_cartoon(image, drawable, 7.,0.2)
        outfile=os.path.join('processed',os.path.basename(infile))
        outfile=os.path.join(os.path.dirname(infile),outfile)
        print "Saving to %s" % outfile
        pdb.file_jpeg_save(image, drawable, outfile, outfile, "0.5",0,1,0,"",0,1,0,0)
        print "Saved to %s" % outfile
        pdb.gimp_image_delete(image)


def run(directory):
        start=time.time()
        print "Running on directory \"%s\"" % directory
#   os.mkdir(os.path.join(directory,'processed'))
        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

称之为:

代码语言: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)"

慢动作中的参数:

  • -idf:在没有用户界面的情况下工作,既不加载数据也不加载字体(您可能需要保留字体来加载pdfs)
  • --batch-interpreter python-fu-eval:随-b而来的是Python,而不是脚本-fu。
  • "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')":我们要求Gimp执行的代码,即:
    • import sys;sys.path=['.']+sys.path;:扩展导入路径以包括当前目录
    • import batch;:使用我们的脚本导入文件,该脚本现在位于路径的一部分目录中。
    • batch.run('./images'):调用我们导入的batch模块的run()函数,给出包含要处理的图像的目录的名称。

  • -b "pdb.gimp_quit(1)":另一段python:完成后退出。

注意命令行如何巧妙地使用双引号和单引号将所有参数传递给Gimp,然后传递给Python(*)。是的,您可以在Windows中使用正斜杠作为文件分隔符。

要调试,Windows中的情况比较复杂,因为并不总是有stdout流。可以帮助的东西:

  • 暂时删除-i参数,这样您就可以获得UI,并可能有机会看到消息。
  • 添加--verbose,使Gimp启动辅助控制台窗口。
  • 还有其他一些技巧可以查看列出的这里消息。
  • 您还可以正常启动Gimp,并从Python控制台(Filters>Python> console )运行脚本。您必须扩展路径并“手动”导入文件。

(*)在Linux/OSX中,情况正好相反: shell的单引号,Python的双引号。

票数 24
EN

Stack Overflow用户

发布于 2017-06-08 09:29:24

因为您使用的脚本创建了图像,然后将其显示在窗口中.但是,您正在使用--no-interface标志调用Gimp,因此没有显示窗口。

IMHO从PDF转换到JPEG,ImageMagick的convert命令就会简单得多。

而且,在Windows中有自己的内置Python2.7解释器,因此您必须为该版本编写Python代码,并且不需要再安装另一个解释器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44430081

复制
相关文章

相似问题

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