Desktop Dreamer - Generate Desktop Backgrounds¶
This is a plugin that automatically generates a unique desktop background based on your interests using OpenAI's API.
Requirements¶
- Python 3.x
requests
libraryopenai
libraryPillow
library
You can install the required libraries using pip:
pip install requests openai Pillow
Setup¶
-
Get OpenAI API Key:
- Visit OpenAI API Keys to get your API Key.
-
Configure API Key:
- Create a file named
AI.key
in the root directory of the project. - Paste your OpenAI API Key as the first line in the
AI.key
file.
- Create a file named
-
Populate Interests:
- Create a file named
interests.txt
in the root directory of the project. - List your interests line by line in the
interests.txt
file. Here is an example:nature technology art science gaming space humankind mathematics anime dungeon and dragons pokemon Japan Programming
- Create a file named
-
Running the Script:
- You can run the script manually or set it up to run automatically using Task Scheduler.
Usage¶
To execute the script, run the following command:
python generate_desktop_v3.py
This will generate a new desktop background based on a randomly selected interest from the interests.txt
file.
Files¶
AI.key
¶
Contains the OpenAI API Key.
interests.txt
¶
A list of your interests. Each line should contain a single interest.
generate_desktop_v3.py
¶
The main script that generates the desktop background.
import openai
from openai import OpenAI
import requests
import random
import ctypes
from datetime import datetime
import os
from io import BytesIO
from PIL import Image
def read_api_key_from_file(file_path):
with open(file_path, 'r') as file:
api_key = file.read().strip()
return api_key
# Read OpenAI API key from the file
api_key_path = 'AI.key'
client = OpenAI(api_key=read_api_key_from_file(api_key_path))
def read_interests_from_file(file_path):
with open(file_path, 'r') as file:
interests = file.read().splitlines()
return interests
def generate_image(category):
text_response = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=f"Describe an interesting desktop background based on the category of {category}",
max_tokens=600,
temperature=1
)
print(f"Generating an exciting background based on {category}")
# Provide a prompt for image generation
prompt = f"{text_response.choices[0].text}"
print(prompt)
# Generate images using the DALL-E model
response = client.images.generate(
model="dall-e-3", # Specify the DALL-E model engine
prompt=prompt,
size="1792x1024",
quality="hd"
)
print(response.data[0].url)
return response.data[0].url
def set_wallpaper(image_path):
# This function sets the wallpaper on Windows using ctypes
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, image_path, 3)
def download_image(url):
response = requests.get(url)
return Image.open(BytesIO(response.content))
def main():
current_date = datetime.now().day
current_month = datetime.now().month
current_year = datetime.now().year
print("Current date:", current_date)
interests_file_path = 'interests.txt'
interests = read_interests_from_file(interests_file_path)
random_interest = random.choice(interests)
image_data = generate_image(random_interest)
image = download_image(image_data)
image_path = os.path.join(os.getcwd(), f'{current_date}_{current_month}_{current_year}_wallpaper_{random_interest}.bmp')
image.save(image_path, 'BMP') # Save as BMP format
set_wallpaper(image_path)
print(f"Wallpaper set to: {image} (Interest: {random_interest})")
if __name__ == "__main__":
main()
README.md
¶
Contains basic information on setting up and using the plugin.
# Generate Desktop Background Plugin
This is a batch file and python script that automatically generates a unique desktop background based on your interests using OpenAI's API!
## Setup Instructions:
1. **API Key:**
- Get your API Key from OpenAI (https://platform.openai.com/api-keys)
- Paste it as the first line in the `AI.key` file (edit with Notepad)
2. **Interests:**
- Populate `interests.txt` with your interests, one per line.
3. **Run the Script:**
- Each execution costs about 8 cents.
4. **Automation (Optional):**
- Set up Task Scheduler to run the batch script every time you log in to your computer!
If you have any questions, reach out to [email protected]
scheduled_generate.bat
¶
A batch script to run the Python script automatically.
@echo off
python3 generate_desktop_v3.py