Replies: 1 comment
|
Jimp's import { Jimp } from 'jimp';
function getPalette(image) {
const counts = new Map();
const data = image.bitmap.data;
for (let i = 0; i < data.length; i += 4) {
if (data[i + 3] === 0) continue;
const rgba = '#' + data.subarray(i, i + 4).toString('hex');
counts.set(rgba, (counts.get(rgba) ?? 0) + 1);
}
return [...counts].map(([rgba, count]) => ({ rgba, count }))
.sort((a, b) => b.count - a.count);
}
const image = await Jimp.read('input.png');
console.log(getPalette(image));Each entry is This is an exact color inventory, so a photograph can have many entries. It does not select a fixed number of perceptually representative colors. Tested with Jimp 1.6.1 on a PNG with repeated colors, transparency and different alpha values. RGBA bitmap API, |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Am I just missing this, or is there no native way to retrieve a color palette of an image?
All reactions