|
1 | 1 | #!/usr/bin/env node |
2 | 2 | "use strict"; |
3 | 3 |
|
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"); |
6 | 7 | const updateNotifier = require("update-notifier"); |
7 | 8 | const meow = require("meow"); |
8 | 9 |
|
@@ -51,80 +52,44 @@ const getXY = index => { |
51 | 52 | return { x, y }; |
52 | 53 | }; |
53 | 54 |
|
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 => { |
66 | 56 | 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}.`); |
98 | 67 | } |
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); |
110 | 70 | } |
111 | 71 | }; |
112 | 72 |
|
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}.`); |
123 | 87 | } |
124 | 88 | }; |
125 | 89 |
|
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); |
128 | 93 | } else { |
129 | | - cropGithubImages(cli.input[0]); |
| 94 | + cropImage(path); |
130 | 95 | } |
0 commit comments