Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gimp 3 - python to edit the selection
#1
I'm trying to make a plugin to edit the current image's selection. I'm pretty sure I've worked out how to pull in and edit the selection at this point, but I don't know how to set the modified selection back into the image. I've a few questions:

  1. Is there documentation for how to handle this sort of thing? It was quite the effort to get as far as I've gotten. I am comfortable with C, if that helps. 

  2. Can anyone provide an example of how to modify the current image's selection based on a byte array? 

  3. Alternately, here is my current logic. If anyone can fill in the blank ("# ???" line) then I'd be very grateful:
Code:
   def do_run(self, procedure, run_mode, image, n_drawables, drawables, config):
       try:
           selection_mask = image.get_selection()
           if selection_mask is None:
               Gimp.message("No selection found. This tool only works with an active selection.")
               return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR, None)

           old_buffer = selection_mask.get_buffer()
           rect = old_buffer.get_extent()
           old_buffer_bytes = old_buffer.get(rect, 1.0, "Y float", Gegl.AbyssPolicy.NONE)
           src_pixels = bytearray(old_buffer_bytes)
           dst_pixels = bytearray(old_buffer_bytes)

           for y in range(1, rect.height - 1):
               for x in range(1, rect.width - 1):
                   pixel_index = x + y * rect.width
                   if (src_pixels[pixel_index + 1] != 0 and
                       src_pixels[pixel_index - 1] != 0 and
                       src_pixels[pixel_index + rect.width] != 0 and
                       src_pixels[pixel_index - rect.width] != 0):
                           dst_pixels[pixel_index] = 0

           new_buffer = Gegl.Buffer.new("Y float", rect.x, rect.y, rect.width, rect.height)
           new_buffer.set(rect, "Y float", list(dst_pixels))
           
           # ???

           return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, None)
       except Exception as e:
           Gimp.message(f"ERR: {e}")
           return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR, None)
Reply
#2
(Yesterday, 09:36 AM)violgamba Wrote: I'm trying to make a plugin to edit the current image's selection. I'm pretty sure I've worked out how to pull in and edit the selection at this point, but I don't know how to set the modified selection back into the image. I've a few questions:

  1. Is there documentation for how to handle this sort of thing? It was quite the effort to get as far as I've gotten. I am comfortable with C, if that helps. 

  2. Can anyone provide an example of how to modify the current image's selection based on a byte array? 

  3. Alternately, here is my current logic. If anyone can fill in the blank ("# ???" line) then I'd be very grateful:
Code:
   def do_run(self, procedure, run_mode, image, n_drawables, drawables, config):
       try:
           selection_mask = image.get_selection()
           if selection_mask is None:
               Gimp.message("No selection found. This tool only works with an active selection.")
               return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR, None)

           old_buffer = selection_mask.get_buffer()
           rect = old_buffer.get_extent()
           old_buffer_bytes = old_buffer.get(rect, 1.0, "Y float", Gegl.AbyssPolicy.NONE)
           src_pixels = bytearray(old_buffer_bytes)
           dst_pixels = bytearray(old_buffer_bytes)

           for y in range(1, rect.height - 1):
               for x in range(1, rect.width - 1):
                   pixel_index = x + y * rect.width
                   if (src_pixels[pixel_index + 1] != 0 and
                       src_pixels[pixel_index - 1] != 0 and
                       src_pixels[pixel_index + rect.width] != 0 and
                       src_pixels[pixel_index - rect.width] != 0):
                           dst_pixels[pixel_index] = 0

           new_buffer = Gegl.Buffer.new("Y float", rect.x, rect.y, rect.width, rect.height)
           new_buffer.set(rect, "Y float", list(dst_pixels))
           
           # ???

           return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, None)
       except Exception as e:
           Gimp.message(f"ERR: {e}")
           return procedure.new_return_values(Gimp.PDBStatusType.EXECUTION_ERROR, None)
If you type gimp-image-select into the PDB browser you will find a number of procedures for selecting parts of the image e.g. gimp-image-select-rectangle()
Reply


Forum Jump: