Trouble with changing pixel colors

I’m wanting to edit individual pixel values. Checking my code as I go, I tested to see if it would copy the top and bottom rows of pixels, and ignore everything else. But instead I got a copy of the entire picture. What am I not understanding about the python version of Processing?

Basic code structure:

fname = 'picture.png'
img = [0]*2 # used to initialize a 2 slot array
imgIndex = 0

def setup():
  size(500, 500)
  this.surface.setResizable(True)
  img[0] = loadImage(fname)
  this.surface.setSize(img[0].width, img[0].height)
  img[1] = convolve(img[0])

def draw():
  image(img[imgIndex], 0, 0)

def convolve(source, kernel):
  target = createImage(source.width, source.height, RGB)
  for y in range(source.height):
      for x in range(source.width):
          c = source.get(x, y)
          if y in [0, source.height - 1]:
              target.set(x, y, c)
  return target

def keyReleased():
    global imgIndex
    if key == '0':
        imgIndex = 0
    elif key == '1':
        imgIndex = 1
3 Likes

We need background() in draw() in order to clear the canvas each frame: :wink:
Py.Processing.org/reference/background.html

4 Likes