Created
December 11, 2017 00:04
-
-
Save sidoh/41a06173f1e4714cf573de1d05f1651e to your computer and use it in GitHub Desktop.
Convert a PNG to a black/white bitfield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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