-
Notifications
You must be signed in to change notification settings - Fork 0
/
reprocessCorner.js
125 lines (107 loc) · 3.81 KB
/
reprocessCorner.js
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
119
120
121
122
123
124
125
import fetch from "node-fetch";
import FormData from "form-data";
import fs from "fs";
import path from "path";
import sharp from "sharp";
const openaiApiKey = "";
if (openaiApiKey.length == 0) {
console.log("You need to set your OpenAI API key in this script!");
}
console.log("Using OpenAI API Key:", openaiApiKey);
async function reprocessCorner(cornerName, prompt) {
const inputPath = "output/" + cornerName + ".png";
const outputPath = "output";
const imageSize = 1024; // Assuming the same image size as the original script
// Step 1: Send the new image data to OpenAI and get the edited image
const editedImagePath = await sendImageToOpenAI(inputPath, prompt);
if (!editedImagePath) {
console.error("Failed to process the new image for corner:", cornerName);
return;
}
await combineImages(outputPath, imageSize);
console.log(
"Reprocessed master image successfully with the new corner:",
cornerName
);
}
async function combineImages(outputPath, imageSize) {
const imageNames = ["topLeft", "topRight", "bottomLeft", "bottomRight"];
const masterImage = sharp({
create: {
width: imageSize * 2,
height: imageSize * 2,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 1 },
},
});
const composites = imageNames.map((name) => {
return {
input: path.join(outputPath, `${name}-edited.png`),
left: name.includes("Right") ? imageSize : 0,
top: name.includes("bottom") ? imageSize : 0,
};
});
masterImage
.composite(composites)
.jpeg({ quality: 100 }) // Specify JPEG output and quality
.toFile(path.join(outputPath, "masterImage.jpg")) // Change the extension to .jpg
.then(() => console.log("Master JPEG image created successfully."))
.catch((err) => console.error("Error in creating master JPEG image:", err));
}
async function sendImageToOpenAI(imagePath, prompt) {
const formData = new FormData();
formData.append("image", fs.createReadStream(imagePath));
formData.append("prompt", prompt);
try {
const response = await fetch("https://api.openai.com/v1/images/edits", {
method: "POST",
body: formData,
headers: {
Authorization: `Bearer ${openaiApiKey}`,
},
});
const data = await response.json();
if (data && data.data) {
const downloadPromises = data.data.map((image) =>
downloadImage(
image.url,
`${path.basename(imagePath, ".png")}-edited.png`
)
);
await Promise.all(downloadPromises);
return `${path.basename(imagePath, ".png")}-edited.png`;
} else {
console.error("No data returned from OpenAI");
console.log(data);
return null;
}
} catch (error) {
console.error("Failed to send image to OpenAI:", error);
return null;
}
}
async function downloadImage(url, outputPath) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch the image: ${response.statusText}`);
}
const buffer = await response.arrayBuffer(); // Changed from `buffer()` to address deprecation
// Define the full output path for correct file saving
const fullOutputPath = path.join("output", outputPath);
// Synchronously write file to ensure that no other operation is pending on this file
fs.writeFileSync(fullOutputPath, Buffer.from(buffer)); // Using Buffer.from to convert ArrayBuffer to Node.js Buffer
console.log("Successfully downloaded and saved:", outputPath);
} catch (error) {
console.error("Failed to download or save image:", error);
}
}
const args = process.argv.slice(2);
if (args.length != 2) {
console.log("Usage: node reprocessCorner.js <cornerName> <prompt>");
process.exit(1);
}
const [cornerName, prompt] = args;
reprocessCorner(cornerName, prompt).catch((err) => {
console.error("Error occurred:", err);
});