Reputation: 137
Using Python-Fu/gimpfu, I was able to register a plugin and run it from the Script-Fu console.
But I could not run the plugin from the created GUI menu item.
No matter which entry I selected from either directory drop-down menu, I would receive these errors:
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()
Upvotes: 1
Views: 1501
Reputation: 8914
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"),
convert
command.<Toolbox>/Xtns/Languages/Python-Fu/
, you must be looking at very old docs.Upvotes: 2