Last active
August 29, 2015 14:00
-
-
Save quanon/3732aaa9d14c40e9a7a4 to your computer and use it in GitHub Desktop.
ターミナルで Gif アニメを表示する。
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 'curses' | |
require 'rmagick' | |
require 'pry' | |
require 'pry-debugger' | |
def clear_screen | |
puts "\e[H\e[2J" | |
end | |
def pixel2color(pixel) | |
color = [pixel.red, pixel.green, pixel.blue].map { |n| (n * 5) / (255 * 255) } | |
"\x1b[48;5;#{16 + color[0] * 36 + color[1] * 6 + color[2]}m \x1b[0m" | |
end | |
def screen_columns | |
Curses.init_screen | |
columns = Curses.cols / 2 | |
Curses.close_screen | |
columns | |
end | |
def draw(image) | |
image = image.sample(1.0 * screen_columns / image.columns) | |
rows = (0...image.rows).map do |row| | |
image.get_pixels(0, row, image.columns, 1).map { |pixel| pixel2color(pixel) }.join | |
end | |
picture = rows.join("\n") | |
puts picture | |
end | |
def animate(image_path) | |
image_list = Magick::ImageList.new(image_path) | |
interval = (1.0 * image_list.delay) / 100 | |
image_list.each do |image| | |
draw(image) | |
sleep(interval) | |
end | |
end | |
def main | |
interrupted = false | |
Signal.trap("INT") { interrupted = true } # traps Ctrl-C | |
until interrupted do | |
animate(ARGV[0]) | |
end | |
clear_screen | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment