GIMP Python-fu 导出文件只导出透明层 [英] GIMP Python-fu exporting file only exports transparent layer
问题描述
我在通过 GIMP 在 python 中保存图像时遇到问题.我可以获得图像并应用我想要的效果,但是当我去保存时,它只保存一层而不是所有内容(注意:背景是透明的)并且因为背景是透明的,所以我无法保存任何东西透明背景.我正在使用的代码发布如下:
I am having issues saving images in python via GIMP. I can get the image and apply the effects I want, but when I go to save, it only saves one layer and not everything (NOTE: The background is transparent) and because the background is transparent, I cannot get it to save anything besides the transparent background. The code I am using is posted below:
image_array = gimp.image_list()
i=0
for image in image_array:
img = image_array[i]
layers = img.layers
last_layer = len(layers)-1
try:
disable=pdb.gimp_image_undo_disable(img)
pdb.gimp_layer_add_alpha(layers[0])
drw = pdb.gimp_image_active_drawable(img)
pdb.plug_in_colortoalpha(img,drw,(0,0,0))
drw = pdb.gimp_image_active_drawable(img)
enable = pdb.gimp_image_undo_enable(img)
except:
print "ERROR"
pdb.file_png_save(img, drw, "C:\\Users\\jammer\\Desktop\\test.png",
"test.png",0,9,1,1,1,1,1)
i+=1
我也尝试过 file_png_save2
,但我感觉问题出在 drw 对象上,因为我只想复制单击 File->Export 和无需通过 GUI 即可保存为 PNG.我宁愿让它自动保存(我有 49 个图像,每个图像都会自动命名,但首先我需要让它用一张图像正确导出).正如我之前所说,上面的代码只导出透明背景,即使更改为 GIF 也不能解决问题.如何在保留所有图层和透明背景的同时将文件导出为 PNG?
I have also tried file_png_save2
, but I have a feeling the problem lies in the drw object as I just want to replicate the option of clicking File->Export and saving as PNG without doing that via GUI. I would rather have it save automatically (I have 49 images and each will be named automatically, but first I need to get it to export correctly with one image). as I said before, the code above only exports a transparent background, even changing to a GIF does not resolve the issue. How do I export a file as a PNG while keeping all layers and transparent background?
推荐答案
我发现了我的问题!我没有合并可见图层并将其设置为等于新图层,然后在将图像保存为 png 时将其用作可绘制对象"!我已经在下面发布了固定代码:
I found my problem! I was not merging the visible layers and setting that equal to the new layer, which I then used as the "drawable object" when saving the image as a png! I have posted the fixed code below:
image_array = gimp.image_list()
i=0
for image in image_array:
img = image_array[i]
layers = img.layers
last_layer = len(layers)-1
try:
disable=pdb.gimp_image_undo_disable(img)
pdb.gimp_layer_add_alpha(layers[0])
drw = pdb.gimp_image_active_drawable(img)
pdb.plug_in_colortoalpha(img,drw,(0,0,0))
layer = pdb.gimp_image_merge_visible_layers(img, gimpfu.CLIP_TO_IMAGE)#FIXES PROBLEM OF ONLY EXPORTING TRANSPARENCY!
enable = pdb.gimp_image_undo_enable(img)
except:
print "ERROR"
pdb.file_png_save2(img, layer, "C:\\Users\\jammer\\Desktop\\test.png","test.png",1,9,1,1,1,1,1,0,1)
i+=1
这篇关于GIMP Python-fu 导出文件只导出透明层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!