The AI Image Generator is a Python script that uses a pre-trained Stable Diffusion model to generate images from text prompts. This project leverages the power of the Stable Diffusion model to create high-quality images based on descriptive text inputs.
- Generate images from descriptive text prompts.
- Utilize the pre-trained Stable Diffusion model for high-quality image generation.
- Supports running on GPU if available, for faster processing.
Before you begin, ensure you have met the following requirements:
- You have installed Python 3.6 or higher.
- You have installed the necessary Python libraries: PyTorch, Hugging Face
diffusers
library,transformers
library, and PIL (Python Imaging Library).
To install the necessary libraries, follow these steps:
-
Clone the Repository
Clone the repository to your local machine using the following command:
git clone https://github.com/ziishanahmad/ai-image-generator.git cd ai-image-generator
-
Install Required Libraries
Install the required libraries using
pip
:pip install torch diffusers transformers pillow
To generate an image from a text prompt, follow these steps:
-
Run the Script
Run the
generate_image.py
script using the following command:python generate_image.py
This will generate an image based on the default prompt "mountain sunset" and save it as
generated_mountain_sunset.png
. -
Modify the Prompt
If you want to generate an image with a different prompt, modify the
prompt
variable in the script:prompt = "your descriptive text here"
-
Example
Here is an example of generating an image from the prompt "mountain sunset":
prompt = "mountain sunset" generated_image = generate_image_from_prompt(prompt)
The generated image will be saved to the file
generated_mountain_sunset.png
.
Here is an example of generating an image from the prompt "mountain sunset":
Here is an example of an AI-generated image:
The main script generate_image.py
performs the following steps:
-
Import Libraries
The script imports necessary libraries such as
torch
,transformers
,diffusers
, andPIL.Image
. -
Device Check
The script checks if a GPU is available and sets the device to CUDA if it is, otherwise it uses the CPU.
-
Load Model
The Stable Diffusion model is loaded using the
StableDiffusionPipeline
from Hugging Face. -
Generate Image Function
The
generate_image_from_prompt
function takes a text prompt as input and generates an image using the Stable Diffusion model. It usestorch.no_grad()
to avoid computing gradients, saving memory and computation. -
Example Usage
An example usage of the function is provided, generating an image from the prompt "mountain sunset" and saving it to a file.
Contributions are welcome! If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request.
To contribute:
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE
file for more details.
Author: Zeeshan Ahmad
GitHub: ziishanahmad
LinkedIn: Zeeshan Ahmad