Done
This commit is contained in:
@@ -1 +1,3 @@
|
||||
To run the script: npx tsx process-images.ts
|
||||
To run the script: npx tsx process-images.ts
|
||||
To add API key:
|
||||
r8_Fg4V9pSBB5RAiTiPITCRyRjsLYdafrL0vabnp
|
||||
+23
-13
@@ -9,35 +9,45 @@ import Replicate from "replicate";
|
||||
import sharp from "sharp";
|
||||
|
||||
const removeWatermark = async () => {
|
||||
|
||||
const replicate = new Replicate({auth: process.env.REPLICATE_API_TOKEN});
|
||||
const replicate = new Replicate({ auth: process.env.REPLICATE_API_TOKEN });
|
||||
|
||||
const prompt = "Carefully remove the watermark in image 1 from image 2, leaving everything else unchanged. Preserve icon details while removing watermark text that says 'Airhex.com'. Save with a white background."
|
||||
|
||||
fs.mkdirSync(outputDirectory, {recursive: true});
|
||||
fs.mkdirSync(outputDirectory, { recursive: true });
|
||||
|
||||
const pngFiles = fs
|
||||
.readdirSync(inputDirectory)
|
||||
.filter((file) => file.toLowerCase().endsWith(".png"))
|
||||
.filter((file) => file.toLowerCase().endsWith(".png"));
|
||||
|
||||
for (const file of pngFiles) {
|
||||
const filePath = path.join(inputDirectory, file);
|
||||
const outputPath = path.join(outputDirectory, file);
|
||||
|
||||
// Ensure the output directory exists before every write
|
||||
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
|
||||
|
||||
if (fs.existsSync(outputPath)) {
|
||||
console.log(`Converting: ${file} (webp → png)`);
|
||||
const webpData = fs.readFileSync(outputPath);
|
||||
const pngData = await sharp(webpData).png().toBuffer();
|
||||
fs.writeFileSync(outputPath, pngData);
|
||||
const fileBuffer = fs.readFileSync(outputPath);
|
||||
const metadata = await sharp(fileBuffer).metadata();
|
||||
|
||||
if (metadata.format !== 'png') {
|
||||
console.log(`About to write to: ${outputPath}`);
|
||||
console.log(`Dir exists: ${fs.existsSync(path.dirname(outputPath))}`);
|
||||
console.log(`Dir writable:`, (() => { try { fs.accessSync(path.dirname(outputPath), fs.constants.W_OK); return true; } catch { return false; } })());
|
||||
console.log(`File writable:`, (() => { try { fs.accessSync(outputPath, fs.constants.W_OK); return true; } catch { return false; } })());
|
||||
const pngData = await sharp(fileBuffer).png().toBuffer();
|
||||
console.log(`PNG buffer size: ${pngData.length}`);
|
||||
fs.writeFileSync(outputPath, pngData);
|
||||
console.log(`Write succeeded`);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`Processing: ${filePath}`);
|
||||
const watermarkData = fs.readFileSync(watermarkImage);
|
||||
|
||||
const imageData = fs.readFileSync(filePath);
|
||||
const watermarkBase64 = `data:image/png;base64,${watermarkData.toString("base64")}`;
|
||||
|
||||
const imageBase64 = `data:image/png;base64,${imageData.toString("base64")}`;
|
||||
|
||||
const output = await replicate.run("qwen/qwen-image-edit-plus", {
|
||||
@@ -46,12 +56,13 @@ const removeWatermark = async () => {
|
||||
prompt,
|
||||
},
|
||||
}) as { url(): string }[];
|
||||
const resultBuffer = Buffer.from(await(await fetch(output[0].url())).arrayBuffer());
|
||||
|
||||
const resultBuffer = Buffer.from(await (await fetch(output[0].url())).arrayBuffer());
|
||||
fs.writeFileSync(outputPath, resultBuffer);
|
||||
|
||||
console.log(`Saved: ${outputPath}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const removeBackground = async () => {
|
||||
fs.mkdirSync(backgroundRemovedDirectory, { recursive: true });
|
||||
@@ -71,7 +82,6 @@ const removeBackground = async () => {
|
||||
const outputPath = path.join(backgroundRemovedDirectory, file);
|
||||
|
||||
if (fs.existsSync(outputPath)) {
|
||||
console.log(`Skipping: ${file} (already processed)`);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user