64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
/**
|
|
* 批量图片优化脚本
|
|
* 功能:将指定目录下的图片放大到 2048px 并优化 JPEG 质量
|
|
*/
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const sharp = require('sharp');
|
|
|
|
async function upscaleImages(sourceDir) {
|
|
const outputDir = sourceDir + '_upscaled';
|
|
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
|
|
|
|
const files = fs.readdirSync(sourceDir).filter(f => f.endsWith('.jpg') || f.endsWith('.png'));
|
|
|
|
console.log(`🚀 开始优化 ${files.length} 张图片...`);
|
|
console.log(`📂 源目录: ${sourceDir}`);
|
|
console.log(`📂 输出目录: ${outputDir}`);
|
|
|
|
for (const file of files) {
|
|
const inputPath = path.join(sourceDir, file);
|
|
const outputPath = path.join(outputDir, file);
|
|
|
|
try {
|
|
const image = sharp(inputPath);
|
|
const metadata = await image.metadata();
|
|
|
|
console.log(`Processing ${file} (${metadata.width}x${metadata.height})...`);
|
|
|
|
// 目标尺寸:长边 2500px (满足亚马逊 Zoom 要求)
|
|
const targetSize = 2500;
|
|
|
|
await image
|
|
.resize({
|
|
width: metadata.width >= metadata.height ? targetSize : null,
|
|
height: metadata.height > metadata.width ? targetSize : null,
|
|
kernel: sharp.kernel.lanczos3 // 高质量插值算法
|
|
})
|
|
// 适度锐化,提升清晰感
|
|
.sharpen({
|
|
sigma: 1.5,
|
|
m1: 1.0,
|
|
m2: 0.5
|
|
})
|
|
.jpeg({
|
|
quality: 95, // 高质量 JPEG
|
|
chromaSubsampling: '4:4:4' // 减少色彩压缩
|
|
})
|
|
.toFile(outputPath);
|
|
|
|
console.log(`✅ 已优化: ${file}`);
|
|
} catch (e) {
|
|
console.error(`❌ 失败 ${file}: ${e.message}`);
|
|
}
|
|
}
|
|
console.log('✨ 全部完成!');
|
|
}
|
|
|
|
// 目标目录
|
|
const targetDir = path.join(__dirname, 'output_carrier_v2_2025-12-27');
|
|
upscaleImages(targetDir).catch(console.error);
|
|
|
|
|
|
|