LoginSignup
17
3

More than 5 years have passed since last update.

Excelで佐々木希を描く with Ruby

Posted at

元ネタ

Excelで佐々木希を描く with python

環境

OS X ElCapitan 10.11.5
ruby 2.0.0p648 (macのデフォルト)

準備

terminalにて
画像処理のための rmagickとExcel操作の為の rubyXLというgemをインストールします

terminal
$ gem install rmagick
$ gem install rubyXL

ソースコード

draw_excel.rb
require 'rubyXL'
require 'RMagick'

img = Magick::ImageList.new(ARGV[0])

# Excelを作成し、最初のシートを選択
book = RubyXL::Workbook.new
sheet = book[0]

img.each_pixel do |pixel, y, x|

  # ピクセル色を16進数で取得
  color = pixel.to_color(Magick::AllCompliance, false, img.depth, true)
  color.delete!('#')

  # 塗りつぶし
  sheet.add_cell(x, y, '')
  sheet.sheet_data[x][y].change_fill(color)

  # 行、列の幅変更
  sheet.change_column_width(x, 0.01)
  sheet.change_row_height(y, 5)
end

file_name = File.basename("#{ARGV[0]}", '.*')
book.write("#{file_name}.xlsx")

実行

terminal
$ ruby draw_excel.rb sasaki.png

完成品

元データ
enter image description here

出力

enter image description here

拡大

enter image description here

おおー!(感動)

17
3
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
17
3