python set win10 wallpaper

import os
import win32gui,win32con
import random
from PIL import Image
import re

ds=[
{"paths":["e:/壁纸/"],"type":1},
{"paths":["e:/醒来/"],"type":2,"sizes":[450,466,450]}
];

wallpaper="e:/wallpaper.jpg";
width=1366;
height=768;

def getFiles(path):
    r=[];
    for root,dirs,files in os.walk(path):
        for v in files:
            if re.match('.+\\.(jpg|png|jpeg|bmp|gif)$',v,re.I):
                r.append(os.path.join(root,v))
    return r;

ds=ds[random.randint(0,len(ds)-1)];

if ds["type"]==1:
    paths=ds["paths"];
    paths=getFiles(paths[random.randint(0,len(paths)-1)]);
    random.shuffle(paths);
    wallpaper=paths[random.randint(0,len(paths)-1)];
else:
    sizes=ds["sizes"]
    imgs=[];
    for v in ds["paths"]:
        for v2 in getFiles(v):
            imgs.append(v2);
    i=0;
    while i<11:
        random.shuffle(imgs);
        i+=1;
    pic=Image.new("RGBA",(width,height),(0,0,0,0));
    w=0;
    h=0;
    index=0;
    for size in sizes:
        h=0;
        while h<height:
            img=Image.open(imgs[index%len(imgs)]);
            index+=1;
            if img.width>size:
                img=img.resize((size,int(size/(img.width/img.height))),Image.ANTIALIAS);
            pic.paste(img,(int(w+(size-img.width)/2), h));
            h+=img.height;
        w+=size;
    pic.save(wallpaper,"PNG");
print(wallpaper);
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,wallpaper,1);

 

Guess you like

Origin blog.csdn.net/slwsss/article/details/104253395