Taking screenshots

The pi3d library includes a useful screenshot function to capture the screen in a .jpg or .png file. We can add a new key event to trigger it and call pi3d.screenshot("filename.jpg") to save an image (or use a counter to take multiple screenshots), as shown in the following code:

shotnum = 0 #Set counter to 0 
while DISPLAY.loop_running() 
... 
  if inputs.key_state("KEY_P"): 
    while inputs.key_state("KEY_P"): 
      inputs.do_input_events()         # wait for key to go up 
      pi3d.screenshot("screenshot%04d.jpg"%( shotnum)) 
      shotnum += 1 
... 

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.