updated image conversion part
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,6 +3,8 @@
|
||||
*.mp3
|
||||
*.gif
|
||||
*.webm
|
||||
*.heic
|
||||
*.heif
|
||||
converted-ss/
|
||||
tmp-ss/
|
||||
node_modules/
|
||||
|
||||
@@ -1,57 +1,93 @@
|
||||
module.exports = convertToAscii = async (file = __dirname + 'input.png', output = __dirname + '/output.png', dotSize = 24, verticalSpacinModifier = 4, callback) => {
|
||||
console.log('converting image to ascii');
|
||||
const hobbitImageFile = file;
|
||||
const density = 'Ñ@#W$9876543210?!abc;:+=-,._ ';
|
||||
const { createCanvas } = require("canvas");
|
||||
const ImageDataURI = require("image-data-uri");
|
||||
var sizeOf = require("image-size");
|
||||
const { exec, execSync } = require("child_process");
|
||||
const heicToPng = require("./heicToPng");
|
||||
|
||||
const { createCanvas } = require('canvas')
|
||||
const ImageDataURI = require('image-data-uri');
|
||||
var sizeOf = require('image-size');
|
||||
var dimensions = sizeOf(hobbitImageFile);
|
||||
module.exports = convertToAscii = async (
|
||||
file = __dirname + "input.png",
|
||||
output = __dirname + "/output.png",
|
||||
dotSize = 24,
|
||||
verticalSpacingModifier = 4,
|
||||
scaledSize = 120,
|
||||
callback
|
||||
) => {
|
||||
const filePNG = file + (file.toLowerCase().endsWith(".png") ? "" : ".png");
|
||||
let fileToConvert = file;
|
||||
if (
|
||||
file.toLowerCase().endsWith(".heic") ||
|
||||
file.toLowerCase().endsWith(".heif")
|
||||
) {
|
||||
console.log("heic file got");
|
||||
await heicToPng(file, filePNG);
|
||||
fileToConvert = filePNG;
|
||||
}
|
||||
console.log("pre image resize");
|
||||
const scaledFile = "scaled-" + filePNG;
|
||||
execSync(
|
||||
`ffmpeg -i ${fileToConvert} -vf scale=${scaledSize}:-2 ${scaledFile} -y`
|
||||
);
|
||||
console.log("post image scale");
|
||||
|
||||
const hobbit = {
|
||||
await convert(scaledFile, output, dotSize, verticalSpacingModifier);
|
||||
callback(output);
|
||||
};
|
||||
|
||||
const convert = async (file, output, dotSize, verticalSpacingModifier) => {
|
||||
const density = "Ñ@#W$9876543210?!abc;:+=-,._ ";
|
||||
var dimensions = sizeOf(file);
|
||||
|
||||
const image = {
|
||||
width: dimensions.width,
|
||||
height: dimensions.height,
|
||||
pixels: [],
|
||||
}
|
||||
};
|
||||
|
||||
function clamp(input, min, max) {
|
||||
return input < min ? min : input > max ? max : input;
|
||||
}
|
||||
|
||||
function map(current, in_min, in_max, out_min, out_max) {
|
||||
const mapped = ((current - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min;
|
||||
const mapped =
|
||||
((current - in_min) * (out_max - out_min)) / (in_max - in_min) + out_min;
|
||||
return clamp(mapped, out_min, out_max);
|
||||
}
|
||||
|
||||
await new Promise((res, rej) => {
|
||||
var PNG = require('png-js');
|
||||
PNG.decode(hobbitImageFile, function(pixels) {
|
||||
var PNG = require("png-js");
|
||||
PNG.decode(file, function (pixels) {
|
||||
const canvas = createCanvas(
|
||||
image.width * dotSize,
|
||||
image.height * dotSize
|
||||
);
|
||||
const ctx = canvas.getContext("2d");
|
||||
|
||||
const canvas = createCanvas(hobbit.width * dotSize, hobbit.height * dotSize);
|
||||
const ctx = canvas.getContext('2d')
|
||||
image.pixels = pixels;
|
||||
|
||||
hobbit.pixels = pixels;
|
||||
ctx.fillStyle = "black";
|
||||
ctx.fillRect(0, 0, image.width * dotSize, image.height * dotSize);
|
||||
|
||||
ctx.fillStyle = 'black';
|
||||
ctx.fillRect(0, 0, hobbit.width * dotSize, hobbit.height * dotSize);
|
||||
|
||||
ctx.font = (dotSize + verticalSpacinModifier) + "px Courier";
|
||||
ctx.textAlign = 'center';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.font = dotSize + verticalSpacingModifier + "px Courier";
|
||||
ctx.textAlign = "center";
|
||||
ctx.textBaseline = "middle";
|
||||
ctx.fillStyle = `white`;
|
||||
|
||||
for(let x = 0; x < hobbit.width; x++) {
|
||||
for(let y = 0; y < hobbit.height; y++) {
|
||||
const pixelPosition = (x + y*hobbit.width)*4;
|
||||
const r = hobbit.pixels[pixelPosition + 0];
|
||||
const g = hobbit.pixels[pixelPosition + 1];
|
||||
const b = hobbit.pixels[pixelPosition + 2];
|
||||
const a = hobbit.pixels[pixelPosition + 3];
|
||||
for (let x = 0; x < image.width; x++) {
|
||||
for (let y = 0; y < image.height; y++) {
|
||||
const pixelPosition = (x + y * image.width) * 4;
|
||||
const r = image.pixels[pixelPosition + 0];
|
||||
const g = image.pixels[pixelPosition + 1];
|
||||
const b = image.pixels[pixelPosition + 2];
|
||||
const a = image.pixels[pixelPosition + 3];
|
||||
|
||||
const avg = Math.floor((r + g + b) / 3) * (a / 255);
|
||||
|
||||
const gray = map(255 - avg, 0, 255, 0, density.length);
|
||||
ctx.fillText(density.charAt(gray), x*dotSize + dotSize/2, y*dotSize + dotSize/2);
|
||||
ctx.fillText(
|
||||
density.charAt(gray),
|
||||
x * dotSize + dotSize / 2,
|
||||
y * dotSize + dotSize / 2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,5 +95,4 @@ module.exports = convertToAscii = async (file = __dirname + 'input.png', output
|
||||
res(1);
|
||||
});
|
||||
});
|
||||
callback(output);
|
||||
}
|
||||
};
|
||||
|
||||
12
heicToPng.js
Normal file
12
heicToPng.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const { promisify } = require("util");
|
||||
const fs = require("fs");
|
||||
const convert = require("heic-convert");
|
||||
|
||||
module.exports = async function heicToPng(input, output) {
|
||||
const inputBuffer = await promisify(fs.readFile)(input);
|
||||
const outputBuffer = await convert({
|
||||
buffer: inputBuffer,
|
||||
format: "PNG",
|
||||
});
|
||||
await promisify(fs.writeFile)(output, outputBuffer);
|
||||
};
|
||||
187
index.js
187
index.js
@@ -21,10 +21,10 @@ const convertToAscii = require("./convertToAscii");
|
||||
const extractFrames = require("./extractFrames");
|
||||
const gifToMp4 = require("./gifToMp4");
|
||||
const scaleVideo = require("./scaleVideo");
|
||||
const fs = require('fs');
|
||||
const fs = require("fs");
|
||||
const makeVideoFromImages = require("./makeVideoFromImages");
|
||||
const mp4ToGif = require("./mp4ToGif");
|
||||
const fsExtra = require('fs-extra');
|
||||
const fsExtra = require("fs-extra");
|
||||
|
||||
args = process.argv;
|
||||
|
||||
@@ -37,67 +37,91 @@ const randomName = (Math.random() + 1).toString(36).substring(2);
|
||||
|
||||
const action = args.shift();
|
||||
|
||||
let usage = '';
|
||||
let example = '';
|
||||
|
||||
let usage = "";
|
||||
let example = "";
|
||||
|
||||
let input;
|
||||
let output;
|
||||
let pixelSize;
|
||||
let pixelModifyer;
|
||||
let pixelModifier;
|
||||
let scaledSize;
|
||||
let fps;
|
||||
|
||||
|
||||
|
||||
const _scaleVideo = (inputFile, pixelSize, pixelModifyer, scaledSize, fps, output, convertToGif = false, deleteInputFile = false) => {
|
||||
scaleVideo(inputFile, 'mp4-scaled-' + randomName + '.mp4', scaledSize, fps, (scaledFile) => {
|
||||
const _scaleVideo = (
|
||||
inputFile,
|
||||
pixelSize,
|
||||
pixelModifier,
|
||||
scaledSize,
|
||||
fps,
|
||||
output,
|
||||
convertToGif = false,
|
||||
deleteInputFile = false
|
||||
) => {
|
||||
scaleVideo(
|
||||
inputFile,
|
||||
"mp4-scaled-" + randomName + ".mp4",
|
||||
scaledSize,
|
||||
fps,
|
||||
(scaledFile) => {
|
||||
if (deleteInputFile) {
|
||||
fs.rmSync(inputFile);
|
||||
}
|
||||
const outputFolder = __dirname + '/extracted-frames-'+randomName;
|
||||
const outputFolder = __dirname + "/extracted-frames-" + randomName;
|
||||
extractFrames(scaledFile, outputFolder, async () => {
|
||||
fs.rmSync(scaledFile);
|
||||
let files = fs.readdirSync(outputFolder);
|
||||
const convertedFolder = __dirname + '/converted-frames-'+randomName;
|
||||
const convertedFolder = __dirname + "/converted-frames-" + randomName;
|
||||
let index = 0;
|
||||
for (let file of files) {
|
||||
index++;
|
||||
await convertToAscii(outputFolder + '/' + file, convertedFolder + '/' + file, pixelSize, pixelModifyer, () => {
|
||||
console.log(index, '/', files.length);
|
||||
fs.rmSync(outputFolder + '/' + file);
|
||||
});
|
||||
await convertToAscii(
|
||||
outputFolder + "/" + file,
|
||||
convertedFolder + "/" + file,
|
||||
pixelSize,
|
||||
pixelModifier,
|
||||
scaledSize,
|
||||
() => {
|
||||
console.log(index, "/", files.length);
|
||||
fs.rmSync(outputFolder + "/" + file);
|
||||
}
|
||||
);
|
||||
}
|
||||
fsExtra.emptyDirSync(outputFolder);
|
||||
fs.rmdirSync(outputFolder);
|
||||
makeVideoFromImages(convertedFolder + '/%d.png', __dirname + 'generated-video-' + randomName + '.mp4', fps, fps, (generatedVideoFIle) => {
|
||||
makeVideoFromImages(
|
||||
convertedFolder + "/%d.png",
|
||||
__dirname + "generated-video-" + randomName + ".mp4",
|
||||
fps,
|
||||
fps,
|
||||
(generatedVideoFIle) => {
|
||||
fsExtra.emptyDirSync(convertedFolder);
|
||||
fs.rmdirSync(convertedFolder);
|
||||
if (convertToGif) {
|
||||
mp4ToGif(generatedVideoFIle, output, (gif) => {
|
||||
console.log('gif generated in: ' + gif);
|
||||
console.log("gif generated in: " + gif);
|
||||
});
|
||||
} else {
|
||||
fs.renameSync(generatedVideoFIle, output);
|
||||
console.log('video generated in: ' + output);
|
||||
console.log("video generated in: " + output);
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
switch (action) {
|
||||
case 'img':
|
||||
|
||||
case "img":
|
||||
usage = `Usage: npm run convert:img input-image-path.png[optional, default:./input.png] output-image-path.png[optional, default:./output.png] pixel-size[optional, default:24] font-size-modifier[optional, degault:4]`;
|
||||
example = `Example: npm run convert:img input.png output.png 24 0`;
|
||||
|
||||
if (args[0] === 'help') {
|
||||
if (args[0] === "help") {
|
||||
console.log(`${usage}\n${example}`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (args.length > 4) {
|
||||
if (args.length > 5) {
|
||||
console.error(`Invalid parameter count!\n${usage}\n${example}`);
|
||||
process.exit(0);
|
||||
}
|
||||
@@ -108,21 +132,33 @@ switch (action) {
|
||||
}
|
||||
|
||||
if (args[3] && isNaN(parseInt(args[3], base))) {
|
||||
console.error(`Font size modifier must be a number!\n${usage}\n${example}`);
|
||||
console.error(
|
||||
`Font size modifier must be a number!\n${usage}\n${example}`
|
||||
);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
convertToAscii(args[0], args[1], args[2], args[3], () => {
|
||||
if (args[4] && isNaN(parseInt(args[4], base))) {
|
||||
console.error(`Scaled size must be a number!\n${usage}\n${example}`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
input = args[0];
|
||||
output = args[1];
|
||||
pixelSize = parseInt(args[2], base);
|
||||
pixelModifier = parseInt(args[3], base);
|
||||
scaledSize = parseInt(args[4], base);
|
||||
|
||||
convertToAscii(input, output, pixelSize, pixelModifier, scaledSize, () => {
|
||||
console.log(`Image converted and saved in ${args[1]}`);
|
||||
});
|
||||
|
||||
break;
|
||||
case 'gif:gif':
|
||||
|
||||
case "gif:gif":
|
||||
usage = `Usage: npm run convert:gif:gif input-image-path.png[optional, default:./input.gif] output-image-path.png[optional, default:./output.gif] pixel-size[optional, default:24] font-size-modifier[optional, degault:4] scaled-size[optional, default=120] fps[optional, default=10]`;
|
||||
example = `Example: npm run convert:gif:gif input.gif output.gif 24 4 120 10`;
|
||||
|
||||
if (args[0] === 'help') {
|
||||
if (args[0] === "help") {
|
||||
console.log(`${usage}\n${example}`);
|
||||
process.exit(0);
|
||||
}
|
||||
@@ -138,7 +174,9 @@ switch (action) {
|
||||
}
|
||||
|
||||
if (args[3] && isNaN(parseInt(args[3], base))) {
|
||||
console.error(`Font size modifier must be a number!\n${usage}\n${example}`);
|
||||
console.error(
|
||||
`Font size modifier must be a number!\n${usage}\n${example}`
|
||||
);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -155,22 +193,29 @@ switch (action) {
|
||||
input = args[0];
|
||||
output = args[1];
|
||||
pixelSize = parseInt(args[2], base);
|
||||
pixelModifyer = parseInt(args[3], base);
|
||||
pixelModifier = parseInt(args[3], base);
|
||||
scaledSize = parseInt(args[4], base);
|
||||
fps = parseInt(args[5], base);
|
||||
|
||||
|
||||
gifToMp4(input, 'gif-to-mp4-'+randomName+'.mp4', (convertedFile) => {
|
||||
_scaleVideo(convertedFile, pixelSize, pixelModifyer, scaledSize, fps, output, true, true);
|
||||
gifToMp4(input, "gif-to-mp4-" + randomName + ".mp4", (convertedFile) => {
|
||||
_scaleVideo(
|
||||
convertedFile,
|
||||
pixelSize,
|
||||
pixelModifier,
|
||||
scaledSize,
|
||||
fps,
|
||||
output,
|
||||
true,
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
break;
|
||||
case 'gif:mp4':
|
||||
|
||||
case "gif:mp4":
|
||||
usage = `Usage: npm run convert:gif:mp4 input-image-path.gif[optional, default:./input.gif] output-image-path.mp4[optional, default:./output.mp4] pixel-size[optional, default:24] font-size-modifier[optional, degault:4] scaled-size[optional, default=120] fps[optional, default=10]`;
|
||||
example = `Example: npm run convert:gif:mp4 input.gif output.mp4 24 4 120 10`;
|
||||
|
||||
if (args[0] === 'help') {
|
||||
if (args[0] === "help") {
|
||||
console.log(`${usage}\n${example}`);
|
||||
process.exit(0);
|
||||
}
|
||||
@@ -186,7 +231,9 @@ switch (action) {
|
||||
}
|
||||
|
||||
if (args[3] && isNaN(parseInt(args[3], base))) {
|
||||
console.error(`Font size modifier must be a number!\n${usage}\n${example}`);
|
||||
console.error(
|
||||
`Font size modifier must be a number!\n${usage}\n${example}`
|
||||
);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -203,22 +250,29 @@ switch (action) {
|
||||
input = args[0];
|
||||
output = args[1];
|
||||
pixelSize = parseInt(args[2], base);
|
||||
pixelModifyer = parseInt(args[3], base);
|
||||
pixelModifier = parseInt(args[3], base);
|
||||
scaledSize = parseInt(args[4], base);
|
||||
fps = parseInt(args[5], base);
|
||||
|
||||
|
||||
gifToMp4(input, 'gif-to-mp4-'+randomName+'.mp4', (convertedFile) => {
|
||||
_scaleVideo(convertedFile, pixelSize, pixelModifyer, scaledSize, fps, output, false, true);
|
||||
gifToMp4(input, "gif-to-mp4-" + randomName + ".mp4", (convertedFile) => {
|
||||
_scaleVideo(
|
||||
convertedFile,
|
||||
pixelSize,
|
||||
pixelModifier,
|
||||
scaledSize,
|
||||
fps,
|
||||
output,
|
||||
false,
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
break;
|
||||
case 'mp4:gif':
|
||||
|
||||
case "mp4:gif":
|
||||
usage = `Usage: npm run convert:mp4:gif input-image-path.mp4[optional, default:./input.mp4] output-image-path.gif[optional, default:./output.gif] pixel-size[optional, default:24] font-size-modifier[optional, degault:4] scaled-size[optional, default=120] fps[optional, default=10]`;
|
||||
example = `Example: npm run convert:mp4:gif input.mp4 output.gif 24 4 120 10`;
|
||||
|
||||
if (args[0] === 'help') {
|
||||
if (args[0] === "help") {
|
||||
console.log(`${usage}\n${example}`);
|
||||
process.exit(0);
|
||||
}
|
||||
@@ -234,7 +288,9 @@ switch (action) {
|
||||
}
|
||||
|
||||
if (args[3] && isNaN(parseInt(args[3], base))) {
|
||||
console.error(`Font size modifier must be a number!\n${usage}\n${example}`);
|
||||
console.error(
|
||||
`Font size modifier must be a number!\n${usage}\n${example}`
|
||||
);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -251,20 +307,27 @@ switch (action) {
|
||||
input = args[0];
|
||||
output = args[1];
|
||||
pixelSize = parseInt(args[2], base);
|
||||
pixelModifyer = parseInt(args[3], base);
|
||||
pixelModifier = parseInt(args[3], base);
|
||||
scaledSize = parseInt(args[4], base);
|
||||
fps = parseInt(args[5], base);
|
||||
|
||||
|
||||
_scaleVideo(input, pixelSize, pixelModifyer, scaledSize, fps, output, true, false);
|
||||
_scaleVideo(
|
||||
input,
|
||||
pixelSize,
|
||||
pixelModifier,
|
||||
scaledSize,
|
||||
fps,
|
||||
output,
|
||||
true,
|
||||
false
|
||||
);
|
||||
|
||||
break;
|
||||
case 'mp4:mp4':
|
||||
|
||||
case "mp4:mp4":
|
||||
usage = `Usage: npm run convert:mp4:mp4 input-image-path.mp4[optional, default:./input.mp4] output-image-path.mp4[optional, default:./output.mp4] pixel-size[optional, default:24] font-size-modifier[optional, degault:4] scaled-size[optional, default=120] fps[optional, default=10]`;
|
||||
example = `Example: npm run convert:mp4:mp4 input.mp4 output.mp4 24 4 120 10`;
|
||||
|
||||
if (args[0] === 'help') {
|
||||
if (args[0] === "help") {
|
||||
console.log(`${usage}\n${example}`);
|
||||
process.exit(0);
|
||||
}
|
||||
@@ -280,7 +343,9 @@ switch (action) {
|
||||
}
|
||||
|
||||
if (args[3] && isNaN(parseInt(args[3], base))) {
|
||||
console.error(`Font size modifier must be a number!\n${usage}\n${example}`);
|
||||
console.error(
|
||||
`Font size modifier must be a number!\n${usage}\n${example}`
|
||||
);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -297,12 +362,20 @@ switch (action) {
|
||||
input = args[0];
|
||||
output = args[1];
|
||||
pixelSize = parseInt(args[2], base);
|
||||
pixelModifyer = parseInt(args[3], base);
|
||||
pixelModifier = parseInt(args[3], base);
|
||||
scaledSize = parseInt(args[4], base);
|
||||
fps = parseInt(args[5], base);
|
||||
|
||||
|
||||
_scaleVideo(input, pixelSize, pixelModifyer, scaledSize, fps, output, false, false);
|
||||
_scaleVideo(
|
||||
input,
|
||||
pixelSize,
|
||||
pixelModifier,
|
||||
scaledSize,
|
||||
fps,
|
||||
output,
|
||||
false,
|
||||
false
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
|
||||
@@ -15,10 +15,13 @@
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"canvas": "^2.9.0",
|
||||
"ffmpeg": "0.0.4",
|
||||
"ffmpeg-extract-frames": "^2.0.2",
|
||||
"fs-extra": "^10.0.0",
|
||||
"heic-convert": "^1.2.4",
|
||||
"image-data-uri": "^2.0.1",
|
||||
"image-size": "^1.0.1",
|
||||
"png-js": "^1.0.0"
|
||||
"png-js": "^1.0.0",
|
||||
"util": "^0.12.4"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
## Commands
|
||||
|
||||
### Options
|
||||
|
||||
- input-file
|
||||
- file to be converted (original is kept)
|
||||
- default: input.mp4/input.gif/input.png depending on command used
|
||||
@@ -52,4 +53,4 @@
|
||||
|
||||
### Convert PNG to ASCII PNG
|
||||
|
||||
`npm run convert:img [input-file] [output-file] [pixel-size] [font-size-modifier]`
|
||||
`npm run convert:img [input-file] [output-file] [pixel-size] [font-size-modifier] [scaled-size]`
|
||||
|
||||
Reference in New Issue
Block a user