Skip to content

Commit 2d642c8

Browse files
committed
chore: replace Jimp with sharp
1 parent 00edef7 commit 2d642c8

File tree

5 files changed

+467
-1087
lines changed

5 files changed

+467
-1087
lines changed

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ import {
1818
createDetector,
1919
TAG_FAMILIES,
2020
} from '@monumental-works/apriltags-kaess-node';
21-
import { Jimp } from 'jimp';
21+
import sharp from 'sharp';
2222

23-
const image = await Jimp.read('image.png');
24-
image.greyscale();
23+
const image = sharp('image.png');
24+
const { data, info } = await image
25+
.greyscale()
26+
.raw()
27+
.toBuffer({ resolveWithObject: true });
2528

26-
const buffer = Buffer.from(image.bitmap.data.filter((_, i) => i % 4 === 0));
2729
const detector = createDetector(TAG_FAMILIES.TAG_36H11);
28-
const detections = detector.detect(
29-
buffer,
30-
image.bitmap.width,
31-
image.bitmap.height
32-
);
30+
const detections = detector.detect(data, info.width, info.height);
3331

3432
detections.forEach((tag) => {
3533
console.log(`Tag ${tag.id} at [${tag.center[0]}, ${tag.center[1]}]`);

examples/detect.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
import { createDetector, TAG_FAMILIES } from '../dist/index.js';
4-
import { Jimp } from 'jimp';
4+
import sharp from 'sharp';
55
import { createServer } from 'http';
66
import { fileURLToPath } from 'url';
77
import { dirname, join, basename } from 'path';
@@ -14,17 +14,18 @@ const PORT = 3000;
1414
async function detectAndServe(imagePath, blackBorder = 1) {
1515
console.log(`\nLoading image: ${imagePath}`);
1616

17-
const image = await Jimp.read(imagePath);
18-
const width = image.bitmap.width;
19-
const height = image.bitmap.height;
17+
const image = sharp(imagePath);
18+
const metadata = await image.metadata();
19+
const width = metadata.width;
20+
const height = metadata.height;
2021

2122
console.log(`Image dimensions: ${width}x${height}`);
2223

2324
// Convert to grayscale for detection
24-
const grayImage = image.clone().greyscale();
25-
const buffer = Buffer.from(
26-
grayImage.bitmap.data.filter((_, i) => i % 4 === 0)
27-
);
25+
const { data: buffer } = await image
26+
.greyscale()
27+
.raw()
28+
.toBuffer({ resolveWithObject: true });
2829

2930
console.log(`Creating detector with blackBorder=${blackBorder}...`);
3031
const detector = createDetector(TAG_FAMILIES.TAG_36H11, { blackBorder });
@@ -42,7 +43,9 @@ async function detectAndServe(imagePath, blackBorder = 1) {
4243
});
4344

4445
// Convert image to base64 for embedding
45-
const imageBase64 = await image.getBase64('image/jpeg');
46+
const imageBase64 =
47+
'data:image/jpeg;base64,' +
48+
(await sharp(imagePath).jpeg().toBuffer()).toString('base64');
4649

4750
const htmlPage = generateHTML(
4851
imageBase64,

0 commit comments

Comments
 (0)