It is currently Tue Mar 19, 2024 1:06 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Inserting a new layer without resizing
PostPosted: Fri Jun 23, 2017 2:41 pm  (#1) 
Offline
New Member

Joined: Jun 22, 2017
Posts: 2
How can I add an image from an external source, and resize it to fit in the current image, without messing up the size of the current image?

I'm using python.

The basic sequence I'm doing is:
new_image_layer = pdb.gimp_file_load_layer(im, file.png)
pdb.gimp_image_insert_layer(im, new_image_layer, None, image_layer_pos)
pdb.gimp_selection_none(im)
pdb.gimp_item_transform_scale(new_image_layer,x,y,x+width, y+height)
pdb.file_png_save_defaults(...)
But since the external png is initially larger than the current image, inserting it changes the borders, so the saved png ends up with a large transparent background. It doesn't let me scale before inserting.

I'm currently just using a crop command at the end with a reference layer known to be the right size,
pdb.plug_in_autocrop(im,reference_layer)
but this isn't a general solution, and I'd rather not change it in the first place.


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: Inserting a new layer without resizing
PostPosted: Fri Jun 23, 2017 2:54 pm  (#2) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 3965
Location: Canada
Can you try ?
pdb.gimp_item_transform_scale(new_image_layer,0,0,reference_layer.width,reference_layer.height)


loading the layer (if it's larger than image) it doesn't change image's border just layer's border so transform scaling it makes the layer fit inside image's boundary according to your reference layer again.
At least for me, i observed it didn't change image/canvas' border.

_________________
TinT


Top
 Post subject: Re: Inserting a new layer without resizing
PostPosted: Fri Jun 23, 2017 5:35 pm  (#3) 
Offline
New Member

Joined: Jun 22, 2017
Posts: 2
Well, after reading your comment that loading the image should only effect the layer it's loaded into, I realized I had just been assuming that was my problem, and hadn't actually confirmed it. Turns out it has nothing to do with loading the external image, or apparently anything else in my script. I can comment out everything except loading the initial base image and saving the png at the end, and I still get the enlarged transparent background behind the base image.

So I assume it must be the base image I'm using, the way I'm loading it, or the way I'm exporting at the end. I tried opening the image in the gui, and using the 'export to' function, and it created a png file as expected, so I don't think the base image is the cause.

In summary, the act of loading the xcf file from a script, then saving it to a png is enlarging the borders of the image. Do I need to specify the size when I'm saving or something?
Here's my code:
# Open file                                                                 
im = pdb.gimp_file_load(fname_base, fname_base  )                           

... script stuff ...
   
# Save                                                                       
merged = pdb.gimp_image_merge_visible_layers(im, 0)                                                                     
outname = "{}{}.png".format(prefix ,title_text.replace(' ', '_'))           
full_output_filename = os.path.join(outdir, outname)                                                                                               
pdb.file_png_save_defaults(im, merged, full_output_filename, full_output_file


EDIT: Problem solved. It was the merge_visible command. I passed a 0 for the merge type, which expands as necessary. I had a one point manually pasted in an external image to the base image, so one of the layers must've gotten expanded to exactly it's size (which is why I thought that was what was happening in the script). It's not visible in the editor so I didn't notice it. Just changing the merge type to 1 fixed my problem. I should also probably fix the base image and figure out which layer is too big.


Top
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


   Similar Topics   Replies 
No new posts Attachment(s) Inserting pdb.python_gegl command in my code

21

No new posts Attachment(s) Resizing Pictures

2

No new posts Resizing Images

6

No new posts Help with image resizing

2

No new posts GMIC Montage - how to prevent resizing

2



* Login  



Powered by phpBB3 © phpBB Group