Python-Fu在目录选择上失败(PF_DIRNAME)

时间:2016-11-26 17:47:46

标签: python gimp gimpfu

使用Python-Fu / gimpfu,我能够注册一个插件并从Script-Fu控制台运行它。

enter image description here

但是我无法从创建的GUI菜单项运行插件。

enter image description here

无论我从哪个目录下拉菜单中选择哪个条目,我都会收到以下错误:

  1. pythonw.exe已停止工作(关闭程序)
  2. GIMP消息:插件崩溃:“batch_scale.py”
  3. GIMP消息:无法运行GimpPdbProgress回调
  4. GIMP版本:2.8.18

    插件代码(为简洁起见,省略了一些功能):

    import os
    from gimpfu import *
    
    # Copy to ~/.gimp-<version>/plug-ins
    # Launch GIMP
    # Should register a function named python-fu-batch-scale
    # Run the function from Filters > Script-Fu > Console
    
    def loadImage(sourceFile):
        if isJPEG(sourceFile):
            return pdb.file_jpeg_load(sourceFile, sourceFile)
        if isPNG(sourceFile):
            return pdb.file_png_load(sourceFile, sourceFile)
    
    def saveImage(outputFile, image):
        drawable = pdb.gimp_image_get_active_drawable(image)
        if isJPEG(outputFile):
            saveJPEG(outputFile, image, drawable)
        if isPNG(outputFile):
            savePNG(outputFile, image, drawable)
    
    def scaleImage(image, maxWidth, maxHeight):
        width = pdb.gimp_image_width(image)
        height = pdb.gimp_image_height(image)
        aspectRatio = width * 1.0 / height
        if aspectRatio >= 1.0:
            # horizontal
            newWidth = min(width, maxWidth)
            newHeight = newWidth / aspectRatio
            pdb.gimp_image_scale(image, newWidth, newHeight)
        else:
            # vertical
            newHeight = min(height, maxHeight)
            newWidth = newHeight * aspectRatio
            pdb.gimp_image_scale(image, newWidth, newHeight)
    
    def run(sourceFolder, outputFolder, maxWidth, maxHeight):
    
        if not os.path.exists(outputFolder):
            os.makedirs(outputFolder)
    
        filenames = [f for f in os.listdir(sourceFolder) if os.path.isfile(os.path.join(sourceFolder, f))]
        for filename in filenames:
            sourceFile = os.path.join(sourceFolder, filename)
            outputFile = os.path.join(outputFolder, filename)
            image = loadImage(sourceFile)
            scaleImage(image, maxWidth, maxHeight)
            saveImage(outputFile, image)
    
    register(
        "batch_scale",
        "Scales a folder of images (JPEG or PNG)",
        "<help>",
        "<author>",
        "<license>",
        "<date>",
        "<Toolbox>/Xtns/Languages/Python-Fu/_Scale Images",
        "",
        [
            (PF_DIRNAME, "sourceFolder", "Source directory", ""),
            (PF_DIRNAME, "outputFolder", "Output directory", ""),
            (PF_INT, "maxWidth", "Maximum width", 1600),
            (PF_INT, "maxHeight", "Maximum height", 900)
        ],
        [],
        run
    )
    
    main()
    

1 个答案:

答案 0 :(得分:2)

我在Linux上遇到类似的问题,一旦点击目录选择器,插件就会死得很厉害。然而,有一个解决方法:给出一个默认目录(它似乎甚至不需要是有效的目录,至少在Linux版本上):

(PF_DIRNAME, "outputFolder", "Output directory", "/tmp"),

PS:

  • 虽然我赞赏你在Gimp python脚本中的尝试,但你可以通过ImageMagick convert command的一次调用来做同样的事情。
  • 您还可以在Python-fu控制台中测试Python脚本
  • 在这个21世纪,没有人用<Toolbox>/Xtns/Languages/Python-Fu/中的菜单注册脚本,你必须查看非常旧的文档。