我正在尝试使用Python-fu做一些批处理,但是无法使我的脚本正常工作。当我尝试在pdb中调用某些方法,例如pdb.plug_in_nlfilt()
时,插件将无法注册。
我正在构建test_batch_invert.py示例here。该示例可以正常工作,并且可以按预期处理文件,但是下面的代码未注册。当我有要处理的图像时,所有这些pdb函数都可以在python控制台中单独工作。
有什么想法吗?另外,当我注释掉所有有问题的pdb调用和插件does寄存器时,除非打开图像,否则菜单中的它会显示为灰色。有没有一种方法可以使它在菜单中起作用而不必手动打开另一个图像文件?
谢谢大家!
-EDIT:修复了一些名称为“ layer”的错别字,但不能解决。
#!/usr/bin/env python
# Repurposed from https://github.com/jfmdev/PythonFuSamples (see copyright at bottom)
import os
from gimpfu import *
def process_card(img, layer, inputFolder, outputFolder):
''' Display the message "Hello world" in the bottom of GIMP.
Parameters:
img : image The current image.
layer : layer The layer of the image that is selected.
'''
for file in os.listdir(inputFolder):
try:
# Build the full file paths.
inputPath = inputFolder + "\\" + file
outputPath = outputFolder + "\\" + file
# Open the file if is a JPEG or PNG image.
image = None
if(file.lower().endswith(('.png'))):
image = pdb.file_png_load(inputPath, inputPath)
if(file.lower().endswith(('.jpeg', '.jpg'))):
image = pdb.file_jpeg_load(inputPath, inputPath)
# Verify if the file is an image.
if(image != None):
if(len(image.layers) > 0):
layer = image.layers[0]
# THIS INVERT WORKS
pdb.gimp_invert(layer)
# (image, 2 for replace, x, y, width, height)
pdb.gimp_image_select_rectangle(image, 2, 865, 680, 1270, 2020)
# Including any of the rest of these pdb calls EXCEPT for pdb.file_jpg.save()
# will prevent plugin from registering.
pdb.gimp_selection_invert(image)
pdb.gimp_edit_clear(layer)
# Run alpha trimmed filter 3x
pdb.plug_in_nlfilt(image, layer, 1.0, 1.0, 0)
pdb.plug_in_nlfilt(image, layer, 1.0, 1.0, 0)
pdb.plug_in_nlfilt(image, layer, 1.0, 1.0, 0)
#run optimal smoothing once
pdb.plug_in_nlfilt(image, layer, 1.0, 1.0, 1)
#Edge enhance
pdb.plug_in_nlfilt(image, layer, 0.5, 0.7, 2)
#Select all
pdb.gimp_selection_all(image)
#zealous crop
pdb.plug_in_zealouscrop(image, layer)
#resize image
pdb.gimp_image_scale(image, 600, 955)
pdb.file_jpeg_save(image, layer, outputPath, outputPath, 0.9, 0, 0, 0, "Card", 0, 0, 0, 0)
except Exception as err:
gimp.message("Unexpected error: " + str(err))
register(
"python_fu_process_card",
"Card Scan",
"Process scanned image with jig, output to scaled jpg",
"My library",
"Open source (BSD 3-clause license)",
"2020",
"<Toolbox>/Filters/Process Card",
"*",
[
(PF_DIRNAME, "inputFolder", "Input directory", ""),
(PF_DIRNAME, "outputFolder", "Output directory", "")
],
[],
process_card)
main()
#
# -------------------------------------------------------------------------------------
#
# Copyright (c) 2013, Jose F. Maldonado
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# - Neither the name of the author nor the names of its contributors may be used
# to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
GIMP详细输出提供的错误:
Querying plug-in: 'C:\Users\dried\AppData\Roaming\GIMP\2.10\plug-ins\img_BSG_card.py'
C:\Program Files\GIMP 2\bin\gimp-2.10.exe: LibGimpBase-WARNING: gimp-2.10.exe: gimp_wire_read(): error
为我工作,经过一些修复,但这些不是PDB的要求:
您的注册声明了2个args(进入和退出目录),您的函数使用4。以前是默认的图像和图层args,但是注册的口头随着时间的流逝而有所变化。 Image和layer作为前两个arg进行了专门处理,但是如果要使用它们,则需要在注册中声明它们。从Gimp 2.6开始不推荐使用菜单位置<Toolbox>
,因此您的示例已大大过时。
“ \”作为文件分隔符在Linux和OSX上不起作用。 “ /”在任何地方都可以使用,甚至在Windows上也可以,但是使用os.path.join()
更合适。
完成后,脚本将为我运行(Linux上为Gimp 2.10.14)。我认为它查找具有特定特征的文件,因为如果运行该文件,结果会很奇怪:)
在打开图像之前,该菜单显示为灰色,因为图像类型是"*"
,它是任何类型的图像,而不是不需要任何图像的""
(空字符串)。
这说,您不需要注册脚本/插件即可在批处理模式下使用它,请参阅here作为示例。
一些提示在Windows here上调试Gimp python脚本。