Beautiful picture screen saver based on Python

Generally speaking, the screen saver can protect the screen, because the static screen image for a long time will make the electron beam continue to hit the fixed position of the screen, which will damage the phosphor of the CRT display for a long time; and the beautiful screen saver can protect the eyes, because Long-term boring screen content will cause eye fatigue, and long-term damage to the brains of apes. Combining popular technologies such as crawlers, Baidu search pictures, opencv, etc., this paper has created a screen saver program that displays beautiful pictures. This program has the following advantages:

1. Automatically display, free your hands, because your hands still need to read the code;

2. Every time you start it, it will be displayed randomly, bringing you a different visual experience;

3. Full screen display, Esc to exit, freely switch between reality and fantasy;

No time to explain, drive now! ! !

import os
import cv2
import threading
import time
import requests
import json
import random
from win32api import GetSystemMetrics

WAIT = 200


def fill_blank(img_in, img_w, img_h, pic_w, pic_h):
    resize_img1 = cv2.resize(img_in, (img_w, img_h))
    if img_w % 2 != 0 and img_h % 2 == 0:
        top, bottom, left, right = (pic_h - img_h) / 2, (pic_h - img_h) / 2, (pic_w - img_w) / 2 + 1, (pic_w - img_w) / 2
    elif img_h % 2 != 0 and img_w % 2 == 0:
        top, bottom, left, right = (pic_h - img_h) / 2 + 1, (pic_h - img_h) / 2, (pic_w - img_w) / 2, (pic_w - img_w) / 2
    elif img_h % 2 == 0 and img_w % 2 == 0:
        top, bottom, left, right = (pic_h - img_h) / 2, (pic_h - img_h) / 2, (pic_w - img_w) / 2, (pic_w - img_w) / 2
    else:
        top, bottom, left, right = (pic_h - img_h) / 2 + 1, (pic_h - img_h) / 2, (pic_w - img_w) / 2 + 1, (pic_w - img_w) / 2
    return cv2.copyMakeBorder(resize_img1, int(top), int(bottom), int(left), int(right), cv2.BORDER_CONSTANT, value=[0, 0, 0])


def resize_img(img, pic_w, pic_h):
    h, w = img. shape[0], img. shape[1]

    if h == w:
        longest = h
        scale = longest / float(pic_h)
        img_h, img_w = int(h / scale), int(w / scale)
        return fill_blank(img, img_w, img_h, pic_w, pic_h)
    elif w > h:
        longest = w
        scale = longest / float(pic_w)
        img_h, img_w = int(h / scale), int(w / scale)
        if img_h > pic_h:
            longest1 = h
            scale1 = longest1 / float(pic_h)
            img_h_1, img_w_1 = int(h / scale1), int(w / scale1)
            return fill_blank(img, img_w_1, img_h_1, pic_w, pic_h)
        else:
            return fill_blank(img, img_w, img_h, pic_w, pic_h)
    elif h > w:
        longest = h
        scale = longest / float(pic_h)
        img_h, img_w = int(h / scale), int(w / scale)
        if img_w > pic_w:
            longest1 = w
            scale1 = longest1 / float(pic_w)
            img_h_1, img_w_1 = int(h / scale1), int(w / scale1)
            return fill_blank(img, img_w_1, img_h_1, pic_w, pic_h)
        else:
            return fill_blank(img, img_w, img_h, pic_w, pic_h)


def image_change(image_path):
    imgshow=True
    url_valid = False
    window_name = 'ppp'
    cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
    cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN,
                          cv2.WINDOW_FULLSCREEN)
    cv2.moveWindow(window_name, 0, 0)
    screen_w, screen_h = GetSystemMetrics(0),GetSystemMetrics(1)

    image_page = random.randint(0,1000)
    image_curAlbum = 1
    image_index = 0
    file_0 = image_path + '0'
    file_1 = image_path + '1'
    base_url = 'https://graph.baidu.com/view/similar?' \
               'srcp=crs_simialbum' \
               ' &tn=wise' \
               ' & idctag=tc' \
            ' &sids=10005_10801_10914_10913_11006_10922_10905_10016_10901_10941_10907_11012_10956_10970_10968_10974_11031_11120_1220 2_13203_16202_17008_17023_17025_16104_17105_17114_17851_17863_17071' \
               ' & logid=292853505' \
               ' & amp; promotion_name=aladdin_38009' \
               ' & carousel=10' \
               ' & amp;page={page}' \
               ' & amp; curAlbum={curAlbum}' \
               ' & seed_id=327081865' \
               ' & amp;sign=121b2c0e81707fee99a8b01607159121' \
               ' & index=0'
    if os.path.exists(file_0):
        os. remove(file_0)
    if os.path.exists(file_1):
        os. remove(file_1)
    while True:
        url = base_url. format(page=image_page, curAlbum=image_curAlbum)
        print(url)
        res = requests. get(url)
        res.encoding = 'utf-8'
        tplData_start = res.text.find('window.tplData')
        if tplData_start != -1:
            tplData_end = res.text.find(';', tplData_start)
            if tplData_end != -1:
                tplData = res.text[tplData_start : tplData_end].split('=', 1)[1]
                # print(tplData)
                tpldata = json. loads(tplData)
                # print(tpldata)
                if 'albumList' in tpldata.keys():
                    url_valid = True
                    albumList = tpldata['albumList']
                    for album in albumList:
                        imgList = album['imgList']
                        for img in imgList:
                            imgSrc = img['imgSrc']
                            img_filename_start = imgSrc.rfind('/')
                            img_filename_end = imgSrc.rfind('@')
                            img_filename = imgSrc[img_filename_start:img_filename_end]
                            imgdata = requests.get(imgSrc, stream=True)
                            print(img_filename)
                            open(image_path + str(image_index), 'wb').write(imgdata.content)
                            image_index = 1 - image_index
                            del imgdata

                            if os.path.exists(file_0) and os.path.exists(file_1):
                                img1 = cv2.imread(image_path + str(image_index))
                                img2 = cv2.imread(image_path + str(1 - image_index))
                                #src1 = resize_img(img1, 420, 640)
                                #src2 = resize_img(img2, 420, 640)
                                src1 = resize_img(img1, screen_w, screen_h)
                                src2 = resize_img(img2, screen_w, screen_h)
                                for it in range(WAIT + 1):
                                    if it % 10 == 0:
                                        weight = it / WAIT
                                        res = cv2.addWeighted(src1, 1 - weight, src2, weight, 0)
                                        cv2.imshow(window_name, res)
                                        cv2.waitKey(10)
                            elif os.path.exists(file_0):
                                img1 = cv2.imread(file_0)
                                #src1 = cv2. resize(img1, (420, 640))
                                src1 = cv2.resize(img1, (screen_w, screen_h))
                                cv2.imshow(window_name, src1)
                            k = cv2.waitKey(8000)
                            if k == 27:
                                cv2.destroyAllWindows()
                                return

        image_page = image_page + 1
        #break


image_change('photo\')

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. OpenCV skill treeHomepageOverview 17782 people are studying systematically