So I collect desktop wallpapers. I have 7719 files weighing in at 16.3 GB. I suppose it could be a problem. I grab new pictures from APOD, NASA's Image of the Day, and NASA's Earth Observatory almost daily. I use John's Background Switcher to make collages. But of course, I want more. I like quality images, and the Windows lockscreen has a nice selection. But how can I save those curated images?

These files are located in C:\​Users\​<username>\​AppData\​Local\​Packages\​Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\​LocalState\​Assets. Those files are named with 64-character hexadecimal names, and have no extensions. Microsoft/Windows will remove/change them unpredictably, so I wrote a little script to grab these images and save them to a different directory with the '.jpg' extension.

import os
import getpass
import json
import pathlib
import shutil

#find asset directory
assetdir=os.environ.get('USERPROFILE','C:\\Users\\'+getpass.getuser())+'\\AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets'

# parse config file
config=pathlib.Path(__file__).resolve().parent
config=config.joinpath('.windowspics.config.json')
if config.exists():
    cfp=open(config,'r')
    configdata=json.load(cfp)
    cfp.close()
    try:
        destdir=configdata['destdir']
    except KeyError:
        raise ValueError('`destdir` not defined')
    try:
        testdate=configdata['date']
    except (KeyError,ValueError,OverflowError,TypeError):
        testdate=0
else:
    raise ValueError('no config file')

# ensure directories exist
a=pathlib.Path(assetdir)
d=pathlib.Path(destdir)
if not (a.exists() and a.is_dir() and d.exists() and d.is_dir()):
    raise ValueError('directory not found')

# check all files in asset directory
files=0
maxdate=testdate
for f in a.glob('*'):
    if f.is_file():
        st=f.stat()
        if st.st_size>102400 and st.st_mtime>testdate:
            dest=d.joinpath(f.name+'.jpg')
            while dest.exists() and len(dest.stem)>1:
                dest=dest.with_name(dest.stem[:-1]+'.jpg')
            if len(dest.stem)==1:
                dest=d.joinpath(f.name+'.jpg')
                i=1
                while dest.exists():
                    dest=dest.with_name(dest.stem+' ({}).jpg'.format(i))
            shutil.copy2(f,dest)
            maxdate=max(st.st_mtime,maxdate)
            files+=1
testdate=maxdate

print('{} files copied...'.format(files))

# write config data back to file
cfp=open(config,'w')
json.dump({'destdir':destdir,'date':testdate},cfp)
cfp.close()

The structure of .windowspics.config.json, which must exist for the above script to work, is very simple:

{
    "destdir": "C:\\\\path\\\\to\\\\wallpaper\\\\directory",
    "date": 1566826904.2192779
}

"date" is the Unix timestamp. It's not essential for the first time the script is run, but if it's not specified later, you may wind up with multiple copies of the same image.