Browse Source

Added extract generator CLI

rigwild 11 months ago
parent
commit
314d43a9b6
4 changed files with 115 additions and 3 deletions
  1. 76 0
      generateExtracts.js
  2. 2 1
      package.json
  3. 1 1
      server/routes/getImageExtracts.js
  4. 36 1
      yarn.lock

+ 76 - 0
generateExtracts.js

@@ -0,0 +1,76 @@
+'use strict'
+/* eslint-disable no-global-assign */
+/* eslint-disable no-native-reassign */
+
+require = require('esm')(module) // ES Module loader
+const fs = require('fs-extra')
+const yargs = require('yargs')
+const path = require('path')
+
+const { getImage } = require('./server/routes/getImage')
+const { cutImage } = require('./server/routes/getImageExtracts')
+
+/**
+ * @typedef {Object} CliObject
+ * @property {String} sceneName Input scene. Must be in the `./images` directory
+ * @property {String} [outputDir] Directory where to output extracts. Defaults to the extracts directory
+ * @property {String} [extractConfigX=4] Horizontal extracting configuration. Defaults to 4
+ * @property {String} [extractConfigY=4] Vertical extracting configuration. Defaults to 4
+ * @property {String} $0 The script name or node command
+ */
+/**
+ * @constant
+ * @type {CliObject}
+ */
+const cli = yargs
+  .usage('Cut a scene using provided configuration.')
+  .usage('Usage: node generateExtracts.js --sceneName=<sceneName> --quality=<quality> [--extractConfigX=<extractConfigX=4>] [--extractConfigY=<extractConfigY=4>] [--outputDir=<outputDir>]')
+  .example('node generateExtracts.js --sceneName=cuisine01 --quality=150 --extractConfigX=4 --extractConfigY=5 --outputDir=extracts')
+  .option('sceneName', {
+    demandOption: true,
+    describe: 'Input scene. Must be in the `./images` directory',
+    type: 'string'
+  })
+  .option('quality', {
+    demandOption: true,
+    describe: 'Scene quality to choose.',
+    type: Number
+  })
+  .option('extractConfigX', {
+    demandOption: false,
+    describe: 'Horizontal extracting configuration. Defaults to 4.',
+    type: Number,
+    default: 4
+  })
+  .option('extractConfigY', {
+    demandOption: false,
+    describe: 'Vertical extracting configuration. Defaults to 4.',
+    type: Number,
+    default: 4
+  })
+  .option('outputDir', {
+    demandOption: false,
+    describe: 'Directory where to output extracts. Defaults to the extracts directory.',
+    type: 'string'
+  })
+  .help('h')
+  .alias('h', 'help')
+  .wrap(yargs.terminalWidth())
+  .argv
+
+
+const setup = async () => {
+  const { sceneName, quality, extractConfigX, extractConfigY, outputDir } = cli
+  console.log(sceneName, quality, extractConfigX, extractConfigY, outputDir)
+  // Get the image path and link
+  const image = await getImage(sceneName, quality)
+
+  // Cut the image
+  const extracts = await cutImage(image, extractConfigX, extractConfigY)
+  console.log(extracts)
+
+  // Move extracts images if outputDir is specified
+  if (outputDir)
+    await Promise.all(extracts.map(x => fs.move(x.path, path.resolve(outputDir, path.basename(x.path)))))
+}
+setup()

+ 2 - 1
package.json

@@ -79,7 +79,8 @@
     "vuetify": "^1.5.14",
     "vuetify-loader": "^1.2.2",
     "vuex": "^3.1.0",
-    "vuex-persist": "^2.0.0"
+    "vuex-persist": "^2.0.0",
+    "yargs": "^13.2.4"
   },
   "postcss": {
     "plugins": {

+ 1 - 1
server/routes/getImageExtracts.js

@@ -151,7 +151,7 @@ const router = express.Router()
  * @param {Number} yExtracts the number of extract to do on the vertical axis (integer)
  * @returns {Promise<Image[]>} the list of extracted images
  */
-const cutImage = async (image, xExtracts, yExtracts) => {
+export const cutImage = async (image, xExtracts, yExtracts) => {
   const input = sharp(image.path)
 
   const { width, height } = await input.metadata()

+ 36 - 1
yarn.lock

@@ -4437,6 +4437,11 @@ get-caller-file@^1.0.1:
   resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.3.tgz#f978fa4c90d1dfe7ff2d6beda2a515e713bdcf4a"
   integrity sha512-3t6rVToeoZfYSGd8YoLFR2DJkiQrIiUrGcjvFX2mDw3bn6k2OtwHN0TNCLbBO+w8qTvimhDkv+LSscbJY1vE6w==
 
+get-caller-file@^2.0.1:
+  version "2.0.5"
+  resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
+  integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
+
 get-port@^4.2.0:
   version "4.2.0"
   resolved "https://registry.yarnpkg.com/get-port/-/get-port-4.2.0.tgz#e37368b1e863b7629c43c5a323625f95cf24b119"
@@ -6892,7 +6897,7 @@ os-locale@^2.0.0:
     lcid "^1.0.0"
     mem "^1.1.0"
 
-os-locale@^3.0.0:
+os-locale@^3.0.0, os-locale@^3.1.0:
   version "3.1.0"
   resolved "https://registry.yarnpkg.com/os-locale/-/os-locale-3.1.0.tgz#a802a6ee17f24c10483ab9935719cef4ed16bf1a"
   integrity sha512-Z8l3R4wYWM40/52Z+S265okfFj8Kt2cC2MKY+xNi3kFs+XGI7WXu/I309QQQYbRW4ijiZ+yxs9pqEhJh0DqW3Q==
@@ -8130,6 +8135,11 @@ require-main-filename@^1.0.1:
   resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1"
   integrity sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE=
 
+require-main-filename@^2.0.0:
+  version "2.0.0"
+  resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b"
+  integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==
+
 require-precompiled@^0.1.0:
   version "0.1.0"
   resolved "https://registry.yarnpkg.com/require-precompiled/-/require-precompiled-0.1.0.tgz#5a1b52eb70ebed43eb982e974c85ab59571e56fa"
@@ -10114,6 +10124,14 @@ yargs-parser@^11.1.1:
     camelcase "^5.0.0"
     decamelize "^1.2.0"
 
+yargs-parser@^13.1.0:
+  version "13.1.1"
+  resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-13.1.1.tgz#d26058532aa06d365fe091f6a1fc06b2f7e5eca0"
+  integrity sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==
+  dependencies:
+    camelcase "^5.0.0"
+    decamelize "^1.2.0"
+
 yargs-parser@^9.0.2:
   version "9.0.2"
   resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-9.0.2.tgz#9ccf6a43460fe4ed40a9bb68f48d43b8a68cc077"
@@ -10157,6 +10175,23 @@ yargs@^11.0.0:
     y18n "^3.2.1"
     yargs-parser "^9.0.2"
 
+yargs@^13.2.4:
+  version "13.2.4"
+  resolved "https://registry.yarnpkg.com/yargs/-/yargs-13.2.4.tgz#0b562b794016eb9651b98bd37acf364aa5d6dc83"
+  integrity sha512-HG/DWAJa1PAnHT9JAhNa8AbAv3FPaiLzioSjCcmuXXhP8MlpHO5vwls4g4j6n30Z74GVQj8Xa62dWVx1QCGklg==
+  dependencies:
+    cliui "^5.0.0"
+    find-up "^3.0.0"
+    get-caller-file "^2.0.1"
+    os-locale "^3.1.0"
+    require-directory "^2.1.1"
+    require-main-filename "^2.0.0"
+    set-blocking "^2.0.0"
+    string-width "^3.0.0"
+    which-module "^2.0.0"
+    y18n "^4.0.0"
+    yargs-parser "^13.1.0"
+
 yorkie@^2.0.0:
   version "2.0.0"
   resolved "https://registry.yarnpkg.com/yorkie/-/yorkie-2.0.0.tgz#92411912d435214e12c51c2ae1093e54b6bb83d9"