Solution on "how to iterate over pixels with python API"
Hi, after a lot of tinkering, I was able to find a solution to the problem presented in this previous thread.
This might be useful to some of you, so here is some example 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, maybe not too huge? 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
Edit:
Not sure about the size of the rectangular region you work on, the variable used to iterate doesn't seem to load more RAM than a regular layer, and GIMP frees the memory slowly after the plugin closes.