Skip to content

Instantly share code, notes, and snippets.

@franzalex
Created January 29, 2022 11:34
Show Gist options
  • Save franzalex/7048a944d9a216890320f1d0cbd3f5f0 to your computer and use it in GitHub Desktop.
Save franzalex/7048a944d9a216890320f1d0cbd3f5f0 to your computer and use it in GitHub Desktop.
Copies cached Windows 10 (and Windows 11) lock screen images to a specified directory.
#! /usr/bin/python3
"""Copies cached Windows 10 lockscreen images to a specified directory."""
from pathlib import Path
from shutil import copy2 as copy
from os import getenv
from argparse import ArgumentParser
import imghdr
import struct
parser = ArgumentParser("Windows 10 Lockscreen Image Copier",
description="Copies cached Windows 10 lockscreeen images to a specified directory.",
prefix_chars="-/",
add_help=False)
parser.add_argument("-s", "--source",
help="Alternate directory to search for lockscreen images.",
type=Path,
default=Path(getenv("LocalAppData"),
"Packages",
"Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy",
"LocalState",
"Assets"))
parser.add_argument("-d", "--destination",
help="Directory to copy lockscreen images to. Defaults to the script's directory.",
type=Path,
default=Path(__file__).parent)
parser.add_argument("-?", "-h", "--help",
action="help",
help="Show this help message and exit.")
args = parser.parse_known_args()[0]
def main():
"""Main entry point."""
# stop execution if any of the paths fail
if not _test_paths():
return
# copy files
copied = [f.stem for f in args.destination.iterdir() if f.is_file()]
count = 0
for fname in (f for f in args.source.iterdir()
if not f.stem in copied and
f.is_file() and
any(dim > 600 for dim in get_image_size(str(f)))):
print(f"Copying {fname.name}", end=' ')
new_file = Path(copy(str(fname),
str(args.destination / (fname.stem + ".jpg"))))
if new_file.exists():
count += 1
print("")
else:
print("FAILED")
print(f"{count} new file(s) copied.")
def _test_paths():
for (name, path) in {"source": args.source, "destination": args.destination}.items():
if not path.exists():
print(
f"ERROR: {name.title()} does not exist.\r\nPath: {str(path)}")
return False
elif not path.is_dir():
print(
f"ERROR: {name.title()} is not a directory.\r\nPath: {str(path)}")
return False
return True
def test_jpeg(stream, f_obj):
"""Tests a given file to determine if it is a JPEG file.
Arguments:
stream {byte-stream} -- byte-stream to be tested.
f_obj {file-like} -- file-like object to be tested.
Returns:
str -- The string `jpeg` if the specified file is a JPEG file.
"""
# SOI APP2 + ICC_PROFILE
if stream[0:4] == '\xff\xd8\xff\xe2' and stream[6:17] == b'ICC_PROFILE':
# print "A"
return 'jpeg'
# SOI APP14 + Adobe
if stream[0:4] == '\xff\xd8\xff\xee' and stream[6:11] == b'Adobe':
return 'jpeg'
# SOI DQT
if stream[0:4] == '\xff\xd8\xff\xdb':
return 'jpeg'
return None
imghdr.tests.append(test_jpeg)
def get_image_size(fname):
'''Determine the image type of fhandle and return its size.
from draco'''
with open(fname, 'rb') as fhandle:
head = fhandle.read(24)
if len(head) != 24:
return
what = imghdr.what(None, head)
if what == 'png':
check = struct.unpack('>i', head[4:8])[0]
if check != 0x0d0a1a0a:
return
width, height = struct.unpack('>ii', head[16:24])
elif what == 'gif':
width, height = struct.unpack('<HH', head[6:10])
elif what == 'jpeg':
try:
fhandle.seek(0) # Read 0xff next
size = 2
ftype = 0
while not 0xc0 <= ftype <= 0xcf or ftype in (0xc4, 0xc8, 0xcc):
fhandle.seek(size, 1)
byte = fhandle.read(1)
while ord(byte) == 0xff:
byte = fhandle.read(1)
ftype = ord(byte)
size = struct.unpack('>H', fhandle.read(2))[0] - 2
# We are at a SOFn block
fhandle.seek(1, 1) # Skip `precision' byte.
height, width = struct.unpack('>HH', fhandle.read(4))
except: # IGNORE:W0703
return
else:
return
return width, height
if __name__ == "__main__":
from sys import version_info
if version_info >= (3, 6):
main()
else:
print("The minimum version required to run this script is version 3.6")
input("Press <ENTER> to exit.")
@franzalex
Copy link
Author

Copy Windows 10 & 11 Lock Screen Pictures

Description

This is a small app to copy lock screen wallpapers to a directory of your choice.

Usage

Option 1:
Double click on the script.

Option 2:
Run the following command: python.exe copy_lockscreen_wallpapers.py

If you have Python 3.6 or newer installed on your computer, the current lock screen pictures will be copied to the script's parent directory.

To see all the arguments available, the command python.exe copy_lockscreen_wallpapers.py -h.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment