🧪 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", "filename-parser": "^1.0.4",
"fs-extra": "^10.0.0", "fs-extra": "^10.0.0",
"imghash": "^0.0.9", "imghash": "^0.0.9",
"jimp": "^0.16.1",
"jsdom": "^15.2.1", "jsdom": "^15.2.1",
"klaw": "^4.0.1", "klaw": "^4.0.1",
"leven": "^3.1.0", "leven": "^3.1.0",

View File

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

View File

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

View File

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