Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gimp 3: get indexed of each pixel from a layer = img.layers[0] pdb.gimp_layer
#3
(04-04-2025, 09:09 PM)TheInstinct Wrote: Hi everybody,

I'm facing a serious problem when porting a plugin I wrote in Gimp 2.10 to Gimp 3.0 in python.

In Gimp 2, I used the following way to the color index of each pixel. It works very well:
Code:
layer = img.layers[0]
region = layer.get_pixel_rgn(0, 0, img.width, img.height, False, False)
pixels = array("B", region[0:img.width, 0:img.height])
pixels contains the index of the color [0;255] which is a byte.

In Gimp 3, you can get access to color information of the layer by using layer.get_buffer(...) but you won't get the color index as you must provide a Babl format. So you will get a color, or luminance in the format you selected "RGB u8" for example, but you won't get the indexes of the colors used by each pixel.

I made research but can't find how to get color index of each pixel. I hope some of you will be able to help me.

Thank you in advance for your support.

The following works for me when checking for transparent bytes - adjust src_offset to 0 for the red index, 1, 2 or 4 for green (depending on the bytes per pixel) etc.:
Code:
       buffer = drawable.get_buffer()

       rect = Gegl.Rectangle.new(0, 0, width, height)
       src_pixels = buffer.get(rect, 1.0, None, Gegl.AbyssPolicy.CLAMP)

       def Pixel_Is_Opaque (x, y):
           byte_opaque = False
           if (x >= 0) and (x < width) and (y >= 0) and (y < height):
               temp = ((y * width) + x) * bytes_per_pixel
               temp += src_index
               byte_opaque = True          # assume opaque
               for i in range (num_bytes_to_check) :
                   if check_pattern[i] != src_pixels[temp + i] :
                       byte_opaque = False
           return byte_opaque;

where src_index is set depending on the bytes per pixel:

Code:
       if (image_precision >= Gimp.Precision.U8_LINEAR) and (image_precision <= Gimp.Precision.U8_PERCEPTUAL) :
           src_index = 3
           check_pattern = [255]
       elif (image_precision >= Gimp.Precision.U16_LINEAR) and (image_precision <= Gimp.Precision.U16_PERCEPTUAL) :
           src_index = 6
           check_pattern = [255, 255]
       elif (image_precision >= Gimp.Precision.U32_LINEAR) and (image_precision <= Gimp.Precision.U32_PERCEPTUAL) :
           src_index = 12
           check_pattern = [255, 255, 255, 255]
       elif (image_precision >= Gimp.Precision.HALF_LINEAR) and (image_precision <= Gimp.Precision.HALF_PERCEPTUAL) :
           src_index = 6
           check_pattern = [0, 60]
       elif (image_precision >= Gimp.Precision.FLOAT_LINEAR) and (image_precision <= Gimp.Precision.FLOAT_PERCEPTUAL) :
           src_index = 12
           check_pattern = [0, 0, 128, 63]
       else :
           proc   = Gimp.get_pdb().lookup_procedure('gimp-message')
           proc_config = proc.create_config()
           proc_config.set_property('message', 'Unrecognised precision (bit depth)' +
                                        "\n" + "\n" + 'Execution terminated.')
           result = proc.run(proc_config)
           return
Reply


Messages In This Thread
RE: Gimp 3: get indexed of each pixel from a layer = img.layers[0] pdb.gimp_layer - by programmer_ceds - 04-05-2025, 04:45 PM

Forum Jump: