├── LICENSE ├── imagegenerator.py └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Avishake Adhikary 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /imagegenerator.py: -------------------------------------------------------------------------------- 1 | import tkinter 2 | import customtkinter as ctk 3 | from PIL import ImageTk 4 | import torch 5 | from torch import autocast 6 | from diffusers import StableDiffusionPipeline 7 | 8 | ctk.set_appearance_mode("Dark") # Modes: "System" (standard), "Dark", "Light" 9 | ctk.set_default_color_theme("dark-blue") # Themes: "blue" (standard), "green", "dark-blue" 10 | 11 | class App(ctk.CTk): 12 | def __init__(self): 13 | super().__init__() 14 | # configures window 15 | self.default_window_width = 1200 16 | self.default_window_height = 800 17 | self.authorization_token = "" 18 | 19 | self.title("Image Generator") 20 | self.geometry(f"{self.default_window_width}x{self.default_window_height}") 21 | 22 | # generates user interface 23 | self.windowlabel = ctk.CTkLabel(self,text="Avishake Adhikary's Image Generator", font=ctk.CTkFont(size=30, weight="bold"),padx=50, pady=50,text_color="white") 24 | self.windowlabel.pack() 25 | self.promptlabel = ctk.CTkLabel(self,text="Prompt", font=ctk.CTkFont(family="Times New Roman",size=20, weight="bold"),text_color="white") 26 | self.promptlabel.pack() 27 | self.promptentry = ctk.CTkEntry(self, placeholder_text="Enter your prompt here",width=self.default_window_width-20, height=40) 28 | self.promptentry.pack(padx=20, pady=20) 29 | 30 | self.generatebutton = ctk.CTkButton(master=self,text="Generate Image",width=self.default_window_width-50, height=40,fg_color="transparent", border_width=2, text_color="white",command=self.generate) 31 | self.generatebutton.pack() 32 | 33 | def generate(self): 34 | self.textprompt = self.promptentry.get() 35 | 36 | self.generatebutton.configure(state="disabled") 37 | 38 | self.progress = ctk.CTkProgressBar(master=self,orientation='horizontal',mode='indeterminate') 39 | self.progress.pack() 40 | self.progress.start() 41 | 42 | self.modelid = "CompVis/stable-diffusion-v1-4" 43 | self.device = torch.device("cuda") 44 | self.pipe = StableDiffusionPipeline.from_pretrained(self.modelid, revision="fp16", torch_dtype=torch.float16, use_auth_token=self.authorization_token) 45 | self.pipe.to(self.device) 46 | 47 | with autocast(): 48 | self.image = self.pipe(self.textprompt, guidance_scale=8.5).images[0] 49 | self.image.save('generatedimage.png') 50 | self.img = ImageTk.PhotoImage(self.image) 51 | 52 | self.imageview = ctk.CTkLabel(self,width=600,height=400) 53 | self.imageview.pack() 54 | self.imageview.configure(image=self.img) 55 | 56 | self.progress.stop() 57 | self.progress.pack_forget() 58 | self.generatebutton.configure(state="normal") 59 | 60 | if __name__ == "__main__": 61 | app = App() 62 | app.mainloop() 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Text-To-Image-Generator 2 | 3 | ![image](https://github.com/AvishakeAdhikary/Text-To-Image-Generator/assets/32614982/1fa835b0-f8e0-4900-a696-57fea1196573) 4 | 5 | > This project uses the Stable Diffusion Pipeline to generate images from text prompts. The Stable Diffusion Pipeline is a machine learning model that uses a diffusion process to generate images from text prompts. 6 | > This is a simple GUI application for generating images based on user prompts using the StableDiffusionPipeline model from the diffusers module. The application allows users to enter a prompt, click a button to generate an image based on the prompt, and view the generated image in the GUI window. 7 | 8 | ## Getting Started 9 | 10 | To get started, you will need to have a GPU and install the following dependencies: 11 | 12 | - PyTorch 13 | - diffusers 14 | - matplotlib 15 | 16 | ### Prerequisites 17 | 18 | Before running this application, you will need to have the following installed: 19 | 20 | - Python 3.6 or later 21 | - tkinter module 22 | - customtkinter module 23 | - diffusers module 24 | - torch module 25 | - Pillow module 26 | 27 | You can install these dependencies using pip. For example: 28 | 29 | ``` 30 | pip install tkinter 31 | ``` 32 | ``` 33 | pip install customtkinter 34 | ``` 35 | ``` 36 | pip install diffusers 37 | ``` 38 | ``` 39 | pip install torch 40 | ``` 41 | ``` 42 | pip install Pillow 43 | ``` 44 | 45 | To run this file you need a HuggingFace API Token. 46 | 47 | 1. Go to the Hugging Face website: https://huggingface.co/ 48 | 2. Click on the "Sign In" button in the top-right corner of the page. 49 | 3. Sign in with your Hugging Face account or create a new account if you don't have one. 50 | 4. Once you are signed in, click on your profile picture in the top-right corner of the page and select "Account settings" from the dropdown menu. 51 | 5. On the account settings page, click on the "API token" tab. 52 | 6. Click on the "Generate new token" button to create a new authorization token. 53 | 7. Enter a name for your token in the "Token name" field (e.g. "Image Generator App"). 54 | 8. Choose the permissions you want to grant to your token (e.g. "Read-only" or "Full access"). 55 | 9. Click on the "Generate" button to create your token. 56 | 10. Copy the generated token and use it in your Python code where it says ```self.authorization_token = ""```. 57 | 58 | Once you have installed the dependencies, you can run the code by running the following command: 59 | 60 | ``` 61 | python imagegenerator.py 62 | ``` 63 | 64 | ## Usage 65 | 66 | When you run the code, you will be prompted to enter a text prompt. Once you have entered a text prompt, the Stable Diffusion Pipeline will generate an image based on the text prompt. The generated image will be displayed using matplotlib. 67 | 68 | ![image](https://github.com/AvishakeAdhikary/Text-To-Image-Generator/assets/32614982/633c43f7-65b6-42f7-adee-7f1d206534d6) 69 | 70 | ## Contributing 71 | 72 | We welcome contributions to this project! If you would like to contribute, please follow these steps: 73 | 74 | 1. Fork this repository to your own GitHub account. 75 | 2. Clone the forked repository to your local machine. 76 | 3. Make changes to the code as needed. 77 | 4. Test your changes to make sure they work as expected. 78 | 5. Commit your changes and push them to your forked repository. 79 | 6. Create a pull request on the original repository to submit your changes. 80 | 81 | We will review your pull request and provide feedback as needed. Thank you for contributing to this project! 82 | 83 | ## License 84 | 85 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE) file for details. 86 | --------------------------------------------------------------------------------