-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.mjs
183 lines (146 loc) · 4.01 KB
/
compress.mjs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { readdirSync, statSync, readFileSync, writeFileSync } from "fs";
import { join } from "path/posix";
import { ImagePool } from "@squoosh/lib";
import { cpus } from "os";
const imagePool = new ImagePool(cpus().length);
const normalScalars = [1.5, 2, 3, 5, 6, 8, 10];
const extra = {
'png': {
oxipng: { "level": 3, "interlace": false },
},
jpg: {
mozjpeg: {
"quality": 75,
"baseline": false,
"arithmetic": false,
"progressive": true,
"optimize_coding": true,
"smoothing": 0,
"color_space": 3,
"quant_table": 3,
"trellis_multipass": false,
"trellis_opt_zero": false,
"trellis_opt_table": false,
"trellis_loops": 1,
"auto_subsample": true,
"chroma_subsample": 2,
"separate_chroma_quality": false,
"chroma_quality": 75,
},
}
}
export async function doImage(img, ext) {
const ing = imagePool.ingestImage(img);
// const ing2 = imagePool.ingestImage(img)
const { width, height } = (await ing.decoded).bitmap;
const scale = width > 2000 || height > 2000;
if (scale) {
const scaleBy =
normalScalars.filter((v) => (width / v < 2000 && height / v < 2000))[0] ??
Math.max(width, height) / 2000;
await ing.preprocess({
resize: {
method: "lanczos3",
width: Math.floor(width / scaleBy),
height: Math.floor(height / scaleBy),
},
});
}
const result = await ing.encode({
webp: {
"quality": 50,
"target_size": 0,
"target_PSNR": 0,
"method": 6,
"sns_strength": 50,
"filter_strength": 60,
"filter_sharpness": 0,
"filter_type": 1,
"partitions": 0,
"segments": 4,
"pass": 1,
"show_compressed": 0,
"preprocessing": 0,
"autofilter": 0,
"partition_limit": 0,
"alpha_compression": 1,
"alpha_filtering": 1,
"alpha_quality": 100,
"lossless": 0,
"exact": 0,
"image_hint": 0,
"emulate_jpeg_size": 0,
"thread_level": 0,
"low_memory": 0,
"near_lossless": 100,
"use_delta_palette": 0,
"use_sharp_yuv": 0,
},
...(extra[ext])
});
if(ext === 'jpg') {
return [result.webp.binary, result.mozjpeg.binary]
} else {
return [result.webp.binary, result.oxipng.binary]
}
}
// imagePool.ingestImage()
// imagePool.
const imageExtensions = [
'png',
'jpg',
'jpeg',
'webp'
]
function walkFolder(folder) {
const files = readdirSync(folder).map(v=>join(folder, v))
const images = files.flatMap(v=>{
if(statSync(v).isFile()) {
if(imageExtensions.some(ext => v.endsWith('.' + ext))) {
return [v]
}
return []
}
return walkFolder(v)
})
return images
}
export function toDo(folder) {
const files = walkFolder(folder)
const todo = files.filter(v=>{
if(v.endsWith('.webp')) return false
const parts = v.split('.')
const webpPath = parts.slice(0, parts.length - 1).join('.') + '.webp'
// console.log(v)
// console.log(webpPath)
// console.log(files.includes(webpPath))
return !files.includes(webpPath)
})
return todo
}
async function run(folder) {
const files = (await toDo(folder)).sort(()=>Math.random()-0.5).slice(0, 30)
console.log(files.join('\n'))
let c = 0
files.forEach(async file => {
const input = readFileSync(file)
try {
const [webp, other] = await doImage(input, file.endsWith('png') ? 'png' : 'jpg')
writeFileSync(file, other)
const parts = file.split('.')
const webpPath = parts.slice(0, parts.length - 1).join('.') + '.webp'
writeFileSync(webpPath, webp)
c++
console.log(file, 'done')
} catch(ex) {
c++
console.log(file, 'error')
console.log(ex)
}
if(c===files.length) {
await imagePool.close()
// process.exit()
}
});
}
run(process.cwd())