-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
37 lines (32 loc) · 1.47 KB
/
convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const yr = require('./lib/yr');
const denodeify = require('denodeify');
const fs = require('fs');
const metadata = new yr.Metadata();
const execFile = denodeify(require('child_process').execFile);
metadata.forEach({progress: true, cpus:7}, async function(image) {
if (image.data.ext == 'tiff') {
const format = await execFile("identify", [image.sourcePath()]).catch(() => '???');
if (/CMYK/.test(format) || !/sRGB/.test(format)) {
console.log("non-sRGB or damaged", image.sourcePath());
return;
}
const tmpPath = `/tmp/${image.data.sha1}.png`;
let newImage;
try {
await execFile("convert", ['-define','png:compression-level=9', image.sourcePath(), tmpPath]);
newImage = await yr.Image.createFromFile(Object.assign({}, image.data, {ext:"png",converted:{
from:image.data.sha1,
op:`${image.data.ext} to png`,
}}), tmpPath);
} catch(err) {
console.error("Error converting", image.sourcePath(), err.stack);
return;
}
console.log("Converted from", image.sourcePath(), "to", newImage.sourcePath(),
"Size", fs.statSync(image.sourcePath()).size, "to", fs.statSync(newImage.sourcePath()).size, image.data.from, image.data.tags && image.data.tags.join(' '));
fs.unlinkSync(tmpPath);
fs.unlinkSync(image.metadataPath());
}
})
.catch(err => console.error("Aborted", err.stack));