const sharp = require('sharp'); const path = require('path'); const fs = require('fs'); const CROP_BOTTOM_HEIGHT = 300; async function removeWatermark() { console.log('🖼️ 开始裁剪图片水印...\n'); const imageDir = path.join(__dirname, '产品', '飞牛 NAS', 'fyd-亚克力机箱 - 单', '详情', 'image'); const backupDir = path.join(imageDir, 'backup'); const imageFiles = fs.readdirSync(backupDir).filter(file => file.endsWith('.jpg')); console.log(`📸 找到 ${imageFiles.length} 张图片需要处理\n`); console.log(`✂️ 裁剪设置: 从底部裁剪 ${CROP_BOTTOM_HEIGHT}px\n`); for (const imageFile of imageFiles) { try { const backupPath = path.join(backupDir, imageFile); const imagePath = path.join(imageDir, imageFile); console.log(`📄 正在处理: ${imageFile}`); const metadata = await sharp(backupPath).metadata(); const newHeight = metadata.height - CROP_BOTTOM_HEIGHT; await sharp(backupPath) .extract({ left: 0, top: 0, width: metadata.width, height: newHeight }) .toFile(imagePath); console.log(` ✅ 已裁剪: ${metadata.width}x${metadata.height} -> ${metadata.width}x${newHeight}\n`); } catch (error) { console.error(` ❌ 处理失败 ${imageFile}: ${error.message}\n`); } } console.log('✨ 所有图片处理完成!'); } removeWatermark().catch(error => { console.error('💥 处理过程出错:', error); process.exit(1); });