How to create an animated GIF using Python
You can create an animated GIF from a portion of the screen using Python. You can do it using libraries such as pyautogui to capture the portion of the screen and Pillow (PIL) to create the GIF. Here is an example of how to do it:
https://www.electronics-engineering.com/en/how-to-create-an-animated-gif-using-python/

You can create an animated GIF from a portion of the screen using Python. You can do it using libraries such as pyautogui to capture the portion of the screen and Pillow (PIL) to create the GIF. Here is an example of how to do it:
import pyautogui
from PIL import Image, ImageGrab
# Imposta le coordinate e le dimensioni della porzione dello schermo da catturare
x = 100
y = 100
width = 200
height = 200
# Imposta il numero di frame e il nome del file GIF
num_frames = 30
output_file_name = 'output.gif'
# Cattura i frame e crea la GIF
frames = []
for _ in range(num_frames):
# Cattura la porzione dello schermo come immagine
screen_capture = ImageGrab.grab(bbox=(x, y, x + width, y + height))
# Aggiungi l'immagine alla lista dei frame
frames.append(screen_capture)
# Aggiungi una pausa tra i frame (ad esempio, 0.1 secondi)
pyautogui.PAUSE = 0.1
pyautogui.sleep(0.1)
# Salva la lista di frame come GIF animata
frames[0].save(output_file_name, save_all=True, append_images=frames[1:], loop=0, duration=100)
In this example, we are using pyautogui to capture the portion of the screen and Pillow (PIL) to create the animated GIF. Make sure you have these libraries installed on your system. You can do so using pip:
pip install pyautogui pillowRemember that you may need to make some adjustments to the coordinates and the pause times to adapt the code to your specific needs.
It is also possible to make the screen recording last until a specific key is pressed using Python. You can do it using the keyboard library to detect keyboard input and stop recording when a specific key is pressed. Here is an example of how to do it:
import pyautogui
from PIL import Image, ImageGrab
import keyboard
# Imposta le coordinate e le dimensioni della porzione dello schermo da catturare
x = 100
y = 100
width = 200
height = 200
# Imposta il nome del file GIF
output_file_name = 'output.gif'
# Crea una lista per i frame
frames = []
# Cattura i frame finché non viene premuto un tasto
while True:
# Cattura la porzione dello schermo come immagine
screen_capture = ImageGrab.grab(bbox=(x, y, x + width, y + height))
# Aggiungi l'immagine alla lista dei frame
frames.append(screen_capture)
# Aggiungi una pausa tra i frame (ad esempio, 0.1 secondi)
pyautogui.PAUSE = 0.1
pyautogui.sleep(0.1)
# Verifica se il tasto 'q' è stato premuto
if keyboard.is_pressed('q'):
break
# Salva la lista di frame come GIF animata
frames[0].save(output_file_name, save_all=True, append_images=frames[1:], loop=0, duration=100)






Ciao, sono l'autore di «Come creare una gif animata usando python». Domande, dubbi, segnalazioni di errori o richieste di approfondimento sono benvenuti: scriveteli qui sotto e vi rispondo io. Potete anche votare la pagina con le stelle all'inizio dell'articolo.