Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created July 4, 2019 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/4d779ca0c920ed06d9021a64390222ac to your computer and use it in GitHub Desktop.
Save havenwood/4d779ca0c920ed06d9021a64390222ac to your computer and use it in GitHub Desktop.
A quick spike to turn ascii art into a png
# frozen_string_literal: true
require 'chunky_png'
ascii = <<~ASCII
@@@@@@ @@@@@@
@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@@@@ @@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@
@@@@@@@@@@@@ @@@@@@@@@@@@
@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@ @@@@@@@@@@@
@@@@@@@@@@@@ @@@@@@@@@@@@
@@@@@@@@@@@@@ @@@@@@@@@@@@@
@@@@@@@@@@@@@@ @@@@@@@@@@@@@@
@@@@@@@@@@@@@ @@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@
@@@@@@@@@@@
@@@@@@@
@@@
@
ASCII
lines = ascii.lines
horizontal = lines.max_by(&:size).size
vertical = lines.size * 2
red = [255, 0, 0]
alpha = 255
filename = 'ruby.png'
png = ChunkyPNG::Image.new(horizontal, vertical, ChunkyPNG::Color::TRANSPARENT)
lines.flat_map { |line| [line, line] }.each_with_index do |line, y|
line.each_char.with_index do |char, x|
png[x, y] = ChunkyPNG::Color.rgba(*red, alpha) if char != ' '
end
end
png.save(filename, interlace: true)
@havenwood
Copy link
Author

ruby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment