使用 Python-Fu/gimpfu,我能够注册一个插件并从 Script-Fu 控制台运行它。
但我无法从创建的 GUI 菜单项运行插件。
无论我从任一目录下拉菜单中选择哪个条目,我都会收到以下错误:
- pythonw.exe 已停止工作(关闭程序)
- GIMP 消息:插件崩溃:“batch_scale.py”
- GIMP 消息:无法运行 GimpPdbProgress 回调
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()