如何从命令行使用 gimpfu 运行 python 脚本?

2025-03-04 08:27:00
admin
原创
74
摘要:问题描述:我正在尝试使用 gimp 2.8.22 的函数将 pdf 转换为 jpeg,并且我想使用来自我的 windows cmd 的 gimpfu 库通过 python 脚本来执行此操作(我已经安装了 python 3.6.1)。现在,我还没有尝试使用示例脚本来执行此操作:#!/usr/bin/env py...

问题描述:

我正在尝试使用 gimp 2.8.22 的函数将 pdf 转换为 jpeg,并且我想使用来自我的 windows cmd 的 gimpfu 库通过 python 脚本来执行此操作(我已经安装了 python 3.6.1)。

现在,我还没有尝试使用示例脚本来执行此操作:

#!/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 运行脚本:

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():错误

编辑:好的,谢谢。我省略了接口语句,还尝试了最简单的示例来找出问题所在:

#!/usr/bin/env python

# Hello World in GIMP Python

from gimpfu import *

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

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

有人知道我在这里遗漏了什么吗?


解决方案 1:

要运行 Python 脚本,您无需将其注册为插件。恕我直言,您甚至应该避免这样做,因为这会不必要地污染 Gimp 的菜单和程序名称空间。以下是示例:

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

#!/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

调用方式:

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:无需用户界面,也不加载数据或字体(你可能需要保留字体来加载 pdf)

  • --batch-interpreter python-fu-eval:后面的内容-b都是 Python,而不是脚本

  • "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')":我们要求Gimp执行的代码,即:

    • import sys;sys.path=['.']+sys.path;:扩展导入路径以包含当前目录

    • import batch;:使用我们的脚本导入文件,该文件现在位于路径的一部分的目录中。

    • batch.run('./images'):调用我们导入的模块run()的函数batch,并给它包含要处理的图像的目录的名称。

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

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

在 Windows 中调试会有些复杂,因为并不总是有 stdout 流。以下方法可以提供帮助:

  • 暂时删除该-i参数,以便您获得 UI 并有机会看到消息。

  • 添加--verbose使 Gimp 启动辅助控制台窗口。

  • 此处列出了查看消息的其他技巧。

  • 您也可以正常启动 Gimp 并从 Python-fu 控制台 (Filters>Python-fu>Console) 运行脚本。您必须扩展路径并“手动”导入文件。

(*) 在 Linux/OSX shell 中,可以执行相反的操作:单引号表示 shell,双引号表示 Python。

解决方案 2:

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

我认为,从 PDF 转换为 JPEG,ImageMagick 的convert命令会简单得多。

此外,Windows 中的 Gimp 自带了内置的 Python 2.7 解释器,因此您必须为该版本编写 Python 代码,而无需安装另一个解释器。

解决方案 3:

另一个 Windows 调试技巧...

尝试以下方法来重定向 stdout 和 stderr:

& "C:Program FilesGIMP 2ingimp-console-2.10.exe" -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;sys.stderr=open('C:/tmp/stderr.log', 'a');sys.stdout=open('stdout.log', 'a');import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"

如果在“批次”加载时发生崩溃,这将非常有用。

相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   3983  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   2747  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Freshdesk、ClickUp、nTask、Hubstaff、Plutio、Productive、Targa、Bonsai、Wrike。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在项目管理过程中面临着诸多痛点,如任务分配不...
项目管理系统   82  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Monday、TeamGantt、Filestage、Chanty、Visor、Smartsheet、Productive、Quire、Planview。在当今快速变化的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多项目经理和团队在管理复杂项目时,常...
开源项目管理工具   90  
  本文介绍了以下10款项目管理软件工具:禅道项目管理软件、Smartsheet、GanttPRO、Backlog、Visor、ResourceGuru、Productive、Xebrio、Hive、Quire。在当今快节奏的商业环境中,项目管理已成为企业成功的关键因素之一。然而,许多企业在选择项目管理工具时常常面临困惑:...
项目管理系统   79  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用