GIMP Chat
http://gimpchat.com/

Python Noob: Save PNG with transparent border
http://gimpchat.com/viewtopic.php?f=9&t=19228
Page 1 of 1

Author:  pinksy [ Sun Mar 14, 2021 12:55 pm ]
Post subject:  Python Noob: Save PNG with transparent border

Hi. First time post, and gimp python newbie here, so apologies if this is obvious. I'm trying to emulate 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! :)

Author:  leftyleo [ Sun Mar 14, 2021 1:40 pm ]
Post subject:  Re: Python Noob: Save PNG with transparent border

not sure why you need transparent border but this is what i did :

---I located an image to use: Minions. It was way too big so I had to first scale it down.
---Opened this in GIMP and keeping aspect ratio gave me 800x450.
---Then i created a new image with a transparent background of 850x500 (which will give me a 50px
border all around)
---went back to the Minion window and select all, copy
---back to new transparent image
---now you can paste right to the transparent layer , then anchor it.
and now it is 800x450 Minion image with a 50px transparent border making the image 850x500 in total
--note: as far as i know you have to export as PNG to retain the transparent border ( JPG did not save the transparent border, it made it white for me )
--you cant see it here on white background (sorry, but it is transparent bordered)

Attachments:
minions_TransparentBorder.png
minions_TransparentBorder.png [ 429.36 KiB | Viewed 2295 times ]

Author:  pinksy [ Sun Mar 14, 2021 4:47 pm ]
Post subject:  Re: Python Noob: Save PNG with transparent border

leftyleo wrote:
not sure why you need transparent border but this is what i did : ...


@leftyleo - thank you for replying, really appreciate the time you took. Actually though, it's not so much how to do it in the gimp UI that I'm struggling with - I can do that ok. It's how to do it in python that I'm struggling with. I wasn't sure if the "Gimp Scripts" sub was the right place to ask!

Author:  Pocholo [ Sun Mar 14, 2021 7:13 pm ]
Post subject:  Re: Python Noob: Save PNG with transparent border

pinksy wrote:
Hi. First time post, and gimp python newbie here, so apologies if this is obvious. I'm trying to emulate 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! :)


Before you saved it, did you add an alpha to the layer:
pdb.gimp_layer_add_alpha(layer)

Otherwise, the image will have a background color and will not keep the Transparency..

Author:  pinksy [ Mon Mar 15, 2021 3:41 am ]
Post subject:  Re: Python Noob: Save PNG with transparent border

Pocholo wrote:
Before you saved it, did you add an alpha to the layer:
pdb.gimp_layer_add_alpha(layer)

Otherwise, the image will have a background color and will not keep the Transparency..


Hi @Pocholo. Ok, I tried that:

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)
pdb.gimp_layer_add_alpha(lyr)
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)


...but it's still coming out cropped to the width of the inserted layer (800px) rather than the width of the overall image (1000px).

Is it something to do with merging? Or something I've got wrong with pdb.file_png_save2?

Thanks again for any help :)

Author:  Pocholo [ Mon Mar 15, 2021 9:45 am ]
Post subject:  Re: Python Noob: Save PNG with transparent border

I see in your code that you not giving the image a transparency.
You cannot expect a layer with an Alpha channel to give you the transparency you want. Have to follow by:

pdb.gimp_drawable_fill(lyr, FILL_TRANSPARENT)

Author:  ofnuts [ Mon Mar 15, 2021 6:43 pm ]
Post subject:  Re: Python Noob: Save PNG with transparent border

pinksy wrote:
Pocholo wrote:
Before you saved it, did you add an alpha to the layer:
pdb.gimp_layer_add_alpha(layer)

Otherwise, the image will have a background color and will not keep the Transparency..


Hi @Pocholo. Ok, I tried that:

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)
pdb.gimp_layer_add_alpha(lyr)
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)


...but it's still coming out cropped to the width of the inserted layer (800px) rather than the width of the overall image (1000px).

Is it something to do with merging? Or something I've got wrong with pdb.file_png_save2?

Thanks again for any help :)


Keep in mind that what you save is a layer, not the image, so the canvas size is irrelevant. Two solutions:

* pdb.gimp_layer_resize_to_image_size(layer) and save the layer
* pdb.gimp_layer_new_from_visible(image) and save that new layer

Author:  pinksy [ Wed Mar 17, 2021 8:37 pm ]
Post subject:  Re: Python Noob: Save PNG with transparent border

Thank you all for the replies. I will definitely try the two solutions offered. I also found a hacky 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)

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/