LoginSignup
7
4

More than 5 years have passed since last update.

Win10のログイン画面を壁紙フォルダにコピーして,壁紙として使う

Last updated at Posted at 2019-02-06

似たようなことをしている記事(Windows10のロック画面の画像がキレイだから壁紙のスライドショーにしたいと思った - Qiita)もあったんだが,pythonでやりたかったので作ってみた.

壁紙フォルダへのリンク

まず,壁紙の入っているディレクトリへのシンボリックリンクをwallpaperという名前で作っておく.
ディレクトリのパスをコードに直接かいてもいいけど,うっかりQiitaとかにコピペする時に自分の使ってるユーザ名とか出るのは気持ち悪いので,私は通常はコードに固有名詞が入らないようにしてる.お好みで.
あと,コマンドプロンプトは管理者権限で開かないとリンクを作れないので注意.

mklink /D src_dir %LOCALAPPDATA%\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalState\Assets

コード

ログイン画面の画像が消えたらこまるので,一旦複製してから拡張子をつけてリネームし,処理している.

import os
import shutil
import sys
from PIL import Image

src_dir = 'src_dir'
temporal_dir = 'tmp'
dst_dir = 'wallpaper'

# scan destination directory
try:
    files = os.listdir(temporal_dir)
    print(f'{temporal_dir} exists. Do you overwrite here [Y/n]?', flush=True)
    ans = sys.stdin.readline()
    if ans[0] != 'y' and ans[0] != 'Y' and ans[0] != '\n':
        print('aborted!', flush=True)
        sys.exit()
    shutil.rmtree(temporal_dir)
    os.mkdir(temporal_dir)
except FileNotFoundError as ex:
    os.mkdir(temporal_dir)

# copy temporally
for f in os.listdir(src_dir):
    shutil.copyfile('/'.join([src_dir,f]), f'{temporal_dir}/{f}.jpg')

# size check and copy to destination
files = os.listdir(dst_dir)
with os.scandir(temporal_dir) as directory:
    for entry in directory:
        if entry.stat().st_size / 1024 < 100:  # たまにHTMLファイルも混在するので100KiB以下のファイルは除外
            continue
        with Image.open(entry.path) as img:
            w, h = img.size
        if w < 1200 or w < h:  # アイコンや小サイズ画像,縦長画像を除外
            continue
        if entry.name in files:
            continue
        shutil.move(entry.path, dst_dir)

shutil.rmtree(temporal_dir)
7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4