Skip to content

Instantly share code, notes, and snippets.

@ondrejzacha
Last active March 18, 2019 11:58
Show Gist options
  • Save ondrejzacha/ce8f693dc7933ca2513b0b3e52326623 to your computer and use it in GitHub Desktop.
Save ondrejzacha/ce8f693dc7933ca2513b0b3e52326623 to your computer and use it in GitHub Desktop.
image rasterization with lines
FG = color(17, 17, 17, 10) # foreground color
BG = "#f3f3f3" # background color
WIDTH = 1000
ASPECT_RATIO = 0.666 # !!! copy from console
TOTAL_FRAMES = 10 # higher means darker & covering higher percentage of picture
LINE_LENGTH = 65 # higher means more blurry & darker
GRID_DISTANCE = 15 # if GRID_DISTANCE >> LINE_LENGTH, picture may be incomplete; higher -> lighter
MAX_LINES_PER_STAR = 50 # higher means higher sensitivity (more shades), darker with less visible stars,
# should be typically >> GRID_DISTANCE
LINE_LENGTH_RATIO_DARK = 0.5 # how much shorter (relatively) should be the lines for darkest areas
X_SHIFT = 3 # to prevent a boring rectangular grid
Y_SHIFT = 4
def setup():
global img
img = loadImage("picture.jpg") # !!! this file needs to be in the sketch folder
aspect_ratio = float(img.height) / float(img.width)
println("Aspect ratio: " + str(aspect_ratio))
img.resize(WIDTH, int(WIDTH * aspect_ratio))
size(WIDTH, int(WIDTH * ASPECT_RATIO))
background(BG)
stroke(FG)
def draw():
if frameCount <= TOTAL_FRAMES:
for x in range((frameCount * X_SHIFT) % GRID_DISTANCE, img.width, GRID_DISTANCE):
for y in range((frameCount * Y_SHIFT) % GRID_DISTANCE,
img.height, GRID_DISTANCE):
c = img.get(x, y)
b = map(brightness(c), 0, 255, 1, 0)
for i in range(int(b * MAX_LINES_PER_STAR)):
random_line(x, y, LINE_LENGTH * (1 - LINE_LENGTH_RATIO_DARK))
def random_line(x, y, w):
pushMatrix()
translate(x, y)
rotate(random(0, TWO_PI))
line(-w/2, 0, w/2, 0)
popMatrix()
def keyPressed():
if key == 's':
saveFrame("frame-######.png")
println("Frame saved")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment