From the course: OpenAI API for Python Developers

Create an image gallery with DALL·E

From the course: OpenAI API for Python Developers

Create an image gallery with DALL·E

- [Instructor] So now the next step will be to create our image gallery. So I'm going to try another generation, image generation. I'm going to try a prompt which is a little bit more elaborated like enchanted forest. I'm going to specify also an old oak tree, for example, and just add another keyword, like sunlight to see what it comes up with. Here we go, so that's not bad, and that's going to be automatically saved to the folder. But the next step will be to be able to display this as a gallery. So let's go back to our project. You'll see that here you have this new picture, which is now saved. So the next step will be to then read from this folder to then display all the images in the gallery. And you see that we already have this function, get_file, so let's go check out actually what it does. Basically, it's going to go look for images with this extension, extension jpg or png, and it's going to create an object with the file and a title for the image. So let's go back and we're going to use these functions to, we're going to make an iteration. So first we're going to get all the files with get_files, and after that we're going to calculate the length of images that we have because we're going to use that to make an iteration, but with a range, so to know how long we need to iterate through the collection. So that's going to be "for i in range" like this. So it looks like Copilot is a little bit ahead of me. But what I want to do is to create a container, which is a feature provided by Streamlit to create a layout. And with this container, I want to create a two column layout. So I want to display one image on one column on one side, and the next image on the other side. Well, that's going to be a very simple process, but first I need to create the column. So that's going to be column one, column two. And actually I just need two columns. So I'm going to remove the third one and just specify two. After that, I'm going to then proceed with displaying. So the first image here in column one like this. So we're going to access the file name for this image. You'll see that I'm going to specify image resize because I want to make the images, you see that it was quite big when we saw it in the browser, so I want to make them a little bit smaller. So I'm going to do that with resize. And then I have specified here the desired size. I have specified it line four. So we're going to use that and then pass image resize here for the first argument. Here we go. And we're going to do the same for column two, here, column two. And then we're going to display the image that comes next in the collection. And the other thing that we're going to do, because you can add parameter within this st.image function, so you can add here the caption. So we're going to specify the title. Here as well. I'm going to do for the column two, I'm going to do the same, copy and then specify the next image that comes in the collection. And we got to be careful because we never know if we go beyond the range. So what we want to do is to do some control flow. So we're going to check if we are not beyond the range. So I'm going to do i and check if I am below the length of the array in order to allow the next step, which is to then display the image. So like this, and I'm going to do the same for the column two, but I'm going to check if I'm not beyond the range, and then +1. So this is just to make sure that we are not beyond the scope of the array. So I think that this is good. So let's go check this one out for now. I'm going to refresh. And here it is, so we have our images. So you could certainly resize a bit because you see that for the width and height you can try to adjust to get a better result. But I'm going to leave this to you. So for now, we've done a good job. We have twice the same images. Let's add another one. So for example, I'm going to describe what I'd like, which is maybe to get a picture about wild nature, with animals. I'm going to add this to my collection to create something artistic with nature. Okay, so now we can see the image that was just generated by AI Excellence. And let's see the result now all together in the gallery. That's Excellence. So for this image gallery project, we've managed to harness the power of the image generation models. So the fusion between technologies and human creativity continues to expand, promising exciting developments in the years to come.

Contents