Hello everyone, Here we will discuss how we can use Image Creation OpenAI API to create/generate the images and show images directly on the UI. For that, I'm using Angular.
What is Image Creation OpenAI API?
How can we generate or manipulate images with OpenAI DALL·E models
This Images API provides three methods for interacting with images:
- Creating images from scratch based on a text prompt passed by the user
- Creating edits of an existing image based on a new text prompt
- Creating variations of an existing image
In this article, we are only creating images based on user-provided input.
Find more details here in this link Image Creation OpenAI
Generate Images using OpenAI API
Step 1. Create an OpenAI account
Go to the OpenAI (https://openai.com/) website and click on the Try ChatGPT button to create an account and sign up if you are new. Otherwise, click on the login button.
![Image Generation using Open AI Image Creation API in Angular]()
Step 2. Get the OpenAI API key
Login into your OpenAI account, click on the profile right-hand tab, then click View API Keys, highlighted with a yellow marker below the screenshot.
Once you click on View API Keys, you need to Create a new secret key if you don't have one previously. Once you click on it, an API key will generate in the Popup window, and you can copy it for using it in the application where we use this API key.
In the screenshot below, you can find your generated API keys as I've already generated it shows me like this.
![Image Generation using Open AI Image Creation API in Angular]()
Step 3. Create a new Angular application
We are using the Node.js library, which you can install by running the following command.
Step 4. Install the OpenAI npm package
Step 5. Install the ngx-spinner npm package (optional)
Install this package to show some interactive loader while fetching data from the server and rendering it to UI. For further info, you can follow this npm package link.
- NPM ngx-spinner link
- Github Link for ngx-spinner package
Note: I'm using the Tailwind CSS framework on the UI side. You can follow the below link to set it up in your angular project.
Step 6. Add configuration code of OpenAI in app.component.ts file
Step 7. Add the FormControl to get the user-entered value.
Note: Here, I'm using the Reactive Form
Step 7. The code for app.component.ts file.
Step 8. The code for the app.component.html file.
Step 8. After adding code in ts and HTML file, below is the output.
When you run your Angular 15 application, you will see UI like the one below.
![Image Generation using Open AI Image Creation API in Angular]()
Step 9. Enter Search Term.
e.g., White Cat playing with ball
![Image Generation using Open AI Image Creation API in Angular]()
Step 10. Output from OpenAI.
![Image Generation using Open AI Image Creation API in Angular]()
This is how we can integrate the Image Creation OpenAI API with our angular application.
Final Output
![Image Generation using Open AI Image Creation API in Angular]()
Thanks for reading the article.
HAPPY CODING !!!!!