I wrote a tiny bit of processing.org (apparently it now supports Python!) to "pointify" images. The sketch allows for the user to define and limit the palette by picking individual colors off of the original image. You can also change the point sizes interactively. When in palette mode, the simple algorithm finds the closest 2 colors off of the palette and picks one randomly, this forces more diversity of colors in a section of the image.

Here's without using a palette:





 With a limiting palette:







from random import random, choice

WIDTH = 1000
HEIGHT = 800
POINT_SIZE = 7
PALLETTE_SIZE = 6
SPECTRUM_HEIGHT = 100
PAUSED = False
USE_PALLETTE = True
HIDE_CONTROLS = False
frames = 1000
img_uri = "/Users/maxime_beauchemin/Downloads/ripples.jpg"
img = None
spectrum = None
THUMB_SIZE = 200
thumb = None

def get_pallette(psize):
    p = []
    for i in range(psize):
        x = random() * WIDTH
        y = random() * HEIGHT
        p.append(img.get(int(x), int(y)));
    
    alternate_colors = []
    return p + alternate_colors

pallette = [color(0), color(255)]
def draw_pallette():
    square_size = 10
    for i, c in enumerate(pallette):
        fill(c)
        rect(i * square_size, HEIGHT-square_size, square_size, square_size)

def draw_point():
    x = random() * WIDTH
    y = random() * HEIGHT
    c = img.get(int(x), int(y));
    if USE_PALLETTE:
        pix = closest_color(c)
        fill(pix);
    else:
        fill(c)
    ellipse(x, y, POINT_SIZE, POINT_SIZE)

def color_dist(c1, c2):
    r = red(c1) - red(c2)
    g = green(c1) - green(c2)
    b = blue(c1) - blue(c2)
    return (b**2 + g**2 + r**2) ** 0.5

def closest_color(c):
    l = sorted([(color_dist(c, p), p) for p in pallette], key=lambda x: x[0])
    return choice(l[:2])[1]

def setup():
    size(WIDTH, HEIGHT)
    noStroke();
    alternate_pallette = []
    global pallette
    global thumb 
    global img
    global spectrum
    frameRate(30);
    
    thumb = loadImage(img_uri)
    img = loadImage(img_uri)
    img.resize(WIDTH, 0)
    spectrum = loadImage("/Users/maxime_beauchemin/Downloads/spectrum.jpg")
    thumb.resize(THUMB_SIZE, 0)
    spectrum.resize(THUMB_SIZE, SPECTRUM_HEIGHT)
    #pallette += get_pallette(PALLETTE_SIZE)
    variance = 50
    for p in pallette:
        c = color(red(p) + variance, green(p)+variance, blue(p)+variance)
        alternate_pallette.append(c)
        
    #pallette += alternate_pallette
    
def draw():
    if not PAUSED:
        for i in range(500):
            draw_point()
        if not HIDE_CONTROLS:
            draw_pallette()
            #thumb
            image(thumb, 0, SPECTRUM_HEIGHT)
            image(spectrum, 0, 0)
    
    
def mouseClicked():
    if mouseY > SPECTRUM_HEIGHT:
        c = thumb.get(mouseX, mouseY-SPECTRUM_HEIGHT)
    else:
        c = spectrum.get(mouseX, mouseY)
    pallette.append(c)
    
def keyPressed():
    global POINT_SIZE
    global PAUSED
    global USE_PALLETTE
    global HIDE_CONTROLS
    if key == 'x' and len(pallette)>1:
        pallette.pop()
    elif key == '-':
        POINT_SIZE -= 1
    elif key == '+':
        POINT_SIZE += 1
    elif key == 'p':
        PAUSED = not PAUSED
    elif key == 'u':
        USE_PALLETTE = not USE_PALLETTE
    elif key == 'h':
        HIDE_CONTROLS = not HIDE_CONTROLS
    

0 comments:

Post a Comment