Skip to content

Instantly share code, notes, and snippets.

@martinohanlon
Created June 15, 2018 15:26
Show Gist options
  • Save martinohanlon/c1fe28f84ac07a3b1ef863e94cf1be42 to your computer and use it in GitHub Desktop.
Save martinohanlon/c1fe28f84ac07a3b1ef863e94cf1be42 to your computer and use it in GitHub Desktop.
A push button animation with guizero
# Install
# - sudo pip3 install guizero
# - sudo pip3 install imageio
# - Attached a picamera and a button to pin 17
# - Run it
from guizero import App, Picture, PushButton, Text
from picamera import PiCamera
from picamera.array import PiRGBArray
from gpiozero import Button
import imageio
# called when a picture is taken
def take_picture():
instructions.value = "processing ..."
camera.capture(camera_output, "rgb")
# append the camera image to the list as a numpy array
animation.images.append(camera_output.array)
# write the animated gif to disk
imageio.mimsave("temp.gif", animation.images)
# truncate the camera output now we have dealt with it
camera_output.truncate(0)
#display the animation
animation.image = "temp.gif"
instructions.value = "now take another"
animation.show()
reset_button.show()
# called to reset the gui
def reset():
instructions.value = "push the button to take a image"
animation.images = []
animation.hide()
reset_button.hide()
# create the button
button = Button(17)
button.when_pressed = take_picture
# create the camera
camera = PiCamera(resolution="640x480")
camera_output = PiRGBArray(camera)
# create the gui
gui = App(title="push button animation", width=640, height=520)
instructions = Text(gui)
animation = Picture(gui)
reset_button = PushButton(gui, command=reset, text="reset")
# reset everything
reset()
# display it
gui.display()
@rsrini7
Copy link

rsrini7 commented Aug 15, 2018

Nice. Can you please suggest, how to use USB Camera instead of Picamera.
i am looking into "https://www.raspberrypi.org/forums/viewtopic.php?t=84388"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment