Skip to content

Instantly share code, notes, and snippets.

@EndruK
Created August 6, 2017 08:37
Show Gist options
  • Save EndruK/3e47db2df7cdff5e777154afeac4f73c to your computer and use it in GitHub Desktop.
Save EndruK/3e47db2df7cdff5e777154afeac4f73c to your computer and use it in GitHub Desktop.
Little Script for my old webcam to make timelapse shots with an appealing brightness by adjusting the exposure
#!/usr/bin/env python
from PIL import Image
from PIL import ImageStat
import sys
import os.path
import subprocess
import sched,time
import math
steps = 1
def analyze(img):
global exposure
global analyzing
analyzing = True
if exposure > 6000:
exposure = 6000
if exposure < 0:
exposure = 0
brightness = getBrightness(img)
print("################ brightness = "+str(brightness)+" ################")
print("################ exposure = "+str(exposure)+" ################")
if exposure == 0 :
exposure = 1
# if brightness < 50:
if brightness < 100 and exposure < 6000:
if brightness > 95:
print("Bild dunkel - erhoehe Belichtungszeit um 1")
exposure += 1
elif brightness <= 95 and brightness > 40:
print("Bild zu dunkel - erhoehe Belichtungszeit um 50")
exposure += 50
elif brightness <= 40:
print("Bild viel zu dunkel - verdopple Belichtungszeit")
exposure = exposure*2
#zu dunkel
# if brightness < 40 and exposure < 6000:
# print("Bild viel zu dunkel - verdopple Belichtungszeit")
# exposure = exposure * 2
# elif brightness >= 40 and brightness < 95 and exposure < 6000:
# exposure += 50
# elif brightness >= 95 and exposure < 6000:
# print("Bild zu dunkel - erhoehe Belichtungszeit")
# exposure+=steps
deleteImage(img)
img = renameFile(img,int(exposure))
takePicture(img,exposure)
analyze(img)
elif brightness > 140 and exposure > 0:
if brightness < 145:
print("Bild hell - verringere Belichtungszeit um 1")
exposure -= 1
elif brightness >= 145 and brightness < 160:
print("Bild zu hell - verringere Belichtungszeit um 50")
exposure -= 50
elif brightness >= 160:
print("Bild viel zu hell - halbiere Belichtungszeit")
exposure = int(math.floor(exposure/2))
# if brightness > 230 and exposure > 0:
# exposure = math.floor(exposure/2)
# #zu hell
# elif brightness <= 160 and brightness > 145 and exposure > 0:
# exposure -= 50
# elif brightness <= 145 and exposure > 0:
# exposure-=steps
deleteImage(img)
img = renameFile(img,exposure)
takePicture(img,exposure)
analyze(img)
else:
print("error mode setting exposure to 6000")
exposure = 6000
analyzing = False
def deleteImage(img):
os.remove(os.path.dirname(os.path.realpath(__file__))+"/"+img)
def takePicture(img,exposure):
abspath = os.path.dirname(os.path.realpath(__file__))+"/"+img
# fswebcam --no-banner -r 1280x720 --set Exposure=$initExposure "$filename"
# command = "fswebcam --no-banner -r 1280x720 --set Exposure="+str(exposure)+" "+abspath
command = "fswebcam -r 1280x720 --set Exposure="+str(exposure)+" "+abspath
# command = os.path.dirname(os.path.realpath(__file__))+"/capture.sh "+str(exposure)+" "+abspath
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
process.wait()
def renameFile(img,exposure):
file = img.split("e")
left = file[0]
result = left+"e"+str(exposure)+".jpg"
return result
def getBrightness(img):
abspath = os.path.dirname(os.path.realpath(__file__))+"/"+img
if not os.path.isfile(abspath):
print(img+" is not a file")
return
print("Analyzing "+img)
i = Image.open(abspath).convert('L')
brightness = ImageStat.Stat(i)
return brightness.mean[0]
def getFilename(exposure):
t = time.strftime("%Y-%m-%d_%H:%M:%S")
file = t+"-e"+str(exposure)+".jpg"
return file
def doLoop():
global analyzing
global exposure
if not analyzing:
img = getFilename(exposure)
takePicture(img,exposure)
analyze(img)
def main(argv1,argv2):
try:
while True:
doLoop()
time.sleep(int(argv2))
except KeyboardInterrupt:
print("I WANT TO BREAK FREEE - KeyboardInterrupt")
return
# argv1 = initial Exposure
# argv2 = capture interval
if len(sys.argv) <= 1:
sys.exit("Wrong param")
exposure = int(sys.argv[1])
analyzing = False
main(sys.argv[1],sys.argv[2])
# ok brightness at 100-140
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment