我正试图用Python fu进行批处理,但我的脚本无法正常工作。
当我尝试调用pdb中的一些方法时,例如pdb.plug_in_nlfilt()
,插件将不会注册
我正在构建test_batch_invert.py示例here。该示例工作正常,并按预期处理文件,但下面的代码未注册。当我有一个映像要处理时,所有这些pdb函数都在python控制台中单独工作
有什么想法吗?另外,当我注释掉所有有问题的pdb调用并且插件注册时,除非打开图像,否则它在菜单中显示为灰色。有没有一种方法可以使它在菜单中正常工作,而不必手动打开另一个图像文件
谢谢大家
--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个参数(in&;out目录),函数使用4个参数。过去有默认的图像和图层参数,但随着时间的推移,注册咒语已经发生了一些变化。图像和图层作为前两个参数是专门处理的,但是如果需要它们,则需要在注册中声明它们。自Gimp 2.6以来,菜单位置
<Toolbox>
已被弃用,因此您的示例已经非常过时作为文件分隔符的“\”在Linux和OSX上不起作用。“/”适用于任何地方,甚至在Windows上,但更适合使用
os.path.join()
完成后,脚本将为我运行(Linux上的Gimp 2.10.14)。我假设它查找具有特定特征的文件,因为如果它运行,结果会很奇怪:)
菜单将灰显,直到您打开图像为止,因为您的图像类型是
"*"
,它是任何类型的图像,而不是不需要任何图像的""
(空字符串)也就是说,您不需要注册脚本/插件就可以在批处理模式下使用它,有关示例,请参见here
在Windows here上调试Gimp python脚本的一些提示
PS:据我所知,
gimp_wire_read(): error
消息可以忽略,它不是由脚本/插件引起的相关问题 更多 >
编程相关推荐