Created
December 7, 2015 00:34
-
-
Save pachacamac/b589fd465c316f5d42c9 to your computer and use it in GitHub Desktop.
show images on the shell - requires imagemagick
This file contains hidden or 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 | |
def display_image(filename) | |
tw = `tput cols`.to_i | |
file = `convert #{filename} -resize #{tw}x ppm:-`.lines | |
format = file.shift.chomp | |
raise 'can only read binary format P6' unless format == 'P6' | |
width, height = file.shift.chomp.split(' ').map(&:to_i) | |
maxval = file.shift.to_i + 1 | |
bytes = file.join.bytes | |
(0...height).each do |y| | |
line = '' | |
(0...width).each do |x| | |
i = 3*(y*width+x) | |
r = bytes[ i ] * 256 / maxval | |
g = bytes[i+1] * 256 / maxval | |
b = bytes[i+2] * 256 / maxval | |
line << "\033[48;5;#{ 16 + 36 * (r/43) + 6 * (g/43) + (b/43) }m \033[0;00m" | |
end | |
puts line | |
end | |
end | |
display_image ARGV[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment