Skip to content

Instantly share code, notes, and snippets.

@sidoh
Created December 11, 2017 00:04
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 sidoh/41a06173f1e4714cf573de1d05f1651e to your computer and use it in GitHub Desktop.
Save sidoh/41a06173f1e4714cf573de1d05f1651e to your computer and use it in GitHub Desktop.
Convert a PNG to a black/white bitfield
#!/usr/bin/env ruby
require 'chunky_png'
bg_color = ChunkyPNG::Color.rgba(255, 255, 255, 0)
file = ARGV[0]
out_file = ARGV[1]
img = ChunkyPNG::Image.from_file(file)
File.open(out_file, 'w') do |f|
img
.pixels
.map do |px|
px == bg_color ? 0 : 1
end
.each_slice(8) do |bits|
f.write bits.join.to_i(2).chr
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment