Skip to content

Commit f265375

Browse files
committed
use sharp and gifsicle instead of jimp
1 parent c184cfa commit f265375

16 files changed

Lines changed: 1231 additions & 96 deletions

File tree

cli.js

Lines changed: 34 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env node
22
"use strict";
33

4-
const { GifFrame, GifUtil, BitmapImage } = require("gifwrap");
5-
const Jimp = require("jimp");
4+
const sharp = require("sharp");
5+
const execa = require("execa");
6+
const gifsicle = require("gifsicle");
67
const updateNotifier = require("update-notifier");
78
const meow = require("meow");
89

@@ -51,80 +52,44 @@ const getXY = index => {
5152
return { x, y };
5253
};
5354

54-
const cropFrame = image => {
55-
const cropped = [];
56-
for (let i = 0; i < 6; i++) {
57-
const clone = image.clone();
58-
const { x, y } = getXY(i);
59-
clone.crop(x, y, CUT_WIDTH, CUT_HEIGHT);
60-
cropped.push(clone);
61-
}
62-
return cropped;
63-
};
64-
65-
const cropGithubGifs = async path => {
55+
const cropImage = async path => {
6656
try {
67-
const source = await GifUtil.read(path);
68-
const { frames } = source;
69-
let croppedGifs = [];
70-
let frameIndex = 1;
71-
for (const frame of frames) {
72-
console.log(`Processing frame ${frameIndex} of ${frames.length}`);
73-
const buf = frame.bitmap.data;
74-
frame.scanAllCoords((x, y, bi) => {
75-
buf[bi + 3] = 0xff;
76-
});
77-
78-
let jimpToCrop = new Jimp(frame.bitmap.width, frame.bitmap.height, 0);
79-
jimpToCrop.bitmap.data = frame.bitmap.data;
80-
jimpToCrop.resize(CONTAINER_WIDTH, Jimp.AUTO);
81-
82-
const frameToCrop = await Jimp.read(jimpToCrop);
83-
84-
const cropped = cropFrame(frameToCrop).map(img => {
85-
return new GifFrame(img.bitmap);
86-
});
87-
88-
cropped.forEach((croppedFrame, i) => {
89-
croppedFrame.scanAllCoords((x, y, bi) => {
90-
buf[bi + 3] = 0xff;
91-
});
92-
croppedGifs[i] = croppedGifs[i]
93-
? [...croppedGifs[i], croppedFrame]
94-
: [croppedFrame];
95-
});
96-
97-
frameIndex++;
57+
const [ext, ...rest] = path.split(".").reverse();
58+
const image = sharp(path).resize(CONTAINER_WIDTH);
59+
for (let i = 0; i < 6; i++) {
60+
const filename = `${i}.${ext}`;
61+
const { x, y } = getXY(i);
62+
await image
63+
.clone()
64+
.extract({ left: x, top: y, width: CUT_WIDTH, height: CUT_HEIGHT })
65+
.toFile(filename);
66+
console.log(`Successfully cropped ${filename}.`);
9867
}
99-
100-
croppedGifs.forEach(async (croppedFrames, i) => {
101-
console.log(
102-
`Quantizing Dekker value for gif ${i} (this might take a while)`
103-
);
104-
GifUtil.quantizeDekker(croppedFrames);
105-
await GifUtil.write(`${i}.gif`, croppedFrames);
106-
console.log(`saved ${i}.gif`);
107-
});
108-
} catch (error) {
109-
console.error(error);
68+
} catch (e) {
69+
console.error(e);
11070
}
11171
};
11272

113-
const cropGithubImages = async path => {
114-
const image = await Jimp.read(path);
115-
116-
image.resize(CONTAINER_WIDTH, Jimp.AUTO);
117-
const cropped = cropFrame(image);
118-
119-
for (let i = 0; i < cropped.length; i++) {
120-
const clone = cropped[i];
121-
await clone.writeAsync(`${i}.jpg`);
122-
console.log(i, "has been written.");
73+
const cropGif = async path => {
74+
const resized = "resized.gif";
75+
await execa(gifsicle, ["--resize", "727x_", "-o", resized, path]);
76+
for (let i = 0; i < 6; i++) {
77+
const filename = `${i}.gif`;
78+
const { x, y } = getXY(i);
79+
await execa(gifsicle, [
80+
"--crop",
81+
`${x},${y}+${CUT_WIDTH}x${CUT_HEIGHT}`,
82+
"--output",
83+
filename,
84+
resized
85+
]);
86+
console.log(`Successfully cropped ${filename}.`);
12387
}
12488
};
12589

126-
if (cli.input[0].endsWith(".gif")) {
127-
cropGithubGifs(cli.input[0]);
90+
const path = cli.input[0];
91+
if (path.endsWith(".gif")) {
92+
cropGif(path);
12893
} else {
129-
cropGithubImages(cli.input[0]);
94+
cropImage(path);
13095
}

examples/0.gif

236 KB
Loading

examples/0.jpg

2.14 KB
Loading

examples/1.gif

190 KB
Loading

examples/1.jpg

1.37 KB
Loading

examples/2.gif

290 KB
Loading

examples/2.jpg

2.28 KB
Loading

examples/3.gif

276 KB
Loading

examples/3.jpg

1.77 KB
Loading

examples/4.gif

241 KB
Loading

0 commit comments

Comments
 (0)