LoginSignup
0
0

More than 3 years have passed since last update.

pythonでPCの壁紙をジブリにする

Posted at

今回はpythonを用いてPC(Windows)の壁紙をジブリにしてみたいと思います。スクレイピングでジブリ画像を収集し、5秒ごとにランダムなジブリ画像を壁紙に設定します。

素材集め

selenium等を用いてスクレイピングします。ジブリの画像はこちらにあるものを使用します。

from selenium import webdriver
import requests
import time
import os

driver = webdriver.Chrome('d:/python/chromedriver.exe') #chromedriverのパスを入力してください
driver.get('https://www.ghibli.jp/info/013381/')
title_link_list = driver.find_elements_by_xpath('//a[@class="panelarea"]')

if not os.path.exists('ジブリ'):
    os.mkdir('ジブリ')

for i in range(len(title_link_list)):
    time.sleep(1)
    title = driver.find_elements_by_xpath('//a[@class="panelarea"]')[i].get_attribute('title')

    if os.path.exists('ジブリ/' + str(title)):
        continue

    driver.find_elements_by_xpath('//a[@class="panelarea"]')[i].click()
    time.sleep(1)
    img_link_list = driver.find_elements_by_xpath('//a[@class="panelarea"]')

    if not os.path.exists('ジブリ/' + title + '/'):
        os.mkdir('ジブリ/' + title + '/')

    for j in range(len(img_link_list)):
        time.sleep(0.5)
        img_link = img_link_list[j]
        img_link.click()
        time.sleep(0.5)
        img = driver.find_element_by_xpath('//img[@class="pswp__img"]')
        src = img.get_attribute('src')
        res = requests.get(src, stream=True)
        with open('ジブリ/' + title + '/' + title + '_' + str(j + 1) + '.jpg', 'wb') as f:
            f.write(res.content)
        driver.back()

    time.sleep(1)    
    driver.back()

となりの山田くん辺りでプログラムがストップしてしまう(element not interactable)ため、その時はもう一度プログラムを実行してください…
全部で950枚(約240MB)もあるため、少し時間がかかります。

壁紙設定

Windowsの壁紙を変更するにはctypesを使用します。

import glob
import ctypes
import numpy as np
import time

img_list = glob.glob('d:/python/Application/ジブリ/*/**.jpg') #絶対パスを入力してください

while True:
    ctypes.windll.user32.SystemParametersInfoW(20, 0, np.random.choice(img_list), 0)
    time.sleep(5)

参考文献

Python3でWindowsの壁紙を変更する

0
0
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
0
0