Gimp-Forum.net

Full Version: get_pixel_rgn() function in gimp 3 python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there any function in gimp 3 python which replaces layer.get_pixel_rgn(0, 0, layer.width, layer.height) ? I tried but this function is not working.
(04-04-2021, 05:02 AM)ksoman Wrote: [ -> ]Is there any function in gimp 3 python which replaces layer.get_pixel_rgn(0, 0, layer.width, layer.height) ? I tried but this function is not working.

The following is from the GIMP Python documentation (layers are drawables):

drawable.get_pixel_rgn(x, y, w, h, [dirty, [shadow])

Creates a pixel region for the drawable. It will cover the region with origin (x,y) and dimensions w x h. The dirty argument sets whether any changes to the pixel region will be reflected in the drawable (default is TRUE). The shadow argument sets whether the pixel region acts on the shadow tiles or not (default is FALSE). If you draw on the shadow tiles, you must call drawable.merge_shadow() for changes to take effect.
(04-04-2021, 04:30 PM)programmer_ceds Wrote: [ -> ]
(04-04-2021, 05:02 AM)ksoman Wrote: [ -> ]Is there any function in gimp 3 python which replaces layer.get_pixel_rgn(0, 0, layer.width, layer.height) ? I tried but this function is not working.

The following is from the GIMP Python documentation (layers are drawables):

drawable.get_pixel_rgn(x, y, w, h, [dirty, [shadow])

   Creates a pixel region for the drawable. It will cover the region with origin (x,y) and dimensions w x h. The dirty argument sets whether any changes to the pixel region will be reflected in the drawable (default is TRUE). The shadow argument sets whether the pixel region acts on the shadow tiles or not (default is FALSE). If you draw on the shadow tiles, you must call drawable.merge_shadow() for changes to take effect.

This function does not work in gimp 3.
I don't think there is a specific python documentation for Gimp v3. The only Python API doc I know is for Gimp 2.x and Python 2.7. AFAIK the intent of the developers is for all languages (except perhaps script-fu) to use Glib introspection to access the API available to all plugins.

IMHO unless you really need to use the raw 32-bit data in your plugin it will be a lot easier to use Gimp 2.10 and Python V2.
(04-04-2021, 05:02 AM)ksoman Wrote: [ -> ]Is there any function in gimp 3 python which replaces layer.get_pixel_rgn(0, 0, layer.width, layer.height) ? I tried but this function is not working.

You may already have answered the question but for the sake of others following the same path the following works for me:

Code:
width = drawable.get_width()
height = drawable.get_height()

buffer = drawable.get_buffer()

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

(You might want to consider not reading the whole of the image in one go if you are working with large files - possible read a row at a time and iterate through the image.)

Note, however, that whilst this works for images in 8-bit integer encoding so that src_pixels[0] = pixel(0,0) red, src_pixels[1] = pixel(0,1) blue etc things look like a work in progress for higher bit-depths.

For 16-bit integer src_pixels is still an 8-bit array but is twice the length. You would have to combine bytes to get the pixel components. Similarly for 32-bit integers there is a further doubling of the array size.

Floating-point values don't seem to convert to the values that I was expecting - this might be as the result of the conversion form 8-bit integer to floating-point form, I don't know.

The function to get the components of a pixel also only seems to give 8-bit values regardless of the bit-depth of the image - and for higher bit-depths these are not just truncated values they are just wrong.

Code:
pixel = drawable.get_pixel(1, 1)

(These comments apply to GIMP V2.99.6)