🧪 Trying out a new histogram lib

This commit is contained in:
2022-05-03 08:47:11 -07:00
parent d19701198e
commit c8d10a91c6
5 changed files with 1213 additions and 63 deletions

1161
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -52,6 +52,7 @@
"filename-parser": "^1.0.4",
"fs-extra": "^10.0.0",
"imghash": "^0.0.9",
"jimp": "^0.16.1",
"jsdom": "^15.2.1",
"klaw": "^4.0.1",
"leven": "^3.1.0",

View File

@@ -8,7 +8,11 @@ import {
Errors,
} from "moleculer";
import path from "path";
import { analyze, getColorHistogramData, resizeImage } from "../utils/imagetransformation.utils";
import {
analyze,
getColorHistogramData,
resizeImage,
} from "../utils/imagetransformation.utils";
export default class ImageTransformation extends Service {
// @ts-ignore
public constructor(
@@ -62,15 +66,23 @@ export default class ImageTransformation extends Service {
const url = new URL(ctx.params.imageFilePath);
const pathName = url.pathname;
const decodedImageFileURI = decodeURI(pathName);
const finalImagePath = path.resolve("." + decodedImageFileURI);
const analyzedData = await analyze(finalImagePath);
const colorHistogramData = await getColorHistogramData(finalImagePath, false);
const finalImagePath = path.resolve(
"." + decodedImageFileURI
);
const analyzedData = await analyze(
finalImagePath
);
const colorHistogramData =
await getColorHistogramData(
finalImagePath,
false
);
return {
analyzedData,
colorHistogramData,
}
};
},
},
},

View File

@@ -3,7 +3,7 @@ import { ISharpResizedImageStats } from "threetwo-ui-typings";
const imghash = require("imghash");
const leven = require("leven");
import { isNull, reject } from "lodash";
import fs from "fs";
import Jimp from "jimp";
export const extractMetadataFromImage = async (
imageFilePath: string
@@ -53,56 +53,33 @@ export const getColorHistogramData = async (
inputFilePath: string | Buffer,
isValueHistogram: Boolean
) => {
const { data, info } = await sharp(inputFilePath)
// output the raw pixels
.raw()
.toBuffer({ resolveWithObject: true });
const src = new Uint32Array(data.buffer);
// const src = new Uint8ClampedArray(data.buffer);
console.log(src);
return new Promise(async (resolve, reject) => {
sharp(inputFilePath)
.toBuffer()
.then((new_image) => {
let index = 0;
let rgb_values = { r: [], g: [], b: [] };
while (index < new_image.length) {
let histBrightness = new Array(256).fill(0);
let histR = new Array(256).fill(0);
let histG = new Array(256).fill(0);
let histB = new Array(256).fill(0);
let point = {
red: new_image[index] & 0xFF,
green: (new_image[index + 1] >> 8) & 0xFF,
blue: (new_image[index + 2] >> 16) & 0xFF,
};
for (let i = 0; i < src.length; i++) {
let r = src[i] & 0xff;
let g = (src[i] >> 8) & 0xff;
let b = (src[i] >> 16) & 0xff;
histBrightness[r]++;
histBrightness[g]++;
histBrightness[b]++;
histR[r]++;
histG[g]++;
histB[b]++;
}
rgb_values.r.push(point.red);
rgb_values.g.push(point.green);
rgb_values.b.push(point.blue);
let maxBrightness = 0;
if (isValueHistogram) {
for (let i = 1; i < 256; i++) {
if (maxBrightness < histBrightness[i]) {
maxBrightness = histBrightness[i];
}
}
} else {
for (let i = 0; i < 256; i++) {
if (maxBrightness < histR[i]) {
maxBrightness = histR[i];
} else if (maxBrightness < histG[i]) {
maxBrightness = histG[i];
} else if (maxBrightness < histB[i]) {
maxBrightness = histB[i];
}
}
}
return {
r: histR,
g: histG,
b: histB,
maxBrightness,
};
index = index + 3;
}
console.log(rgb_values);
resolve(rgb_values);
})
.catch((e) => {
reject(e);
});
});
};
export const calculateLevenshteinDistance = async (

View File

@@ -344,18 +344,14 @@ export const uncompressZipArchive = async (filePath: string) => {
const { fileNameWithoutExtension, extension } =
getFileConstituents(filePath);
const targetDirectory = `${USERDATA_DIRECTORY}/expanded/${fileNameWithoutExtension}`;
const resizedImagesDirectory = `${USERDATA_DIRECTORY}/expanded/resized/${fileNameWithoutExtension}`;
await createDirectory(directoryOptions, targetDirectory);
await p7zip.extract(filePath, targetDirectory, [], "", false);
return await resizeImageDirectory(targetDirectory, resizedImagesDirectory);
return await resizeImageDirectory(targetDirectory);
};
export const uncompressRarArchive = async (filePath: string) => { };
export const resizeImageDirectory = async (
directoryPath: string,
resizedImagesDirectory: string
) => {
export const resizeImageDirectory = async (directoryPath: string) => {
const files = await walkFolder(directoryPath, [
".jpg",
".jpeg",
@@ -364,7 +360,6 @@ export const resizeImageDirectory = async (
".png",
".bmp",
]);
await createDirectory({}, resizedImagesDirectory);
const resizePromises = [];
map(files, (file) => {
resizePromises.push(
@@ -381,19 +376,23 @@ export const resizeImageDirectory = async (
resizedStream
.pipe(sharpResizeInstance)
.toFile(
`${resizedImagesDirectory}/${file.name}_275px${file.extension}`
`${directoryPath}/${file.name}_275px${file.extension}`
)
.then((data) => {
console.log(
`Resized image ${JSON.stringify(data, null, 4)}`
);
fse.unlink(
`${directoryPath}/${file.name}${file.extension}`
);
resolve(
`${resizedImagesDirectory}/${file.name}_275px${file.extension}`
`${directoryPath}/${file.name}_275px${file.extension}`
);
});
}
})
);
});
return await Promise.all(resizePromises);
};