-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreframe-images.cjs
More file actions
118 lines (106 loc) · 3.52 KB
/
reframe-images.cjs
File metadata and controls
118 lines (106 loc) · 3.52 KB
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
// Node.js script to reframe product images for online store look
// Usage: node reframe-images.js
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
const collections = [
{
src: path.join(__dirname, 'src/assets/mens-collection'),
dest: path.join(__dirname, 'src/assets/mens-collection-framed'),
},
{
src: path.join(__dirname, 'src/assets/womens-collection'),
dest: path.join(__dirname, 'src/assets/womens-collection-framed'),
},
];
const TARGET_WIDTH = 1000;
const TARGET_HEIGHT = 1250;
function getDominantEdgeColor(imageBuffer, width, height) {
// Sample pixels from the edges and return the most common color
// For simplicity, sample corners and average them
return sharp(imageBuffer)
.raw()
.toBuffer({ resolveWithObject: true })
.then(({ data, info }) => {
const channels = info.channels;
const getPixel = (x, y) => {
const idx = (y * info.width + x) * channels;
return [data[idx], data[idx + 1], data[idx + 2]];
};
const samples = [
getPixel(0, 0),
getPixel(info.width - 1, 0),
getPixel(0, info.height - 1),
getPixel(info.width - 1, info.height - 1),
];
const avg = samples.reduce(
(acc, val) => acc.map((c, i) => c + val[i]),
[0, 0, 0]
).map((c) => Math.round(c / samples.length));
return { r: avg[0], g: avg[1], b: avg[2] };
});
}
async function processImage(srcPath, destPath) {
const image = sharp(srcPath);
const metadata = await image.metadata();
const buffer = await image.toBuffer();
const bgColor = await getDominantEdgeColor(buffer, metadata.width, metadata.height);
// Special zoom-out for specific images
const fileName = path.basename(srcPath);
const isBeigeBoy = ["beigeboyfront.jpg", "beigeboyback.jpg"].includes(fileName);
let resizeOptions;
if (isBeigeBoy) {
// Zoom out: fit to 80% of canvas height
resizeOptions = {
width: Math.round(TARGET_WIDTH * 0.8),
height: Math.round(TARGET_HEIGHT * 0.8),
fit: 'contain',
background: { r: bgColor.r, g: bgColor.g, b: bgColor.b, alpha: 1 },
};
} else {
resizeOptions = {
width: TARGET_WIDTH,
height: TARGET_HEIGHT,
fit: 'contain',
background: { r: bgColor.r, g: bgColor.g, b: bgColor.b, alpha: 1 },
};
}
// Create a blank canvas
const canvas = sharp({
create: {
width: TARGET_WIDTH,
height: TARGET_HEIGHT,
channels: 3,
background: { r: bgColor.r, g: bgColor.g, b: bgColor.b },
},
});
// Resize the image
const resizedBuffer = await image.resize(resizeOptions).toBuffer();
const resizedMeta = await sharp(resizedBuffer).metadata();
// Composite the resized image onto the center of the canvas
await canvas
.composite([
{
input: resizedBuffer,
left: Math.round((TARGET_WIDTH - resizedMeta.width) / 2),
top: Math.round((TARGET_HEIGHT - resizedMeta.height) / 2),
},
])
.toFile(destPath);
console.log(`Processed: ${destPath}`);
}
async function processCollection({ src, dest }) {
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
const files = fs.readdirSync(src).filter(f => f.match(/\.(jpe?g|png)$/i));
for (const file of files) {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file.replace(/\.(jpe?g)$/i, '.png'));
await processImage(srcPath, destPath);
}
}
(async () => {
for (const collection of collections) {
await processCollection(collection);
}
console.log('All images processed.');
})();