python基于tkinter實(shí)現(xiàn)gif錄屏功能
from tkinter import *if __name__ == ’__main__’: tk = Tk() tk.geometry(’500x400+500+150’) tk.title(’有趣的透明窗體-開篇了!!!’) canvas = Canvas(tk) canvas.pack(fill=BOTH, expand=Y) tk.mainloop()
太簡(jiǎn)單了,不詳細(xì)說(shuō)了,相信大家都看得懂。
(二)把灰色設(shè)置成透明色TRANSCOLOUR = ’gray’tk.wm_attributes(’-transparentcolor’, TRANSCOLOUR)(三)放置一個(gè)矩形框在canvas上
canvas.create_rectangle(0, 0, canvas.winfo_width(), canvas.winfo_height(), fill=TRANSCOLOUR, outline=TRANSCOLOUR)(四)讓透明窗體不斷重畫的onsize函數(shù)
def on_resize(evt): tk.configure(width=evt.width,height=evt.height) canvas.create_rectangle(0, 0, canvas.winfo_width(), canvas.winfo_height(), fill=TRANSCOLOUR, outline=TRANSCOLOUR) print(canvas.winfo_width())(五)綁定onsize函數(shù)
tk.bind(’<Configure>’, on_resize)(六)透明窗體的效果
哈哈,透明主界面效果出來(lái)了。
def _GifScreen(): global i i += 1 HWND = win32gui.FindWindow(None,’有趣的透明窗體-開篇了!!!’) print(HWND) rect=win32gui.GetWindowRect(HWND) #獲取當(dāng)前窗口坐標(biāo) rect = (rect[0]+10,rect[1]+32,rect[0]+10+ canvas.winfo_width(),rect[1]+10+canvas.winfo_height()+16) print(rect) im=ImageGrab.grab(rect) #截取目標(biāo)圖像 im.save('./out/CaptureScreen_%s.jpeg' % i,’jpeg’) #前面一個(gè)參數(shù)是保存路徑,后面一個(gè)參數(shù)是保存格式(二)啟用一個(gè)線程
啟用一個(gè)線程,調(diào)用makegif函數(shù),實(shí)現(xiàn)gif圖片的截取
def GifScreen(): thread_list = [] t1 = threading.Thread(target=makegif) thread_list.append(t1)(三)實(shí)現(xiàn)具體的makegif函數(shù)
當(dāng)生成的圖片達(dá)到20張的時(shí)候,就開始調(diào)用create_gif函數(shù)打包成gif文件。
def makegif(): global i,image_list while True:_GifScreen()time.sleep(0.5)if i > 20: break; create_gif(image_list,’out.gif’,0.5)(四)實(shí)現(xiàn)create_gif,將文件夾里面的圖片打包成GIF文件
image_list = []def create_gif(image_list, gif_name, duration = 1.0): frames = [] for image_name in image_list:frames.append(imageio.imread(image_name)) imageio.mimsave(gif_name, frames, ’GIF’, duration=duration)(五)增加錄屏按鈕,并綁定響應(yīng)事件GifScreen
b = Button(tk, text=’GIF截圖’, command=GifScreen)b.pack()(六)實(shí)現(xiàn)按鈕的響應(yīng)事件GifScreen
def GifScreen(): thread_list = [] t1 = threading.Thread(target=makegif) thread_list.append(t1) #正式開啟現(xiàn)線程 for t in thread_list:t.setDaemon(True)t.start()三、整體實(shí)現(xiàn)效果(一)界面效果
代碼量不大,效果還行。再深入研究可以有更多更有趣的應(yīng)用。
以上就是python基于tkinter實(shí)現(xiàn)gif錄屏功能的詳細(xì)內(nèi)容,更多關(guān)于python gif錄屏的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問(wèn)題及解決2. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊3. 為什么你的android代碼寫得這么亂4. Java剖析工具YourKit 發(fā)布5.0版本5. 跟我學(xué)XSL(一)第1/5頁(yè)6. 使用JSP技術(shù)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的在線測(cè)試系統(tǒng)的實(shí)例詳解7. Python中內(nèi)建模塊collections如何使用8. 動(dòng)態(tài)設(shè)置django的model field的默認(rèn)值操作步驟9. 開發(fā)效率翻倍的Web API使用技巧10. Docker創(chuàng)建本地鏡像實(shí)現(xiàn)方法解析
