This is the seventh day of my August challenge

GIF article last time to achieve the image synthesis, take a look at our last requirements decomposition

1. Requirement analysis My requirement is to be able to record GIF pictures, because it is the first time to do this kind of thing, so I need to decompose the requirement.

Step 1: I need to be able to record the screen, that is, capture the screen function, save the current screen, the technical solution is not yet decided, this time to achieve

Step 2: I need to be able to combine the screenshots and make GIF images. I have found a suitable library to do it immediately. The implemented

Step 3: Make python files user-friendly, non-core functions, delay.

Step two has been achieved, step one we have achieved this time

The technical analysis still uses The Pillow to implement our screen recording function. Python uses 3.8. The installation of Pillow is detailed in the last article.

PIP install pillow

Write your own GIF generator in Python. Pillow is so damn easy!

3. Detailed decomposition of requirements 1

2. You can adjust the frame rate of the screen recording to prevent the generated GIF file from being too large

3. You can select some areas to record the screen

4. You can choose to delay processing at the beginning

I am testing the code of screen recording function:

def takeSnapTest() :
   m = int(input("Please enter how many seconds you want to capture the screen:"))
   n = 1
   m = frame * m
   while n < m:
       sleep(sleepTime)
       im = ImageGrab.grab()
       local = (r"%s.jpg" % (n))
       im.save(local, 'jpeg')
       n = n + 1
Copy the code

The above code will save the screen generated image in the folder where the current code is located

As you can see from the results, the above code captures the screen and generates an image that connects to the work I’ve been doing to synthesize a GIF from the image

5, show you code requirements are also analyzed, interface is also verified, direct code.

#! /usr/bin/env python
# encoding: utf-8
""" "#Author: coriander @time: 2021/8/90009 8:55 PM """
from time import sleep
 
from PIL import ImageGrab
 
frame = 10
sleepTime = 1.0 / frame
 
def makeGif(imgList) :
   im = imgList[0]
   im.save('beauty.gif', save_all=True, append_images=imgList[1:], loop=0, duration=sleepTime)
 
def takeSnap() :
   m = int(input("Please enter how many seconds you want to capture the screen:"))
   x = int(input("How many seconds to start, 0 starts immediately:"))
   ifx ! =0:
       sleep(x)
   n = 1
   m = frame * m
   imgList = []
   while n < m:
       sleep(sleepTime)
       im = ImageGrab.grab()
       w, h = im.size
       cropim = im.crop((0.0, w / 2, h / 2))
       # local = (r"%s.jpg" % (n))
       imgList.append(cropim)
       # cropim.save(local, 'jpeg')
       n = n + 1
   return imgList
if __name__ == '__main__':
   imgList = takeSnap()
   makeGif(imgList)
Copy the code

Test it out, run it and see what happens

That’s pretty much what I needed.

1. The selection of coordinates can be optimized. The coordinates I set now are directly on the upper left corner, which can be selected by the user when designing the interface

2, can optimize the speed, the current speed has not been accurately calculated, seems to be a bit fast, too lazy to come out, to you.

3, the next thing to do is to make an interface, a little difficult, I’m not good at, may give up, you come on

4. Problems encountered in the process of writing code, such as the interception of pictures, and almost missing the interface to be intercepted with other libraries

Source address: download.csdn.net/download/pe…

Original is not easy, ask for support