Random wallpapers from AI every day

After reading a recent article about wallpapers in different environments, starting with Microsoft Windows 11 and methods for changing them, I became interested in whether there was an application that could not display ready-made wallpapers from a folder or download them from a resource, but create them periodically using any available AI service.

Googling turned up nothing. Right off the bat, I didn’t find a single service that would create wallpaper from any AI service on the fly. In essence, this is a very simple operation – why not write your script on a free day?

I have already written a plugin for Visual Code to check spelling and Russian-English translations, so I have the OpenAI key. We will try DALL-E 3, to which this key fits. It was mentioned on Reddit that DALL-E can create seamless tiled images, which would be great for generating wallpaper. This was not subsequently confirmed, no matter how hard I tried. So the final version works with the full image format.

It seemed to me that the simplest option was to write a PowerShell script and put it on a task scheduler so that it would run itself every day and change the wallpaper.

The starting point is a document describing modern API DALL-E 3.

Here, in fact, is the entire code for receiving an image from DALL-E in PowerShell:

$body = @{
    "model"   = "dall-e-3"
    "prompt"  = "$prompt --ar 16:9"
    "size"    = "1792x1024"
    "style"   = "vivid"
} | ConvertTo-Json

$response = Invoke-RestMethod -Uri "https://api.openai.com/v1/images/generations" `
    -Method Post `
    -Headers @{ "Authorization" = "Bearer $apiKey"; "Content-Type" = "application/json" } `
    -Body $body

$imageUrl = $response.data[0].url

We will put our prompt in the $prompt variable. I would like to say something special about the resolution. The maximum resolution currently supported by DALL-E is 1792×1024. But problems arise with it – every now and then a square image is created, with margins on the sides. The problem is known, described here. It helps to add the prompt with “–ar 16:9” at the end.

It is a good idea not to overwrite the previous received image, but to create a new one with a random name. Firstly, after a while we will have “favorites”, and secondly, it will be possible to assign different wallpapers from the existing ones to different monitors.

$chars = "abcdefghijklmnopqrstuvwxyz0123456789"
$randomString = -Join ((1..8) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] })
$outputFile = "$outputDir\dalle_generated_$randomString.jpg"

Well, all that remains is to install the resulting image on the desktop:

$code = @"
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
"@
$type = Add-Type -MemberDefinition $code -Name "Wallpaper" -Namespace "Win32" -PassThru
$SPI_SETDESKWALLPAPER = 0x0014
$SPIF_UPDATEINIFILE = 0x01
$SPIF_SENDCHANGE = 0x02
$type::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $outputFile, $SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE)

I chose the simple SystemParametersInfo API method, but it does not allow you to arbitrarily set which of the available monitors to put the wallpaper on – it puts it on everything at the same time. A more complex method that allows you to work with arbitrary monitors and change the way the wallpaper is displayed is described in Pete Hinchley. It creates a complete wrapper for COM on .NET. For my purposes, a simple method is enough; I don’t want to complicate the code too much.

My prompt is a matter of taste. I love wallpapers in the literal sense, with a paper texture and low contrast so that it is not distracting and does not interfere with seeing the icons. “A full-sized wallpaper with flowers and birds and leaves. Muted dark colors, with the texture of rough paper.

This is what the main screen looks like:

in the process of writing an article

in the process of writing an article

The script worked, generating a new image each time (this takes up to ten seconds). All that remains is to run it automatically every day.

Run the script in the task scheduler (pay attention to the command line):

setting up the script to run daily

setting up the script to run daily

Here is the link to github with the script source. Only one file needed, .ps1

What's in the plans: work with all monitors independently, using the Windows API wrapper from Pete Hinchley (link above) and assign different images to different monitors automatically. I also need to deploy Stable Diffusion on a free day and play with it from PowerShell. But I didn’t count on Stable Diffusion from the very beginning – it requires a serious GPU and a lot of memory. I foresee a lot of fuss with the setup, and not every desktop or laptop will have a suitable configuration. But it would be interesting to try.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *