Created
October 15, 2016 05:49
-
-
Save chrisg32/138c4e3366da240472e67a0e3ba8c833 to your computer and use it in GitHub Desktop.
Simple Python script for exporting Windows 10 Lock images for use as desktop background.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys, imghdr | |
from os import listdir, makedirs | |
from os.path import join, isdir, exists | |
from PIL import Image | |
from shutil import copyfile | |
# accept extract location as a command line argument or default to the Desktop | |
extractLocation = sys.argv[1] if len(sys.argv) > 1 else os.path.expanduser("~") + '\\Desktop' | |
# create folder paths to extract the images to | |
extractLocation = join(extractLocation, 'LockImageExtract') | |
tallPath = join(extractLocation, 'Tall') | |
widePath = join(extractLocation, 'Wide') | |
# create extract directories if needed | |
if not exists(extractLocation): | |
makedirs(extractLocation) | |
if not exists(tallPath): | |
makedirs(tallPath) | |
if not exists(widePath): | |
makedirs(widePath) | |
startingPath = join(os.path.expanduser("~"), 'AppData\Local\Packages') | |
microsoftFolderPrefix = 'Microsoft.Windows.ContentDeliveryManager_' | |
subPath = 'LocalState\Assets' | |
microsoftPath = '' | |
# we need to find the path where windows 10 stores the images | |
for directory in listdir(startingPath): | |
microsoftPath = join(startingPath, directory) | |
tempPath = join(startingPath, microsoftFolderPrefix) | |
if isdir(microsoftPath) and microsoftPath.startswith(tempPath): | |
break | |
# set the source path | |
sourcePath = join(microsoftPath, subPath) | |
print('Extracting images from: ' + sourcePath) | |
for fileName in listdir(sourcePath): | |
filePath = join(sourcePath, fileName) | |
fileExtension = imghdr.what(filePath) | |
if isdir(filePath) or not (fileExtension == 'jpeg' or fileExtension == 'png'): | |
continue | |
image = Image.open(filePath) | |
size = image.size | |
# ignore store assets | |
if(size[0] < 300 and size[1] < 300): | |
continue | |
targetDir = tallPath if size[1] > size[0] else widePath | |
targetPath = join(targetDir, fileName + '.' + fileExtension) | |
if not exists(targetPath): | |
copyfile(filePath, targetPath) | |
print('Extracted: ' + targetPath) | |
print('Done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment