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
|
||||||
+19
-9
@@ -9,7 +9,6 @@ import Replicate from "replicate";
|
|||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
|
|
||||||
const removeWatermark = async () => {
|
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."
|
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."
|
||||||
@@ -18,26 +17,37 @@ const removeWatermark = async () => {
|
|||||||
|
|
||||||
const pngFiles = fs
|
const pngFiles = fs
|
||||||
.readdirSync(inputDirectory)
|
.readdirSync(inputDirectory)
|
||||||
.filter((file) => file.toLowerCase().endsWith(".png"))
|
.filter((file) => file.toLowerCase().endsWith(".png"));
|
||||||
|
|
||||||
for (const file of pngFiles) {
|
for (const file of pngFiles) {
|
||||||
const filePath = path.join(inputDirectory, file);
|
const filePath = path.join(inputDirectory, file);
|
||||||
const outputPath = path.join(outputDirectory, 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)) {
|
if (fs.existsSync(outputPath)) {
|
||||||
console.log(`Converting: ${file} (webp → png)`);
|
const fileBuffer = fs.readFileSync(outputPath);
|
||||||
const webpData = fs.readFileSync(outputPath);
|
const metadata = await sharp(fileBuffer).metadata();
|
||||||
const pngData = await sharp(webpData).png().toBuffer();
|
|
||||||
|
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);
|
fs.writeFileSync(outputPath, pngData);
|
||||||
|
console.log(`Write succeeded`);
|
||||||
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Processing: ${filePath}`);
|
console.log(`Processing: ${filePath}`);
|
||||||
const watermarkData = fs.readFileSync(watermarkImage);
|
const watermarkData = fs.readFileSync(watermarkImage);
|
||||||
|
|
||||||
const imageData = fs.readFileSync(filePath);
|
const imageData = fs.readFileSync(filePath);
|
||||||
const watermarkBase64 = `data:image/png;base64,${watermarkData.toString("base64")}`;
|
const watermarkBase64 = `data:image/png;base64,${watermarkData.toString("base64")}`;
|
||||||
|
|
||||||
const imageBase64 = `data:image/png;base64,${imageData.toString("base64")}`;
|
const imageBase64 = `data:image/png;base64,${imageData.toString("base64")}`;
|
||||||
|
|
||||||
const output = await replicate.run("qwen/qwen-image-edit-plus", {
|
const output = await replicate.run("qwen/qwen-image-edit-plus", {
|
||||||
@@ -46,12 +56,13 @@ const removeWatermark = async () => {
|
|||||||
prompt,
|
prompt,
|
||||||
},
|
},
|
||||||
}) as { url(): string }[];
|
}) 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);
|
fs.writeFileSync(outputPath, resultBuffer);
|
||||||
|
|
||||||
console.log(`Saved: ${outputPath}`);
|
console.log(`Saved: ${outputPath}`);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const removeBackground = async () => {
|
const removeBackground = async () => {
|
||||||
fs.mkdirSync(backgroundRemovedDirectory, { recursive: true });
|
fs.mkdirSync(backgroundRemovedDirectory, { recursive: true });
|
||||||
@@ -71,7 +82,6 @@ const removeBackground = async () => {
|
|||||||
const outputPath = path.join(backgroundRemovedDirectory, file);
|
const outputPath = path.join(backgroundRemovedDirectory, file);
|
||||||
|
|
||||||
if (fs.existsSync(outputPath)) {
|
if (fs.existsSync(outputPath)) {
|
||||||
console.log(`Skipping: ${file} (already processed)`);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user