How to Extract AI Picture Prompts from Generated Images with EasyImage.work
Have you ever wondered how to extract the original prompts and parameters from an AI-generated image? Or wanted to understand the exact settings used to create that stunning artwork? At EasyImage.work, we've developed a sophisticated AI analysis tool that can extract detailed prompt information directly from AI-generated images. Today, let's dive deep into the technical implementation of how to extract AI picture prompts from image metadata, just like we've done at EasyImage.work!
The Importance of AI Prompt Extraction
AI-generated images have become increasingly popular, and understanding how they were created is valuable for several reasons:
- Learning and Improvement: By analyzing successful prompts, you can learn what works and improve your own AI art generation skills.
- Reproduction and Variation: Extracting prompts allows you to recreate similar images or create variations with slight modifications.
- Documentation and Sharing: Having the original prompts helps document your creative process and share techniques with others.
- Quality Assessment: Understanding the parameters used can help evaluate the quality and complexity of AI-generated artwork.
However, it's crucial to note that only original AI-generated images can extract prompts successfully. Once an image has been edited, resized, or converted to different formats, the metadata containing the prompt information may be lost or corrupted, making extraction impossible.
Technical Insights Behind EasyImage.work's AI Analysis
You might be curious about how EasyImage.work extracts detailed prompt information directly from AI-generated images in the browser. Today, we'll reveal the secret by examining the core JavaScript code that powers our AI analysis functionality.
// Extract AI information from file
const extractAIInfoFromFile = async (file: File): Promise<AIInfo | null> => {
try {
const arrayBuffer = await file.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
let extractedText = "";
// Use different extraction methods based on file type
if (file.type === "image/png" || file.name.toLowerCase().endsWith(".png")) {
extractedText = extractPNGText(uint8Array);
} else if (
file.type.startsWith("image/jpeg") ||
file.name.toLowerCase().match(/\.(jpg|jpeg)$/)
) {
extractedText = extractJPEGText(uint8Array);
}
if (!extractedText.trim()) {
console.log(`[DEBUG] File ${file.name} has no extracted text`);
return null;
}
// Check if the text contains AI keywords
const aiKeywords = [
"Steps:",
"Sampler:",
"CFG scale:",
"Seed:",
"Size:",
"Model:",
"Negative prompt:",
"parameters",
"DPM++",
"Euler",
];
const hasAI = aiKeywords.some((keyword) => extractedText.includes(keyword));
if (!hasAI) {
console.log(`[DEBUG] File ${file.name} does not contain AI keywords`);
return null;
}
// Clean and format the text
const cleanText = extractedText
.replace(/\0/g, "")
.replace(/\r/g, "")
.trim();
// Format AI information
const formattedText = formatAIText(cleanText);
return {
rawText: formattedText,
};
} catch (error) {
console.error(`Error analyzing file ${file.name}:`, error);
return null;
}
};
PNG Text Extraction
For PNG images, AI tools often store prompt information in text chunks within the image file. Here's how we extract this data:
// Extract PNG text
const extractPNGText = (uint8Array: Uint8Array): string => {
try {
let text = "";
let offset = 8; // Skip PNG signature
while (offset < uint8Array.length - 8) {
const length =
(uint8Array[offset] << 24) |
(uint8Array[offset + 1] << 16) |
(uint8Array[offset + 2] << 8) |
uint8Array[offset + 3];
const type = String.fromCharCode(
uint8Array[offset + 4],
uint8Array[offset + 5],
uint8Array[offset + 6],
uint8Array[offset + 7]
);
if (type === "tEXt" || type === "iTXt") {
const data = uint8Array.slice(offset + 8, offset + 8 + length);
const chunkText = new TextDecoder("utf-8", { fatal: false }).decode(
data
);
text += chunkText + "\n";
console.log(`[DEBUG] Found PNG ${type} chunk:`, chunkText.substring(0, 200));
}
offset += 12 + length;
if (type === "IEND") break;
}
return text;
} catch (error) {
console.error("PNG text extraction failed:", error);
return "";
}
};
JPEG Text Extraction
For JPEG images, AI metadata is typically stored in EXIF data within APP1 segments:
// Extract JPEG text
const extractJPEGText = (uint8Array: Uint8Array): string => {
try {
let text = "";
let offset = 2; // Skip SOI marker
while (offset < uint8Array.length - 4) {
const marker = (uint8Array[offset] << 8) | uint8Array[offset + 1];
if (marker === 0xffe1) {
// APP1 segment
const length = (uint8Array[offset + 2] << 8) | uint8Array[offset + 3];
const data = uint8Array.slice(offset + 4, offset + 2 + length);
const segmentText = new TextDecoder("utf-8", { fatal: false }).decode(
data
);
text += segmentText + "\n";
console.log(
`[DEBUG] Found JPEG APP1 segment:`,
segmentText.substring(0, 200)
);
}
const segmentLength =
(uint8Array[offset + 2] << 8) | uint8Array[offset + 3];
offset += 2 + segmentLength;
// Stop when reaching image data
if (
marker >= 0xffc0 &&
marker <= 0xffcf &&
marker !== 0xffc4 &&
marker !== 0xffc8
) {
break;
}
}
return text;
} catch (error) {
console.error("JPEG text extraction failed:", error);
return "";
}
};
AI Text Formatting
Once we extract the raw text, we need to format it properly to make it readable and organized:
// Format AI text
const formatAIText = (rawText: string): string => {
let formattedText = "";
// Look for parameters section
const paramMatch = rawText.match(/parameters[:\s]*(.*)/is);
if (paramMatch && paramMatch[1]) {
const fullContent = paramMatch[1].trim();
// Split content: Prompt, Negative prompt, Parameters
const sections = fullContent.split(/(?=Negative prompt:|Steps:)/);
// Process Prompt section
if (sections[0]) {
const promptText = sections[0].replace(/\n+/g, " ").trim();
if (
promptText &&
!promptText.toLowerCase().startsWith("negative prompt")
) {
formattedText += `Prompt:\n${promptText}\n\n`;
}
}
// Process Negative prompt section
const negativeSection = sections.find((s) =>
s.trim().startsWith("Negative prompt:")
);
if (negativeSection) {
const negativeMatch = negativeSection.match(
/Negative prompt:\s*(.*?)(?=\n*Steps:|$)/s
);
if (negativeMatch && negativeMatch[1]) {
const negativeText = negativeMatch[1].replace(/\n+/g, " ").trim();
formattedText += `Negative prompt:\n${negativeText}\n\n`;
}
}
// Process Parameters section
const paramSection = sections.find((s) => s.includes("Steps:"));
if (paramSection) {
formattedText += "Parameters:\n";
// Extract parameter lines
const paramText = paramSection.replace(/^.*?(Steps:)/s, "Steps:");
// Convert comma-separated parameters to line-separated
const params = paramText.split(/,\s*(?=[A-Za-z])/);
params.forEach((param) => {
const trimmedParam = param.trim();
if (trimmedParam) {
formattedText += `${trimmedParam}\n`;
}
});
}
}
return formattedText.trim();
};
Code Explanation
- File Type Detection: The code first determines whether the uploaded file is a PNG or JPEG image, as different formats store metadata differently.
- Binary Data Processing: The file is converted to a
Uint8Array
to process the raw binary data and extract embedded text information. - Format-Specific Extraction:
- For PNG files, the code searches for
tEXt
andiTXt
chunks that contain text metadata - For JPEG files, it looks for APP1 segments that contain EXIF data with AI parameters
- For PNG files, the code searches for
- AI Keyword Detection: The extracted text is analyzed for specific AI-related keywords like "Steps:", "Sampler:", "CFG scale:", etc., to confirm it contains AI generation parameters.
- Text Formatting: The raw extracted text is cleaned and formatted into a readable structure with clear sections for Prompt, Negative prompt, and Parameters.
- Result Validation: Only files that contain valid AI metadata are processed and displayed to the user.
Critical Requirements for Successful Extraction
Important: This extraction process only works with original AI-generated images. Here's why:
- Metadata Preservation: AI tools like Stable Diffusion, Midjourney, and DALL-E embed prompt information directly into the image file's metadata when the image is first generated.
- Data Loss During Processing: When images are edited, resized, converted to different formats, or processed through image editing software, the metadata containing the prompt information is often stripped or corrupted.
- Format Limitations: Only PNG and JPEG formats are supported, as these are the formats that AI tools typically use to store metadata.
- Original Source Requirement: Images that have been downloaded from social media, screenshotted, or processed through web applications may lose their original metadata.
Advantages of EasyImage.work's AI Analysis
This code represents just one component of EasyImage.work's comprehensive AI analysis functionality. In our practical implementation, we've made numerous optimizations and enhancements:
- Multi-Format Support: EasyImage.work supports both PNG and JPEG formats, covering the most common AI-generated image types.
- Intelligent Detection: Our system automatically detects whether an image contains AI metadata and provides clear feedback when no information is found.
- User-Friendly Interface: With EasyImage.work, you don't need to understand the technical details. Simply upload your AI-generated image and instantly see the extracted prompts and parameters.
- Export Functionality: You can easily copy the extracted prompts or export all results as JSON files for further analysis.
- Batch Processing: Process multiple images at once to extract prompts from entire collections of AI-generated artwork.
Conclusion
Through this technical deep dive, you now understand how to extract AI picture prompts from generated images. This process relies on carefully parsing image metadata that AI tools embed during the generation process.
Remember: The key to successful prompt extraction is using original, unmodified AI-generated images. Once an image has been processed, edited, or converted, the valuable prompt information may be permanently lost.
If you need a simple, efficient, and powerful tool to extract AI prompts from your generated images, remember EasyImage.work! We'll continue to work hard to bring you more convenient and practical AI analysis features, helping you unlock the secrets behind your favorite AI-generated artwork!