吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6739|回复: 7
收起左侧

[Python 转载] 元气桌面动态壁纸录制

  [复制链接]
xuyincheng 发表于 2021-12-16 21:44
如题。。
因为不想玩了,所以只是半成品,导出图片还需要用PR之类的合成视频!!
[Python] 纯文本查看 复制代码
import time
import os
import threading
import win32gui
import win32con
import win32ui
from PIL import Image
from queue import Queue
import ctypes

class Timer(object):	

	def __init__(self):
		freq = ctypes.c_longlong(0)
		ctypes.windll.kernel32.QueryPerformanceFrequency(ctypes.byref(freq))
		self.__freq=freq.value
		self.__beginCount = self.counter()	
	
	def counter(self):
		freq = ctypes.c_longlong(0)
		ctypes.windll.kernel32.QueryPerformanceCounter(ctypes.byref(freq))
		return freq.value
	
	def beginCount(self):		
		self.__beginCount = self.counter()		

	#时间差,精确到微秒	
	def secondsDiff(self):
		self.__endCount = self.counter()
		return (self.__endCount-self.__beginCount)/(self.__freq+0.)


class Record():
    def __init__(self):
        self.hwnd = 0
        self.get_hwnd()
        self.images = Queue(maxsize=0)
        self.flag = False

    def set_fps(self, fps):
        self._fps = fps

    def set_length(self, length):
        self._length = length

    def set_output(self, path):
        self.path = path

    def fecth_image(self):
        hwnd = self.hwnd
        rect = win32gui.GetWindowRect(hwnd)
        width = rect[2] - rect[0]
        height = rect[3] - rect[1]
        hwndDC=win32gui.GetWindowDC(hwnd)
        mfcDC=win32ui.CreateDCFromHandle(hwndDC)
        neicunDC=mfcDC.CreateCompatibleDC()
        savebitmap=win32ui.CreateBitmap()
        savebitmap.CreateCompatibleBitmap(mfcDC,width,height)
        neicunDC.SelectObject(savebitmap)
        neicunDC.BitBlt((0,0),(width, height),mfcDC,(0,0),win32con.SRCCOPY)
        bmpinfo = savebitmap.GetInfo()
        bmpstr = savebitmap.GetBitmapBits(True)
        image = Image.frombuffer("RGB", (width, height), bmpstr, 'raw', 'BGRX', 0, 1)
        self.images.put(image)
        #savebitmap.SaveBitmapFile(neicunDC,filename)
        neicunDC.DeleteDC()
        mfcDC.DeleteDC()
        win32gui.ReleaseDC(hwnd,hwndDC)
        win32gui.DeleteObject(savebitmap.GetHandle())

    def write_out(self):
        i = 1
        path = self.path
        if not os.path.exists(path):
            os.mkdir(path)
        while True:
            if self.flags and self.images.empty():
                break
            if not self.images.empty():
                image = self.images.get()
                filename = os.path.join(path, str(i).zfill(5)+".jpg")
                image.save(filename)
                i+=1
                time.sleep(0.02)

    def record(self):
        n = int(self._fps * self._length)
        wait = 1 / self._fps

        self.flags = False
        t = threading.Thread(target=self.write_out, args=())
        t.start()
        t0 = time.time()
        for i in range(n):
            timer = Timer()
            t = threading.Thread(target=self.fecth_image, args=())
            t.start()
            t.join()
            while True:
                if int(timer.secondsDiff()*1000000) >= int(wait * 1000000):
                    break
        self.flags = True
        print(time.time() - t0)

    def get_hwnd(self):
        win32gui.EnumWindows(self.e_fun_1, 0)
        
    def e_fun_1(self, hwnd, param):
        if win32gui.GetClassName(hwnd) == "WorkerW":
            win32gui.EnumChildWindows(hwnd, self.e_fun_2, None)

    def e_fun_2(self, hwnd, param):
        if win32gui.GetClassName(hwnd) == "KWallpaperPlayerWindow" and win32gui.GetWindowText(hwnd) == "KWallpaper":
            self.hwnd = win32gui.GetParent(hwnd)

Record = Record()
Record.set_fps(25)      #帧率
Record.set_length(30)   #录制时长
Record.set_output("./月亮") #输出文件夹
Record.record()



分享两个自己录的,看下效果....
链接: https://pan.baidu.com/s/1P-LlY0uS8-a-JtOENZ9UZA
提取码: umv6

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
joneqm + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ZorroLee 发表于 2022-4-4 22:30
djjiewufeng 发表于 2022-3-3 16:46
链接挂了 求个鲸落

「SJ03.mkv」https://www.aliyundrive.com/s/mvQrTN1Sr87
点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
djjiewufeng + 1 + 1 谢谢@Thanks!

查看全部评分

wYw729 发表于 2021-12-16 23:33
shenhua97 发表于 2021-12-17 05:27
zm55555 发表于 2021-12-17 09:12
谢谢分享,蛮好的
lykenan 发表于 2021-12-17 09:38
感谢分享
wangyuguo5 发表于 2022-2-14 13:33
连接没有了  可否在分享一下
djjiewufeng 发表于 2022-3-3 16:46
链接挂了 求个鲸落
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 06:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表