Skip to content

Instantly share code, notes, and snippets.

@FrostBird347
Last active June 23, 2023 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrostBird347/22f3f8892eb231e976b9d50a9223f8ec to your computer and use it in GitHub Desktop.
Save FrostBird347/22f3f8892eb231e976b9d50a9223f8ec to your computer and use it in GitHub Desktop.
An extremely simple script that applies the differences between 2 images to a third image. It has not been tested on other systems, assigns a massive amount of ram, can't open images close to or above 1GB in size and doesn't support images with an alpha channel. With all that being said, it is very useful when copying effects from a low res imag…
//An extremely simple script that applies the differences between 2 images to a third image.
//It has not been tested on other systems, assigns a massive amount of ram, can't open images close to or above 1GB in size and doesn't support images with an alpha channel.
//With all that being said, it is very useful when copying effects from a low res image to a higher res version of the same image.
const qoi = require('qoijs');
const fs = require('fs');
const execFileSync = require('child_process').execFileSync;
if (process.argv.length != 6) {
console.error("Invalid arguments!");
console.log("Usage:\n copyimgdif original_image_a edited_image_a image_b output_image\n (to apply the changes made to image_a to image_b)");
process.exit(1);
}
let imgBData;
let imgOrigAData;
let imgEditAData;
let _tempString = process.argv[4];
try {
imgBData = qoi.decode(execFileSync("/usr/local/bin/convert", [process.argv[4], "-alpha", "on", "QOI:-"], {maxBuffer: 1000000000}));
_tempString = process.argv[2];
imgOrigAData = qoi.decode(execFileSync("/usr/local/bin/convert", [process.argv[2], "-alpha", "on", "-resize", imgBData.width + "x" + imgBData.height + "!", "QOI:-"], {maxBuffer: 1000000000}));
_tempString = process.argv[3];
imgEditAData = qoi.decode(execFileSync("/usr/local/bin/convert", [process.argv[3], "-alpha", "on", "-resize", imgBData.width + "x" + imgBData.height + "!", "QOI:-"], {maxBuffer: 1000000000}));
} catch(err) {
console.error(err.message);
console.error(_tempString);
process.exit(1);
}
let diffData = [];
for (let i = 0; i < imgBData.data.length; i++) {
if (i % 4 == 3) {
diffData.push(255);
} else {
let diffValue = imgBData.data[i] + (imgEditAData.data[i] - imgOrigAData.data[i]);
diffData.push(Math.min(255, Math.max(0, diffValue)));
}
}
const finalDiffData = new Uint8Array(diffData);
const outputBuffer = qoi.encode(finalDiffData, {
width: imgBData.width,
height: imgBData.height,
channels: 4,
colorspace: 0
});
execFileSync("/usr/local/bin/convert", ["QOI:-", process.argv[5]], {input: new Buffer(outputBuffer)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment