Mask-based image editing
Edit existing images with DALL-E 2 by describing what you want to change.
Precise mask-based editing for specific areas and seamless modifications.
Beautiful winter cross-country ski scene
The white area in the sky shows where your prompt will be applied
Fill out the form and click "Edit Image" to see AI magic happen! The white area in the sky will be replaced with your prompt.
DALL-E 2 can seamlessly edit images by replacing masked areas with AI-generated content based on your prompt.
Background replacement, object insertion, artistic enhancement, style modification, and creative transformations.
DALL-E 2: Currently the only OpenAI model that supports image editing with masks
Original Image: PNG format, square aspect ratio
Mask: White areas indicate where to edit, black areas remain unchanged
Prompt: Clear description of what to add/replace
Key Kotlin code for AI image editing using OpenAI's DALL-E 2 API:
// Edit image with DALL-E 2
suspend fun editImage(
prompt: String,
imageFile: ByteArray,
maskFile: ByteArray,
model: String = "dall-e-2",
size: String = "1024x1024"
): ImageEditResponse {
val response = client.submitFormWithBinaryData(
url = "https://api.openai.com/v1/images/edits",
formData = formData {
append("prompt", prompt)
append("model", model)
append("size", size)
append("response_format", "url")
append("image", imageFile, Headers.build {
append(HttpHeaders.ContentType, "image/png")
append(HttpHeaders.ContentDisposition, "filename=\"image.png\"")
})
append("mask", maskFile, Headers.build {
append(HttpHeaders.ContentType, "image/png")
append(HttpHeaders.ContentDisposition, "filename=\"mask.png\"")
})
}
) {
headers { append("Authorization", "Bearer $openaiApiKey") }
timeout { requestTimeoutMillis = 300000 }
}
return json.decodeFromString<ImageEditResponse>(response.bodyAsText())
}
// Usage in controller
val editResponse = openAI.editImage(
prompt = "A hot air balloon floating in the sky",
imageFile = originalImageBytes,
maskFile = maskImageBytes,
model = Models.ImageGeneration.DALL_E_2,
size = "1024x1024"
)
val editedImageUrl = editResponse.data.firstOrNull()?.url