It is currently Sun Jun 30, 2024 3:26 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Python Noob: Save PNG with transparent border
PostPosted: Sun Mar 14, 2021 12:55 pm  (#1) 
Offline
GimpChat Member

Joined: Mar 14, 2021
Posts: 5
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! :)


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: Python Noob: Save PNG with transparent border
PostPosted: Sun Mar 14, 2021 1:40 pm  (#2) 
Offline
GimpChat Member
User avatar

Joined: Mar 08, 2021
Posts: 329
Location: NorthEast USA
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 2289 times ]

_________________
Image
-----------
GIMP 2.10.25 (AppImage) / G'mic-Qt for GIMP 2.8 Linux 64bits 2.9.8 / Linux Mint 19.3
(LeftyLeo/James)
Top
 Post subject: Re: Python Noob: Save PNG with transparent border
PostPosted: Sun Mar 14, 2021 4:47 pm  (#3) 
Offline
GimpChat Member

Joined: Mar 14, 2021
Posts: 5
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!


Top
 Post subject: Re: Python Noob: Save PNG with transparent border
PostPosted: Sun Mar 14, 2021 7:13 pm  (#4) 
Offline
GimpChat Member

Joined: Jul 28, 2018
Posts: 1196
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..

_________________
https://www.deviantart.com/pocholo17
Image


Top
 Post subject: Re: Python Noob: Save PNG with transparent border
PostPosted: Mon Mar 15, 2021 3:41 am  (#5) 
Offline
GimpChat Member

Joined: Mar 14, 2021
Posts: 5
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 :)


Top
 Post subject: Re: Python Noob: Save PNG with transparent border
PostPosted: Mon Mar 15, 2021 9:45 am  (#6) 
Offline
GimpChat Member

Joined: Jul 28, 2018
Posts: 1196
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)

_________________
https://www.deviantart.com/pocholo17
Image


Top
 Post subject: Re: Python Noob: Save PNG with transparent border
PostPosted: Mon Mar 15, 2021 6:43 pm  (#7) 
Offline
Script Coder
User avatar

Joined: Oct 25, 2010
Posts: 4752
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

_________________
Image


Top
 Post subject: Re: Python Noob: Save PNG with transparent border
PostPosted: Wed Mar 17, 2021 8:37 pm  (#8) 
Offline
GimpChat Member

Joined: Mar 14, 2021
Posts: 5
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)


Top
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Where did GIMP save my file, & how do I change default save location

5

No new posts Attachment(s) NOOB seeks help

2

No new posts Attachment(s) Gradient not smooth - noob question

2

No new posts Convert GIMP plugin from Python 2 to Python 3

4

No new posts Attachment(s) Transparent Frame

3



* Login  



Powered by phpBB3 © phpBB Group