LoginSignup
3
7

More than 3 years have passed since last update.

Node.jsで画像ファイルを一括変換してみる(サイズ指定、Exif抜き、プレフィクス指定、イメージパス指定、リサイズイメージパス指定、イメージタイプ指定付き)多分・・・

Last updated at Posted at 2017-05-04

開発の経緯

写真撮るのが好きな私は個人ブログに写真をアップしたりしてるんですが、その際に毎回リサイズするだけで画像アプリを使ってるのがなんだか面倒に感じていました。

一括でできませんか?

出来ました

イメージマジックとノードが走ってるマックに下記のソースをぶっこんでコマンドラインから叩いたら画像をリサイズしてくれるじゃあありませんか。

/**
  * [Resize And Strip V.1.00]
  * Copyright (c) [2017] [(fD)Pinchos.Ponchos]
  * http://qiita.com/fd2016ta
  * This software is released under the MIT License.
  */

/**
 * [getArgument]
 * @param  {[type]} argumentIndex
 * @param  {[type]} defaultData
 * @param  {[type]} successMessage
 * @param  {[type]} errorMessage
 * @return {[type]}
 */
function getArgument(argumentIndex, defaultData, successMessage, errorMessage)
{
  var result = defaultData;
  argumentIndex;

  if (typeof process.argv[++argumentIndex] !== 'undefined') {
    //リサイズイメージのファイルメイプレフィクスの獲得 コマンドライン第一引数をファイル名にする
    result = process.argv[argumentIndex];
    console.log('Argument_' + --argumentIndex + ' ' + successMessage + result);
  } else {
    console.log('Argument_' + --argumentIndex + ' ' + errorMessage);
  }

  return result;
}

/**
 * [resizeAndStrip]
 * @param  {[type]} files
 * @return {[type]}
 */
function resizeAndStrip(files)
{
  for(var index in files) {
    console.log(path.resolve(sourcePath, files[index]));
    im.convert(
      [
        path.resolve(sourcePath, files[index]),
        '-resize', imageSize,
        '-strip',
        path.resolve(imagesConvertPath, prefix + index + '.' + imageType)
      ],
    function(err, stdout){
      if (err) {
        console.log('');
        console.log('convert error:', err);
      }
    });
  }
}

/**
 * [convertImages]
 * @return {[type]}
 */
function convertImages()
{
  //ファイル一覧を取得
  fs.readdir(
   sourcePath,
   function (err, files) {
     if (err) {
       console.log('fs error ' + err);
       throw err;
     }

     resizeAndStrip(files);

     console.log('**************** END  **********************************');
   }
 );
}

console.log('**************** STRT **********************************');

//require
var fs = require('fs');
var path = require('path');
var im = require('imagemagick');

//1 ファイルプレフィクス
var prefix = getArgument(1, 'noArg', 'resize image file prefix is ', 'argument 1 is convert image filename prefix.');
//2 リサイズイメージサイズ
var imageSize = getArgument(2, '1024x1024', 'image size is ', 'argument 2 is image size.');
//3 ファイルフォーマット
var imageType = getArgument(3, 'png', 'image type is ', 'argument 3 is image type.');
//4 ソースパス
var sourcePath = getArgument(4, path.resolve(__dirname, 'images'), 'source image path is ', 'argument 4 is source image file Relative path.');
//5 コンバート先パス
var imagesConvertPath = getArgument(5, path.resolve(__dirname, 'imagesConvert'), 'risize image path is ', 'argument 5 is risize image file Relative path.');

//コンバート開始
convertImages();

使い方

一番簡単な使い方は
* このスクリプトをどっかに置く
* スクリプトと同じディレクトリにimages,imagesConvertというフォルダを作る
* imagesディレクトリにソース画像を置く
* iTermとかでスクリプトを叩く

スクリプトをフルパスで指定して叩くだけで長辺を1024ピクセルにしたpng画像ファイルが出来上がるはずです。

でも自分で指定したいなって事も多いでしょうからその場合は引数を指定してください。

  1. ファイルプレフィクス
  2. リサイズイメージサイズ
  3. ファイルフォーマット
  4. ソースパス
  5. コンバート先パス

引数は全部指定しても良いですし、三番目まで指定とか最初の一個だけ指定でもおっけーです。

動くと思うわたぶん。
(nodeいじるの初めてなんで色々甘いところは大目にみてください・・・出典も記載したいわ・・・)

3
7
2

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