Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Created December 7, 2015 00:34
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 pachacamac/b589fd465c316f5d42c9 to your computer and use it in GitHub Desktop.
Save pachacamac/b589fd465c316f5d42c9 to your computer and use it in GitHub Desktop.
show images on the shell - requires imagemagick
#!/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