├── LICENSE ├── README.md ├── dalle3-arg.py ├── dalle3_api.py └── requirements.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Weyaxi 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DALL-E 3 Unofficial API 2 | 3 | The DALL-E 3 Unofficial API is a Python script that allows you to interact with the DALL-E 3 AI image generator app, enabling you to search for and download images created by the model. 4 | 5 | ## Table of Contents 6 | - [Introduction](#dall-e-3-unofficial-api) 7 | - [Prerequisites](#prerequisites) 8 | - [Usage](#usage) 9 | - [Functionality](#functionality) 10 | - [Output](#output) 11 | 12 | ## Prerequisites 13 | 14 | Before using the DALL-E 3 Unofficial API, ensure you have the following prerequisites set up: 15 | 16 | - Python 3.x 17 | - Required Python packages: `undetected_chromedriver`, `selenium`, `requests` 18 | 19 | To install the required packages, run: 20 | 21 | ```bash 22 | pip install undetected-chromedriver selenium requests 23 | ``` 24 | ### Write Your Cookie to `dalle3_api.py` File: 25 | 26 | You should obtain your cookie to run this program. You can follow these steps to obtain your cookie: 27 | 28 | 1. Go to [Bing Image Creator](https://www.bing.com/create) in your browser and log in to your account. 29 | 30 | 2. Then press `Ctrl+Shift+J` to open developer tools. 31 | 32 | 3. Navigate to the Application section. 33 | 34 | 4. Click on Cookies section. 35 | 36 | 5. Find variable `_U` and copy it's value. 37 | 38 | ![image](https://github.com/Weyaxi/Dalle-3-Unoffical-API/assets/81961593/2393dc26-205c-4d5a-bdc7-dd3d7cda6b1b) 39 | 40 | Paste this value in the code. 41 | 42 | ## Usage 43 | 44 | To utilize the DALL-E 3 Unofficial API, follow these steps: 45 | 46 | 1. Clone the repository or download the repo to your local machine. 47 | 48 | ```bash 49 | git clone https://github.com/Weyaxi/Dalle-3-Unoffical-API 50 | ``` 51 | 52 | 2. Open a terminal or command prompt and navigate to the script's directory: 53 | 54 | ```bash 55 | cd Dalle-3-Unoffical-API 56 | ``` 57 | 58 | 3. Run the script `dalle3_arg.py` with a query or queries as an argument to download the generated images: 59 | 60 | ```bash 61 | python3 dalle3_arg.py "your_search_query" "your_search_query_2" 62 | ``` 63 | 64 | ## Functionality 65 | 66 | The DALL-E 3 Unofficial API provides the following functionality:, 67 | 68 | ### Image Search and Download 69 | 70 | - The script opens the Bing Image Creator (DALL-E 3) in a headless Chrome browser. 71 | - It adds a cookie to bypass any automation detection. 72 | - Searches for images based on the provided query. 73 | - Extracts and downloads image URLs. 74 | 75 | ## Output 76 | 77 | The script will create a timestamped folder in the specified directory (usually the current working directory) containing downloaded images. The images will be named with the format "query (index).png." Additionally, the script logs information about the process. 78 | 79 | You can look at this too: 80 | 81 | ``` 82 | query/ 83 | ├─ 10-10-2023 12-24-03/ 84 | │ ├─ query (1).png 85 | │ ├─ query (2).png 86 | │ ├─ query (3).png 87 | │ ├─ query (4).png 88 | ``` 89 | 90 | Please note that this is an unofficial API and may be subject to changes on the target website. Use it responsibly. 91 | -------------------------------------------------------------------------------- /dalle3-arg.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import logging 3 | 4 | logging.basicConfig(level=logging.INFO, format='[%(levelname)s] %(message)s') 5 | 6 | if __name__ == "__main__": 7 | parser = argparse.ArgumentParser(description="Bing Image Creator Downloader (Dalle-3)") 8 | parser.add_argument("queries", type=str, nargs='+', help="Search queries for Bing Image Creator (Dalle-3)") 9 | args = parser.parse_args() 10 | queries = args.queries 11 | 12 | from dalle3_api import * # The import is here because we don't need to import functions and open the webdriver if there is an error in the arguments. 13 | 14 | logging.info(f'{get_time()} Program started') 15 | 16 | for query in queries: 17 | open_website(query) 18 | urls = get_urls() 19 | download_images(urls, query) 20 | -------------------------------------------------------------------------------- /dalle3_api.py: -------------------------------------------------------------------------------- 1 | from undetected_chromedriver import Chrome, ChromeOptions 2 | from selenium.webdriver.common.by import By 3 | from selenium.webdriver.support.ui import WebDriverWait 4 | from selenium.webdriver.support import expected_conditions as EC 5 | from selenium.webdriver.common.keys import Keys 6 | import datetime 7 | import logging 8 | import requests 9 | import os 10 | 11 | logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') 12 | 13 | options = ChromeOptions() 14 | options.add_argument("--disable-blink-features=AutomationControlled") 15 | options.add_argument("--headless") 16 | driver = Chrome(options=options) 17 | cookie_value = "" 18 | 19 | 20 | def get_time(): 21 | return datetime.datetime.now().strftime("[%d/%m/%Y %H:%M:%S]") 22 | 23 | def get_time_save(): 24 | return datetime.datetime.now().strftime("%d-%m-%Y %H-%M-%S") 25 | 26 | def download_images(urls, save_folder): 27 | save_folder = save_folder[:225] # Limit folder name length 28 | try: 29 | timestamp_folder = os.path.join(save_folder, get_time_save()) 30 | os.makedirs(timestamp_folder, exist_ok=True) 31 | 32 | for index, url in enumerate(urls): 33 | try: 34 | response = requests.get(url, timeout=10) 35 | response.raise_for_status() 36 | 37 | filename = os.path.join(timestamp_folder, f"{save_folder} ({index + 1}).png") 38 | with open(filename, 'wb') as file: 39 | file.write(response.content) 40 | 41 | logging.info(f'{get_time()} Image downloaded successfully: "{filename}"') 42 | except requests.exceptions.RequestException as e: 43 | logging.error(f"Failed to download image from {url}: {e}") 44 | except Exception as e: 45 | logging.critical(f"General error in downloading images: {e}") 46 | 47 | def open_website(query): 48 | try: 49 | driver.get('https://www.bing.com/images/create') 50 | logging.info(f"{get_time()} Bing Image Creator opened") 51 | 52 | # Add the required cookie 53 | driver.add_cookie({"name": "_U", "value": cookie_value}) 54 | driver.refresh() 55 | logging.info(f"{get_time()} Cookie added and page refreshed") 56 | 57 | # Input query and search 58 | input_box = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#sb_form_q'))) 59 | input_box.send_keys(query) 60 | input_box.send_keys(Keys.RETURN) 61 | logging.info(f"{get_time()} Query sent: {query}") 62 | 63 | return True 64 | except Exception as e: 65 | logging.critical(f"Error opening website: {e}") 66 | return False 67 | 68 | def get_urls(): 69 | try: 70 | # Wait for image elements to load 71 | image_elements = WebDriverWait(driver, 600).until( 72 | EC.presence_of_all_elements_located((By.CLASS_NAME, "mimg")) 73 | ) 74 | # Extract URLs, skipping invalid ones (like blob URLs) 75 | urls = [ 76 | element.get_attribute("src") 77 | for element in image_elements 78 | if element.get_attribute("src") and not element.get_attribute("src").startswith("blob:") 79 | ] 80 | return list(set(urls)) # Remove duplicates 81 | except Exception as e: 82 | logging.critical(f"Error while extracting image URLs: {e}") 83 | return [] 84 | 85 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests 2 | undetected-chromedriver==3.5.3 3 | selenium==4.9.0 4 | --------------------------------------------------------------------------------