07-02-2026, 12:35 PM
I was finally able to crack this problem. Here is some sample code:
Code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# your standard imports, don't forget Gegl
import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp
gi.require_version('GimpUi', '3.0')
from gi.repository import GimpUi
gi.require_version('Gegl', '0.4')
from gi.repository import Gegl # << Gegl needed
# [...] other imports [...]
# needed for this example
import random
#
# [...] some code [...]
#
def randomColors(layer) :
# example on how to iterate over pixels with Gegl and apply these in batches
# this is much faster than drawing one by one with Gimp.Drawable.set_pixel()
# important : the changed pixels can't be undone, it's better to draw them on a new layer
thisBuffer = layer.get_buffer()
startx = 100
starty = 100
sizex = 80 # region size, preferably not too big, iterate if necessary
sizey = 80
rectangleRegion = Gegl.Rectangle.new(startx, starty, sizex, sizey)
bufferSize = sizex * sizey
bufferData = []
i = 0
while i < bufferSize :
# attribute some random values to rgb
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
bufferData.extend([r, g, b])
i += 1
thisBuffer.set(rectangleRegion, "RGB u8", bufferData) # << this is where we write pixels!
layer.update(startx, starty, sizex, sizey) # << necessary refresh

