Generate AI Images with OpenAI DALL-E in Python
Key Takeaways
The video demonstrates how to use OpenAI DALL-E in Python to generate AI images, covering image generation, editing, and variations, with a focus on practical implementation using the OpenAI API and Python libraries.
Full Transcript
what is going on guys welcome back in this video today we're going to learn how to use open ai's dolly in Python to create AI generated images but also in order to edit images using this AI so let us get right into it [Music] alright so for this video today we're going to need to use an open AI account so the first thing you want to do is you want to go to platform.openai.com if you want to create a new account for yourself if you don't have one yet chances are you already have one because you're using chat GPT or you have used chat gbt this is the same account so if you have a chat GPT account you also have an open AI account so you can just log into it and what you want to do here is you want to click on your account in the upper right corner let me just move my camera here so that you can see what I'm doing you want to click on your account here in the upper right corner and you want to go to view API keys and in my case I already have a secret key here that I'm going to use for this video today but if you don't have a key here or you forgot your key that you have here you can just click here on create new secret key and it's going to generate a new key for you you can just copy it save it in a file or put it clear text into your code however if someone gets that key you should always keep it private because if someone gets that key they can authenticate themselves as you so they can use your credits they can use your account to send requests they can do all sorts of things in your name so you want to keep the secret this is what authenticates you uh for the open AI API so copy it stored in a file stored in the environment load it do something with it but don't just keep it somewhere don't just put it maybe into your code and definitely don't share it with anyone so I'm going to revoke this key here so that you cannot use it from this video here this is the one that I'm going to use I have it in a text file and with this API key now let me just move back my camera I want to have it in the upper right corner with this API key we can now authenticate ourselves and use the various apis to the various services and we're interested today in the image generation API which is using Dolly the image generation model and we have three basic use cases here image Generation image editing and variations of images so the first case first use case is quite simple we have a simple prompt in this case here some white cat we provide a number of images that we are interested in and we provide a resolution and I think we have only three formats supported 256 512 and 1024 this is what we have and they also have a different pricing now regarding the pricing chances are you don't have to spend any money on this because when you create a new openai account when I go to manage account here uh chances are that you will get some free credits to use so I have 18 here I never loaded any money onto this account um so chances started when you create an account or if you already have an account you already have some credits mine are going to expire in three days which is why I'm making this video today um but maybe you don't have to load in a lot of money and if you have to load in money you can load in five bucks and you can do a lot of stuff with it because when you go to the pricing page you can see when you scroll down to image models that requesting a high resolution image here costs 2 cents per image so two cent per request and if you go for the lower resolutions even less than that so this definitely is not too too expensive now we can do the calculation if I have uh five dollars if I have five dollars divided by 0.02 250 images with five bucks if you want to play around with that um so this is what we're going to use and we're going to use the documentation here to do all of this in Python now in order to be able to use that sample code here we will need to have the openai package installed and we will also need to authenticate with the API key so what we want to do first is we want to open up a command line and we want to type pip install open AI to install the package and once this is installed we can go into our development environment here I already have my API key in a file so what I'm going to do is I'm going to say import open AI and then I'm going to say open AI dot API key equals and we're going to load that file so open API key in reading mode and we want to read the content this is already the authentication process and now with that um being part of our script we can just copy the example here from the documentation we can paste it into the development environment and now we can just choose a prompt for example I can go and say something like uh five yellow docks playing ball in the rain something like that um and then what we do here is we send a response using openai dot image.create we send a prompt we say we want to have one image this is the resolution that we're going for as a result we get an image URL by accessing the data key value pair the first element of that and then accessing the URL key value pair or actually the value of the URL and of course we want to print the result because then we can download the image from that URL so I can just run this now and you're going to see here in a second that I'm going to get a link this one I can click on it and this is the resulting image so we have okay I mean we can see that those are AI generated first of all there are four docs I think I provided five as a prompt so that's not optimal they're kind of yellow but they are not really yellow the the crown is yellow the balls are yellow uh so it's definitely imperfect and this is another thing that's important to mention here the examples are oftentimes very good so you have examples where things work out pretty well this is not always the case so you cannot expect always to get good results oftentimes you're going to get pretty bad results but I think some basic stuff so for example if I say blue car or blue racing car in or on the street I think this is a quite easy prompt so I would expect this to be of higher quality even though I don't know if I should call that or if I should predict that before seeing the image but let's see there you go that's not too bad but you can still see it's AI generated but yeah this is a basic racing car the thing is you can run this multiple times so I can just rerun the code here every time I spend two cents when I do this um but you can see that then we're going to get different results for the same prompt I don't have to change anything but I get different results and maybe I can change racing car to sports car and maybe I will get some BMW or something or some Lambo let's see okay this Works kind of so that is what we can do in a first use case you have to play around with that to be honest I don't know what happens if I just change this now to five if I'm going to get one URL with five images or if I'm going to get uh probably I'm going to get data and then multiple Fields right so let's just go ahead and say print response data just in case I don't get everything I want so now it should create five images and they should be different they should not be with the same seat there you go so you can see we get a data object and then we have uh what do we have here we have multiple dictionaries with the URL key in them so this is the first image here this is the second image there are all very close up so I want to see the full car not just the wheels I don't know why this is the default case but seems like that is the case maybe I have to provide the information that I want to see the full car but I think the more details I add the more mistakes it's going to make but we can try let's go and say blue sports car on the street um with full view on the car this is probably going to go wrong but we can try I think stable diffusion gives you better results to be honest I'm not sure maybe I'm doing it wrong but stable diffusion seems to give you better results and you can also run it offline we don't have to pay for that okay now we can see more of the car maybe that's a coincidence let's see if this is also the case for the other images now we can see a little bit more it's not just the wheels let me just load all of them here yeah I mean it's a little bit better but we still see a very close-up view all right so that's the generation part we can also edit images how does editing images work um quite easily you take some image like here this one on the left this is an example that works quite well again you have a hotel with a pool you have a mask where you have basically the same image but some area is um is transparent so you basically deleted it you upload both these images you provide a prompt and then here for example it says a sunlit indoor Lounge area with a pool containing a flamingo and here you can see now we have a flamingo right um so that is the editing use case now first of all I would like to have something that's worth editing so let me just try to create something else let's say we have a um I don't know maybe some beach beautiful Beach with sand and waves I hope it doesn't mess this up but then we're going to just select an area and maybe insert a dock in there this should be an easy task hopefully it is capable of doing that okay this is a simple Beach so we're going to take that here is the base image I'm going to download this I'm going to save it as image.png then I'm going to open this image PNG here in the Explorer I'm going to where is it there you go I'm going to right click it I'm going to open it with and in I'm going to delete some area so I'm going to use this deletion tool I'm going to go to layer transparency at Alpha Channel now this is also an image editing tutorial and for example maybe we can go and do something like I could enter I could insert a dock here for example or I could say maybe a surfer somewhere here so let's maybe delete this area here and let's say we have some guys surfing so we can export this as mask.png export go into python or go actually into the documentation copy this code go into python delete this part here enter the part here and then we're going to say image PNG mask PNG and we want to have the prompt uh beautiful beach with sand and waves and Surfer surfing the waves let's see if that's enough to get a solid image so now basically here we we should see some Surfer but maybe I didn't I didn't mask out enough let's see what the response is take some more time than just the image generation there you go and oh look at that we have something in here that looks at least somewhat like a surfer so yeah actually this seems to work kind of well um but as I said you can see that this is not perfect I mean when we go to the image you can see that yes this should be a surfer but I mean yeah it could be a server actually but it doesn't look exactly like a server it could be done better I guess it doesn't look realistic we we can see that this is an AI image um let's see what we get now okay this is completely this is bad I mean unless you say this is a guy carrying a surfboard Maybe let's try something else let's go back here and let's just enter a oh this is too much let's just say we want to have a dock here so export mask change the prompt to uh actually almost the same prompt but in waves then with dog lot laying on the beach let's see what we'll get here and then we can take if we get a somewhat good result we can take it and try the variation as well so we can basically this is the third use case you can go uh you can upload an image and you can get a similar version of that image so it's not the same but it's kind of similar it's almost the same uh so let's see what we get here oh we have some dog here but this looks more like a rock to be honest and also doesn't fill all the area why is that maybe I should say with large dock or large or big what's the actual what's the proper word big dog yeah if this doesn't work we're going to just go with one of the images we already have so let's see there you go oh okay now it looks like the dog is dead but I think he's just relaxing let's say that uh so let's take that image now and upload it for a variation so I'm going to just copy this code now I'm going to download the dock um this is the image save image dock and then we're going to replace this part actually I want to show you one of the generations that I did before so um when I go to the prepared images here this was one of the images that were generated I said five purple docs smiling into the camera this is actually quite good I would say for an AI image generation but yeah we're going to now just do that um we're going to use the doc image here doc PNG to generate a variation of that and if this doesn't work well if this looks bad I'm going to just use the prepared image from here so I'm going to copy purple docs PNG that I generated in the preparation for this video let's see what we get here okay now the dog became either waist or a rock or something that doesn't look in any way good but you can see that the waves change so you can see we have still a beach with waves and with sand but it looks different now so this is a variation let's go and try to do the dock image that we have here the purple oh sorry purple docs image so let's purple docs PNG and let's see if we can if we can get some other purple dogs hopefully uh but yeah you can see I don't know if if you think that's better than stable diffusion I don't know if I'm maybe I mean we can do some fine tuning here but this is I think in general for all the models not just for for the image generation so I think it's for chat completion and stuff like that as well but I think that actually stable diffusion if you run it locally with a strong GPU and you have a correct model or a good model uh that you use I think you can get better results there and I also have a video by the way on stable diffusion how to set it up so let's see here oh this looks kind of different so the base image was this one yeah this looks like four purple dog demons but yeah but yeah that's it that is how you use um openai Dolly in Python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting the like button leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye [Music] thank you
Original Description
Today we learn how to generate AI images using OpenAI's DALL-E in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: Prompt Craft
View skill →Related Reads
📰
📰
📰
📰
I Put Google’s Squoosh Codecs in the Browser — and Cut My Image Bill Before the Upload Even…
Medium · Programming
I Put Google’s Squoosh Codecs in the Browser — and Cut My Image Bill Before the Upload Even…
Medium · JavaScript
Which AI tools can generate ready-to-use 3D character models for games, animation, or 3D printing?
Reddit r/artificial
A Practical QA Pipeline for Real Estate Image Deliveries
Dev.to · Eric PixelShouters
🎓
Tutor Explanation
DeepCamp AI