Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active June 15, 2021 11:59
Show Gist options
  • Save JoshCheek/331bdec2667b3bcba19f to your computer and use it in GitHub Desktop.
Save JoshCheek/331bdec2667b3bcba19f to your computer and use it in GitHub Desktop.
Cheating at drawful

Video of this code being run: https://vimeo.com/152923160/settings


First, I would google for an image that represented what I had to draw. Then ran this code in pry to convert it into a set of x, y coordinates that should be dark:

require 'chunky_png'
canvas = ChunkyPNG::Canvas.from_file('scarface.png').grayscale
gradient = 0.1
ratex=10
ratey=10
sampled = canvas.height.times.map { |y| canvas.width.times.map { |x| (ChunkyPNG::Color.r(canvas[x, y])/255.0).to_s[0,4].to_f  }.select.with_index { |c, i| i%ratex == 0 } }.select.with_index { |c, i| i%ratey == 0 }
sampled = sampled.drop(1) if sampled.length.odd?
to_draw = sampled.map { |row| row.each_cons(2).map { |l, r| (r - l).abs > gradient } }.zip(sampled.each_cons(2).map { |top, bot| top.zip(bot).map {|t,b| (t-b).abs > gradient }  }).select { |as, bs| as && bs }.map { |as, bs| as.zip(bs).map { |a, b| (a ^ b) ? 1 : 0 } }.map(&:join).join("\n")

Then run this JRuby code to move the mouse to the x, y coordinate of the pixels, and draw them by clicking the mouse there:

r = java.awt.Robot.new
to_draw.lines.map(&:chomp).map.with_index { |l, y| l.chars.map.with_index { |isedge, x| if isedge=='1'; r.mouse_move(500+x*3, 170+y*3); r.mouse_press(java.awt.event.InputEvent::BUTTON1_DOWN_MASK); sleep(0.01); r.mouse_release(java.awt.event.InputEvent::BUTTON1_DOWN_MASK); end } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment