首页 > 解决方案 > 当前图像中的 Gimp python 脚本

问题描述

我正在尝试使用 GIMP 进行图像处理,但我在第一个障碍中失败了:

在 GIMP 中加载图像后,我想运行一个注册脚本来运行一个函数 - 只需将整个图像旋转 90 度,然后显示一条消息。

#!/usr/bin/env python
 
import os
import sys
import time
from gimpfu import *

def simple_test():
  num = 90
  image = gimp.pdb # not sure if this is calling the active image
  drawable = pdb.gimp_image_get_active_layer(image)

  rotate_it(image, 90)


def rotate_it(image, deg):
  msg = "simple_test!! " + str(deg) + "\n"
  pdb.gimp_image_rotate(gimp.pdb, num)
  gimp.message(msg)

 
register(
    "simple_test",
    "A simple Python-Fu plug-in",
    "When run this plug-in rotates an image 90 degrees",
    "Ghoul Fool",
    "Ghoul Fool",
    "2020",
    "simple test",
    "",
    [],
    [],
    simple_test,
    menu="<Image>/Filters/simple-test",
)
 
main() 

更重要的是尝试获取一些错误消息/日志/控制台输出以找出我哪里出错了——只是默认情况下似乎不会显示。

标签: gimppython-fu

解决方案


看起来您确实错误地调用了当前图像。也许这个示例脚本可以帮助你:

#!/usr/bin/env python

# Tutorial available at: https://www.youtube.com/watch?v=nmb-0KcgXzI
# Feedback welcome: jacksonbates@hotmail.com

from gimpfu import *

def hello_warning(image, drawable):
    pdb.gimp_message("Hello world!")
    

register(
    "python-fu-hello-warning",
    "SHORT DESCRIPTION",
    "LONG DESCRIPTION",
    "Jackson Bates", "Jackson Bates", "2015",
    "Hello warning",
    "", # type of image it works on (*, RGB, RGB*, RGBA, GRAY etc...)
    [
        (PF_IMAGE, "image", "takes current image", None),
        (PF_DRAWABLE, "drawable", "Input layer", None)
    ],
    [],
    hello_warning, menu="<Image>/File")  # second item is menu location

main()

我还建议这个家伙关于 gimp python fu 的视频系列:https ://www.youtube.com/watch?v=nmb-0KcgXzI 。不幸的是,考虑到调试,官方文档(在我看来)缺乏对初学者的友好性。


推荐阅读