Use a list to updatePixels()

  • filter() already got the GRAY type: Py.Processing.org/reference/filter.html
  • It converts all colors to 1 of the 256 grey tones.
  • You state you require 6 shades instead.
  • So we just need to use POSTERIZE after GRAY in order to get the desired effect:
"""
 Gray Posterized PImage (v1.0.1)
 GoToLoop (2019-Dec-09)
 https://Discourse.Processing.org/t/use-a-list-to-updatepixels/16268/10
"""

def hex6Digits(v): return str(hex(v, 6))
def mapToHex(container): return tuple(map(hex6Digits, container))

FILENAME = (
    'https://' 'Upload.Wikimedia.org' '/wikipedia/commons/thumb/2/2e/'
    'Processing_3_logo.png/' '240px-Processing_3_logo.png')

IMGS, NUM_GREYS, COLOR_OCTET, BG = 3, 6, 0xff, 0300
GREY_INTERVAL = COLOR_OCTET / (NUM_GREYS - 1)

GREYS_RANGE = tuple(range(0, COLOR_OCTET + 1, GREY_INTERVAL))
GREYS = tuple(color(grey) for grey in GREYS_RANGE)

print FILENAME
print GREYS_RANGE, len(GREYS_RANGE)
print mapToHex(GREYS), len(GREYS)

def settings():
    global original, greyed, posterized

    original = loadImage(FILENAME)
    greyed, posterized = original.get(), original.get()

    size(IMGS * original.width, original.height)
    noLoop()


def draw():
    background(BG)
    set(0, 0, original)

    transferPixels(original, greyed)
    greyed.filter(GRAY)
    set(width / IMGS, 0, greyed)

    transferPixels(greyed, posterized)
    posterized.filter(POSTERIZE, NUM_GREYS)
    set(2 * width / IMGS, 0, posterized)

    posterized.loadPixels()
    unique_colors = set(mapToHex(posterized.pixels))
    print unique_colors, len(unique_colors)


def transferPixels(src, dst):
    src.loadPixels()
    arrayCopy(src.pixels, dst.pixels)
    dst.updatePixels()
3 Likes