基于Python的美丽图片屏幕保护

一般来说,屏幕保护程序能保护屏幕,因为长时间静止的屏幕画面会让电子束持续打击屏幕的固定位置,长期如此会伤害CRT显示器荧光粉;而美丽的屏幕保护程序则能保护眼睛,因为长时间枯燥乏味的屏幕内容会使人眼产生疲劳,长期如此会伤害猿类的大脑神经。本文结合爬虫,百度搜图,opencv等喜闻乐见的技术,生撸出了一个显示美丽图片的屏幕保护程序,本程序具有以下几个优点:

1.自动显示,解放双手,因为你的双手还要撸代码;

2.每次启动,随机显示,带给你不一样的视觉体验;

3.全屏显示,Esc退出,在现实和幻想中自由切换;

没时间解释了,马上开车!!!


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_12202_13203_16202_17008_17023_17025_16104_17105_17114_17851_17863_17071' \
               '&logid=292853505' \
               '&promotion_name=aladdin_38009' \
               '&carousel=10' \
               '&page={page}' \
               '&curAlbum={curAlbum}' \
               '&seed_id=327081865' \
               '&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\\')

 

  • 8
    点赞
  • 12
    评论
  • 3
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值