Image generation provides us a way to bring life to natural language through a visual medium. Through a simple or detailed description, we can generate a completely unique image that tells the story we want whether that's for dynamically creating blog images or exploring new forms of art.
We can use AI to generate images with DALL-E and the OpenAI SDK in Node.js. In this lesson you'll learn how to use the createImage method generate an image using a descriptive prompt, the number of images you want to generate, and the size of the image.
https://platform.openai.com/docs/api-reference/images/create
Instructor: [0:00] One of the interesting features of OpenAI is its image generation capabilities. We can take advantage of these capabilities programmatically by hooking into the DALL-E model, using the OpenAI SDK.
[0:11] Once we have OpenAI installed and configured inside of our Node environment, we can use the createImage method, along with the given options, to create an image based off of a prompt using human language.
[0:22] I'm going to create a new constant called Response, and I'm going to set that = toawait openai.createImage, where inside, I'm going to pass in a new object that includes a prompt n for the number of images that I want to generate, and the size.
[0:39] For n, I just want to generate one image to start, so I'm going to specify one. For the size, we're limited to a few different options, including 256x256, 512x512 and 1024x1024. Let's just go with 512x512 for now. I'm going to paste that in as my size option.
[0:56] Now, for the interesting part we can provide a prompt so we can tell DALL-E what we want to generate. I'm going to say I want to prompt of a jellyfish in the space. Now in order to see and use that response, I'm going to pass back response.data.data as part of my Json response.
[1:14] Now, depending on the environment you're in, if you're in an Next.js serverless function like I am, if you invoke that new function, we can now see that we were giving that response.
[1:22] If we open that up in a new tab, we can see, I now have my jellyfish floating around in space. We can even change that to whatever we want such as a carton of eggs where after we refresh that, we get a new URL and we can now see our carton of eggs where it doesn't necessarily appear, like it's in space, but it was pretty cool that we were able to actually get a carton of eggs.
[1:43] Now, the interesting thing with prompts is the more descriptive you can be, the more accurate it could potentially be for what you're trying to generate. If, for instance, I say with stars and a sun in the background, we can see that with my new image response, I actually get just that and now it does look like a carton of eggs in space.
[2:02] In review, in order to easily generate a new image with DALL-E, we can use the create image method which allows us to pass in a prompt the number of images we want and the size or we can change the prompt to whatever we want, such as an egg with a pair of eyeglasses.
[2:18] Where once generated, we can now see an image based on the description we gave.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!