Random wallpapers from AI every day / Habr

Random wallpapers from AI every day / Habr

After reading a recent article about wallpapers in various environments, starting with Microsoft Windows 11 and methods of changing them, I became interested – if there is no program that can display unfinished wallpapers from a folder or upload from a resource, and create them periodically using any available AI service

Googling yielded nothing. I didn’t find any service that created wallpapers from any AI service on the fly. In fact, this is a very simple operation – why not sketch your script on a free day?

I already wrote a plugin for Visual Code to check spelling and translations in Russian-English, so I have the OpenAI key. Let’s try DALL-E 3, to which this key is suitable. It was mentioned on Reddit that DALL-E can create seamless tiled images, which would be great for creating wallpapers. This was not confirmed later, no matter how hard I tried. So, the final version works with the full image format.

The easiest option seemed to me to write a PowerShell script and put it on the schedule (task scheduler) so that it starts every day and changes the wallpaper.

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

Here, in fact, is the entire code for obtaining an image from DALL-E on 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 permission. The maximum resolution currently supported by DALL-E is 1792×1024. But there are problems with it – after one time a square image is created, with fields on both sides. The problem is known, described here. The advice to supplement the prompt with the instruction “–ar 16:9” at the end helps.

It is a good idea not to overwrite the previous received image, but to create a new one with a random name. First, after some time 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, it remains 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 monitor to put the wallpaper on – it sets everything at once. A more sophisticated method that allows you to work with arbitrary monitors and change how the wallpaper is displayed is described by Pete Hinchley. It creates a complete COM wrapper on .NET. For my purposes a simple method is enough, I don’t want to overcomplicate the code.

My prompt is a matter of taste. I love wallpapers literally, with the texture of paper and low contrast, so that it does not distract and does not interfere with viewing the icons. “A full-sized wallpaper with flowers and birds and leaves. Beaten dull colors, with a pan texture.

This is what the main screen looks like:

in the process of writing the 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):

daily script launch settings

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

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

Related posts