How can i download the generated images,which are generated through dall-e-2 api

here is my code
const API_KEY = “my_api_key”;
const submit = document.querySelector(“#submit_btn”);
const inputPrompt = document.querySelector(“#real__prompt”);
const imgElement = document.querySelector(“.image_section”)
const getImages = async () => {
const options = {
method: “POST”,
headers: {
“Content-Type”: “application/json”,
“Authorization”: Bearer ${API_KEY}
},
body: JSON.stringify({
“model”: “dall-e-2”,
“prompt”: “full view and white walls in the background in a cozy room where a” + inputPrompt.value,
“n”: 1,
“size”: “256x256”
})
}
try {
const response = await fetch(‘https://api.openai.com/v1/images/generations’, options)
const data = await response.json();
console.log(data);
$(“.loader”).removeClass(“show”)
data?.data.forEach(imageObject => {
const ImageContainer = document.createElement(“div”);
const anchorContainer = document.createElement(“button”);
ImageContainer.classList.add(“Image__container”);
anchorContainer.setAttribute(“id”, “Download_btn”);
const imageElement = document.createElement(“img”);
imageElement.setAttribute(“src”, imageObject.url);
anchorContainer.append(“Download”);
ImageContainer.append(imageElement);
ImageContainer.append(anchorContainer);
imgElement.append(ImageContainer);
const btn = document.getElementById(‘Download_btn’);
const url = imageObject.url;

				 btn.addEventListener('click', (event) => {
					 event.preventDefault();
					 console.log('ABC')
					 downloadImage(imageObject.url);
				 })


				 function downloadImage(url) {
					 const link = document.createElement("a");
					 link.href = url;
					 link.download = document.getElementById("Download_btn").value.split(" ").join("_") + ".png";
					 link.target = "_blank";
					 link.click();
				 }
			 })
		 } catch (error) {
			 console.error(error)

If you’re not setting the response format, it defaults to URL…

8. How are images returned by the endpoint?

The API can output images as URLs (response_format =url) or b64_json. Our developer guide includes more details.

See the images endpoint docs for more.

what should be the response format for downloading the file base64? but this isnt working

Using the Pillow python library for example, this is what I have:

for image_data in images_response.data:
    image_obj = image_data.model_dump()["b64_json"]
    image_obj = Image.open(BytesIO(base64.b64decode(image_obj)))
    # Add something here to change the filename in each loop
    image_obj.save(image_path)