python-fu select copy paste

4k Views Asked by At

I'm a newbie in python-fu, (my second day), so my question may seem naive: I'd like to select a rectangular portion from "r400r.png", rotate it 90 degrees, and save my selection in "r400ra.png".

So far, I tried something on these lines:

for fv in range(400,401):
  fn='r%sr.png' % fv
  img=pdb.gimp_file_load('/path/'+fn,fn)
  drw=pdb.gimp_image_get_active_layer(img)
  img1=pdb.gimp_image_new(1024,1568,0)
  lyr=pdb.gimp_layer_new(img1,1024,1568,0,'ly1',0,0)

  pdb.gimp_rect_select(img,10,200,1422,1024,2,0,0)
  drw=pdb.gimp_rotate(drw,0,1.570796327)
  pdb.script_fu_selection_to_image(img1,drw)
  f0=fn[:5]+'a'+fn[5:]
  pdb.gimp_file_save(drw,'/path/'+f0,f0)

The "lyr" layer is there because my understanding is that it is a must, although it's not clear to me why. The "for" loop eventually should bulk process a bunch of files; for testing it is restricted to one file only. I get an error where I try o execute "script_fu_selection_to_image".

Can you point me, please, in the right direction?

Thanks, SxN

1

There are 1 best solutions below

2
On

The PDB calls to do that are better in this order:

# import your image:
img=pdb.gimp_file_load('/path/'+fn,fn)

#make the selection
pdb.gimp_rect_select(img,10,200,1422,1024,2,0,0)


# copy
pdb.gimp_edit_copy(img.layers[0])
# (no need to "get_active_layer" - if
# your image is a flat PNG or JPG, it only has one layer,
# which is accessible as img.layers[0]) 

# create a new image from the copied area:
new_img = pdb.gimp_paste_as_new()

#rotate the newly created image:
pdb.gimp_image_rotate(new_img, ...)

#export the resulting image:
pdb.gimp_file_save(new_img, ...)

#delete the loaded image and the created image:
# (as the objects being destroyed on the Python side
# do not erase then from the GIMP app, where they
# stay consuming memory)
pdb.gimp_image_delete(new_img)
pdb.gimp_image_delete(img)