Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the screencap function of gif by python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article "python how to achieve gif screencap function" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "python how to achieve gif screencap function" article.

1. Realization of the main interface

(1) implement the simplest form from tkinter import * if _ _ name__ ='_ _ main__': tk = Tk () tk.geometry ('500x400' 500 '150') tk.title ('interesting transparent form-here we go!') Canvas = Canvas (tk) canvas.pack (fill=BOTH, expand=Y) tk.mainloop ()

It's too easy. I don't want to go into details. I believe everyone can understand it.

(2) set gray to transparent color TRANSCOLOUR = 'gray'tk.wm_attributes ('-transparentcolor', TRANSCOLOUR) (3) place a rectangular box on canvas canvas.create_rectangle (0,0, canvas.winfo_width (), canvas.winfo_height (), fill=TRANSCOLOUR, outline=TRANSCOLOUR) (4) onsize function 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 ()) (V) bind onsize function tk.bind ('', on_resize) II. Implementation of GIF screencap function

(1) Save each time as a picture def _ GifScreen (): global i i + = 1 HWND = win32gui.FindWindow (None,' interesting transparent form-start!') Print (HWND) rect=win32gui.GetWindowRect (HWND) # get the coordinates of the current window rect= (rect [0] + 10 rect rect [1] + 32 10+canvas.winfo_height [0] + 10 recording canvas.winfol (), rect [1] + 10+canvas.winfo_height () + 16) print (rect) im=ImageGrab.grab (rect) # capture the target image im.save (". / out/CaptureScreen_%s.jpeg"% iParticipjpeg') # the first parameter is to save the path The latter parameter is the save format (2) enable a thread

Enable a thread and call the makegif function to intercept gif images.

Def GifScreen (): thread_list = [] T1 = threading.Thread (target=makegif) thread_list.append (T1) (3) implement specific makegif functions

When the number of images generated reaches 20, the create_gif function is called to package the gif file.

Def makegif (): global I imagewriting list while True: _ GifScreen () time.sleep (0.5) if I > 20: break Create_gif (image_list,'out.gif',0.5) (IV) implementation of create_gif Package the pictures in the folder into a GIF file 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) (V) add screencap button And bind the response event GifScreenb = Button (tk, text='GIF screenshot' Command=GifScreen) b.pack () (6) implement the button's response event GifScreendef GifScreen (): thread_list = [] T1 = threading.Thread (target=makegif) thread_list.append (T1) # officially start the current thread for t in thread_list: t.setDaemon (True) t.start () above is the content of this article on "how python implements gif screencap" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report