Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with this code
#1
Can someone help with this. I need help turning this program from making gifs to taking individual pictures. Thanks.

#import modules
import RPi.GPIO as GPIO
from time import sleep
import os
import picamera
import pytumblr
from fractions import Fraction

import picamera
from time import sleep
import pygame
import random
import pygame.camera



#create variables to hold commands 
makeVid = "convert -delay 50 image*.jpg animation.gif"
meow3 = "mpg321 sounds/meow3.mp3"
meow2 = "mpg321 sounds/meow2.mp3"

#put you Tumblr API keys here
consumer_key = 'CONSUMER KEY REMOVED'
consumer_secret = 'CONSUMER SECRET REMOVED'
token_key = 'TOKEN KEY REMOVED'
token_secret = 'TOKEN SECRET REMOVED'

#create variables to hold pin numbers
yellowLed = 17
blueLed = 27
buttonGif = 18
buttonpic = 22

# Authenticate via OAuth
client = pytumblr.TumblrRestClient(
  consumer_key,
  consumer_secret,
  token_key,
  token_secret
)

#set up pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonGif, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(buttonpic, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(yellowLed, GPIO.OUT)
GPIO.setup(blueLed, GPIO.OUT)



camera = picamera.PiCamera() #initiate picamera module and class
camera.resolution = (750, 500) #set resolution of picture here
camera.brightness = 60 #set brightness settings to help with dark photos
camera.annotate_foreground = picamera.Color(y=0.2, u=0, v=0) #set color of annotation 


try:
    #read button 
    while True:
        input_state = GPIO.input(buttonGif)
        if input_state == True:
            print('Button Pressed')
            sleep(0.2)
            #if pressed blink yellow LED at two speeds
            for i in range(3):
                GPIO.output(yellowLed, True)
                sleep(1)
                GPIO.output(yellowLed, False)
                sleep(1)
            for i in range(3):
                GPIO.output(yellowLed, True)
                sleep(.25)
                GPIO.output(yellowLed, False)
                sleep(.25)
            #start camera preview                
            camera.start_preview()
            #display text over preview screen
            camera.annotate_text = 'Get Ready!'
            camera.annotate_text = '1'
            #take 6 photos
            for i, filename in enumerate(camera.capture_continuous('image{counter:02d}.jpg')):
                sleep(2)
                if i == 1:
                    camera.annotate_text = '2'
                elif i == 2:
                    camera.annotate_text = '3'
                elif i == 3:
                    camera.annotate_text = '4'
                elif i == 4:
                    camera.annotate_text = '5'
                if i == 5:
                    break
            camera.stop_preview() #stop preview 
            os.system(makeVid) #send command to convert images to GIF
            print('uploading') #let us know photo is about to start uploading
            #upload photo to Tumblr
            client.create_photo('severelycrazypirate', state="published", tags=["Husky photobooth", "raspberry pi", "Huskies"], data="animation.gif")
            print("uploaded") #let us know GIF has been uploaded
            #turn on uploaded LED and play meow samples
            GPIO.output(blueLed, True)
            os.system(meow2)
            os.system(meow2)
            GPIO.output(blueLed, False)

            #pic button.....................................................................

        input_state = GPIO.input(buttonpic)
        if input_state == True:
            print('Button Pressed')
            sleep(0.2)
            #if pressed blink yellow LED at two speeds
            for i in range (3):
                GPIO.output(yellowLed, True)
                sleep(1)
                GPIO.output(yellowLed, False)
                sleep(1)
            for i in range(3):
                GPIO.output(yellowLed, True)
                sleep(.25)
                GPIO.output(yellowLed, False)
                sleep(.25)
                

# INIT CAMERA
            pygame.camera.init()
            camera = picamera.PiCamera()
            camera.vflip = False
            camera.hflip = False
            camera.brightness = 60

# BUILD A SCREEN
            pygame.init()
            screen = pygame.display.set_mode((WIDTH,HEIGHT))
            black = pygame.Color(0, 0, 0)
            textcol = pygame.Color(255, 255, 0)
            screen.fill()
            #start camera preeview
                # TAKE A PHOTO
            camera.start_preview()
            sleep(0.5)
            camera.capture('image.gif', format='gif', resize=(WIDTH,HEIGHT))
            screen.fill(black)
            pygame.display.update()
            camera.stop_preview()

            #READ IMAGE AND PUT ON SCREEM
            img = pygame.image.load('image.gif')
            screen.blit(img, (0, 0))

            #OVERLAY CAPTIONS AS TEXT
            text = quote('Beautiful')
            font = pygame.font.Font('freesansbold.ttf', FONTSIZE)
            font_surf = font.render(text, True, textcol)
            font_rect = font_surf.get_rect()
            font_rect.left = 100
            font_rect.top = 100
            screen.blit(font_surf, font_rect)
            pygame.display.update()

    # WAIT A BIT
            sleep(3)

    
    GPIO.cleanup() #cleanup GPIO channels
    
#hit Ctrl + C to stop program
except KeyboardInterrupt:
    print ('program stopped')
Moderator Larz60+: Added Python tags. Please do this in the future (see help, BBCODE)
Reply
#2
(Apr-03-2017, 05:59 PM)TEST1 Wrote:
#put you Tumblr API keys here
You should NOT put your personal API keys. Please blank these out and allow other uses to generate their own set to test out the code and help you.
Reply
#3
I'm not sure I understand the question as you are already taking individual pictures and then creating an animated gif from those pictures. If you don't want the animated gif, don't create it.

Also you are importing some modules twice.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020