LoginSignup
31
30

More than 5 years have passed since last update.

Excelで佐々木希を描く with Node.js

Last updated at Posted at 2016-06-11

【環境】

 windows8.1
 Excel 2013
 node.js 4.4.5
 npm 2.15.5

【概要】

 佐々木希の写真から色の情報を取得して、Excelのセルに塗りつぶします。

【フォルダ構成】

|---sasaki_excel
      |---sasaki_excel.js
      |---sasaki.jpg(佐々木希の画像)

こちらの画像を使用しました。
sasaki.jpg

【プログラム】

 Pythonの時と比べると短くコーディングできるのがいいね!

sasaki_excel.js
var Jimp = require("jimp");
var Excel = require("exceljs");

// 新しくExcel作成
var wb = new Excel.Workbook();
var ws = wb.addWorksheet("Nozomi Sheet");

Jimp.read("./sasaki.jpg", function (err, sasaki) {
    sasaki.scan(0, 0, sasaki.bitmap.width, sasaki.bitmap.height, function (x, y, idx) {
        // RGBA値を16進数へ変換
        var r = this.bitmap.data[ idx + 0 ].toString(16);
        var g = this.bitmap.data[ idx + 1 ].toString(16);
        var b = this.bitmap.data[ idx + 2 ].toString(16);
        var a = this.bitmap.data[ idx + 3 ].toString(16);
        var nozomi_argb = a + r + g + b;

        var row = ws.getRow(y)
        row.height = 1.5;
        ws.getColumn(x+1).width = 0.3;

        // 塗りつぶし
        row.getCell(x+1).value = " ";
        row.getCell(x+1).fill = {
          type: 'pattern',
          pattern:'solid',
          fgColor:{argb: nozomi_argb}
        };
    });
    // Excel保存
    wb.xlsx.writeFile('sasaki_nozomi.xlsx');
});

【結果】

nozomi_excel.png

【問題点】

 赤や黄色の点があちこちに描画されてしまう。
 (改善点はありそうかな?・・・('_'))

【参考サイト】

 jimp-github
 exceljs-github

【他言語版】

 Python版はこちら
 Ruby版はこちら

31
30
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
31
30