LoginSignup
39
35

More than 5 years have passed since last update.

ターミナルに画像を表示する

Posted at

 概要と元ネタ。

terminal で画像を cat できるようにした - 終わる世界とコンテンツ

#!/usr/bin/env ruby

require 'curses'
require 'rmagick'

Curses.init_screen
columns = Curses.cols / 2 
Curses.close_screen

ARGV.each {|filename|
  source = Magick::Image.read(filename).first
  image = source.sample(1.0 * columns / source.columns)

  puts (0...image.rows).map {|row|
    image.get_pixels(0, row, image.columns, 1).map {|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"
    }.join
  }.join("\n")
}

デース!

楽しい! ✌(’ω’✌ )三✌(’ω’)✌三( ✌’ω’)✌

ANSI escape code - Wikipedia

39
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
39
35