正如承诺的那样,这里有一个适用于普通白色的工作解决方案-->;alpha用例。这是在Ubuntu 10.04.1 LTS服务器上运行的,带有标准的GIMP安装(2.6.8)。
from gimpfu import *
def run(input_filepath):
image = pdb.gimp_file_load(input_filepath, input_filepath)
image.disable_undo()
layer = image.active_layer
if not layer.is_rgb:
pdb.gimp_image_convert_rgb(image)
white = gimpcolor.RGB(1.0, 1.0, 1.0, 1.0)
bg_color = pdb.gimp_image_pick_color(image, layer, 0, 0, True, False, 0)
if bg_color == white:
pdb.plug_in_colortoalpha(image, layer, bg_color)
layer_copy = layer.copy()
image.add_layer(layer_copy)
image.merge_visible_layers(CLIP_TO_IMAGE)
pdb.file_png_save_defaults(image, image.active_layer, input_filepath, input_filepath)
run( %(input_filepath)s )
我使用子流程模块(code_as_string是上面作为字符串的代码,插入了input_filepath)从Python(在Django中)执行这段代码:
gimp_args = (settings.PATH_TO_GIMP,
-i ,
--batch-interpreter=python-fu-eval ,
-b , code_as_string,
-b , from gimpfu import pdb; pdb.gimp_quit(True) )
environ = os.environ.copy()
environ[ GIMP2_DIRECTORY ] = settings.PATH_TO_GIMP_DIR
p = subprocess.Popen(gimp_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=environ)
rc = p.wait()
if rc:
logging.error(p.stdout.read())