Last active
February 11, 2022 10:17
-
-
Save jozsefDevs/ebf4ee3c890b8cb79f8355d6bafd11bf to your computer and use it in GitHub Desktop.
Have a nicer interface over ImageMagick to easily combine images horizontally/vertically from command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Prerequsite to run this script: | |
* - ImageMagick installed | |
* - Node (>= 12LTS) | |
* | |
* Save this somewhere on your machine as a node script (my choice is usually ~/bin/node_scripts) | |
* | |
* After alias it in zshrc as: | |
* alias image_combine="node ~/node_scripts/image-combine.js" | |
* | |
* Use it like anywhere from command line like (by default combining images vertically): | |
* imagecombine file1.png file2.png fileout.png [--horizontal] | |
*/ | |
const { spawnSync } = require('child_process'); | |
const directionMap = { '--vertical': '+append', '--horizontal': '-append' }; | |
let userPreference = '--vertical'; | |
if (process.argv.filter(x => x === '--horizontal').length) { | |
userPreference = '--horizontal' | |
} | |
const cleanArgs = () => { | |
const args = [...process.argv.slice(2)].filter(x => !x.startsWith('--')); | |
return args; | |
} | |
const processArgs = cleanArgs(); | |
if (!processArgs.length || processArgs.length < 3) { | |
console.log('Please provide arguments in the following format: fileIn1.png fileIn2.png fileOut.png [--horizontal]') | |
return; | |
} | |
console.log(`Running in ${userPreference} mode`); | |
let sp = spawnSync('convert', [directionMap[userPreference], ...processArgs], { stdio: ['inherit', 0, 'inherit']}); | |
if (!sp.status) { | |
console.log(`Wonderful! Your image has been created! ;)`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment