PastPawn PastPawn - 11 days ago 12
Python Question

Python-Fu fails on directory selection (PF_DIRNAME)

Using Python-Fu/gimpfu, I was able to register a plugin and run it from the Script-Fu console.

enter image description here

But I could not run the plugin from the created GUI menu item.

enter image description here

No matter which entry I selected from either directory drop-down menu, I would receive these errors:


  1. pythonw.exe has stopped working (Close program)

  2. GIMP Message: Plug-in crashed: "batch_scale.py"

  3. GIMP Message: Unable to run GimpPdbProgress callback



GIMP version: 2.8.18

Plug-in code (some functions omitted for brevity):

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()

Answer

I get a similar problem on Linux, the plugin dies horribly as soon as I click on the directory selectors. There is however a workaround: give a default directory (it seems it doesn't even need to be valid directory, at least for on the Linux version):

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

PS:

  • While I applaud your foray in Gimp python scripting, you can do the same thing with one invokation of ImageMagick's convert command.
  • You can also test your Python scripts in the Python-fu console
  • In this 21st century, nobody registers scripts with menus in <Toolbox>/Xtns/Languages/Python-Fu/, you must be looking at very old docs.