Be creative and have fun: Use the Dall-E /variations API to generate fun, unique images!

After obtaining the URL of the picture generated by Dall-E, you can get the different variations of the picture by making a request to OpenAI's /variations API endpoint. This API endpoint allows you to apply various changes and editing effects on images to create more interesting and unique images.

 

The following is a sample PHP code that demonstrates how to use the cURL library to make a request to OpenAI's /variations API endpoint and get the generated image variations:

<?php

$api_key = 'YOUR_API_KEY';

$image_url = '/dall-e/encoded/707e2b7bb027ff9fa1f7d68d0b3fc7fe.png';

$url = "https://api。openai。com/v1/images/variations";

$headers = array(

'Content-Type: application/json',

'Authorization: Bearer '.$api_key,

);

$data = array(

'image_url' => $image_url,

'variations' => array(

'rotate' => 45,

'flip' => true,

'color_filter' => 'sepia',

),

);

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

curl_close($ch);

$results = json_decode($response, true);

$variations = $results['data']['variations'];

foreach ($variations as $variation) {

echo "<img src='$variation'>";

}

?>

In this sample code, we first specify the Dall-E API key and the generated image URL. Then, we make a POST request to the /variations API endpoint and specify the variation effects to be applied, such as rotation, flip, and color filter.

Finally, we get the generated image URLs for various variants from the response results of the API and display them on the page.

Of course, in actual use, you can set the variant parameters differently according to your own needs and preferences to generate more interesting and unique pictures.

Note here that the image format of $image_url should be .png

If you want to know how to use v1/images/generations, please refer to my previous article! It is inconvenient for Baijiahao to directly post the link address!

Guess you like

Origin blog.csdn.net/u012240615/article/details/129995639