Search code examples
gimppython-fu

Save PNG with transparent border


Just starting out with python-fu in Gimp. I'm trying to emulate in python what I can do in the UI:

  1. Create a new image, say 1000px wide, 500px high, transparent fill.
  2. Open as layers "file1.png", which is 800px wide, 500px high - this leaves 100px of transparecy either side of the layer, which is what I want
  3. Export as "file2.png" - gives me a 1000px by 500px PNG, with the 800px image in the middle, and 100px transparency on either side - perfect.

Here's what I have:

img=pdb.gimp_image_new(1000, 500, 0)
lyr=pdb.gimp_file_load_layer(img,'C:\temp/file1.png')
pdb.gimp_image_insert_layer(img, lyr, None, 0)
#here's where I'm lost... how do I save as png with the transparent border? The following saves just as 800px wide, and loses the 100px transparency on either side...
drw=pdb.gimp_image_active_drawable(img)
pdb.file_png_save2(img,drw,'C:\temp/file2.png', 'C:\temp/file2.png',0,9,0,0,0,1,1,1,1)

Any help gratefully received! :)


Solution

  • I found a solution, by creating a separate transparent background png that is 1000px * 500px, adding that as an extra layer, and merging the two layers, clipped to the image size:

    img=pdb.gimp_image_new(1000, 500, 0)
    lyr=pdb.gimp_file_load_layer(img,'C:\temp/file1.png')
    pdb.gimp_image_insert_layer(img, lyr, None, 0)
    lyr2=pdb.gimp_file_load_layer(img,'C:\temp/bg.png')
    pdb.gimp_image_insert_layer(img, lyr2, None, 0)
    pdb.gimp_image_merge_visible_layers(img,CLIP_TO_IMAGE)
    drw=pdb.gimp_image_active_drawable(img)
    pdb.file_png_save2(img,drw,'C:\temp/file2.png', 'C:\temp/file2.png',0,9,0,0,0,1,1,1,1)