numpyHDR/picamburst.py

82 lines
2.4 KiB
Python
Raw Normal View History

#!//usr/bin/python3
2023-03-24 17:00:09 +01:00
import time
2023-03-24 19:16:41 +01:00
from libcamera import controls
from picamera2 import Picamera2
picam2 = Picamera2()
config = picam2.create_still_configuration()
picam2.configure(config)
2023-03-24 15:37:52 +01:00
picam2.set_controls({"AwbEnable": 1})
picam2.set_controls({"AeEnable": 1})
2023-03-24 16:51:40 +01:00
picam2.set_controls({"AfMode": controls.AfModeEnum.Manual })
picam2.set_controls({"LensPosition": 0.0 })
2023-03-24 18:14:44 +01:00
def get_exposure_stack(factor: int = 2):
'''Returns a list with arrays that contain different exposures controlled by the factor.'''
'''The Autoamtically set exposure of the first frame is saved and multiplied or divided ba the factor to get the above or under epxosures.'''
picam2.start()
time.sleep(1)
print(picam2.capture_metadata())
start = picam2.capture_metadata()
exposure_start = start["ExposureTime"]
gain_start = start["AnalogueGain"]
2023-03-24 20:27:33 +01:00
if exposure_start > 117000:
print("Low Light levels out of range, can't make HDR")
if exposure_start < factor:
print("HIgh Light levels out of range, cant make HDR")
2023-03-24 18:14:44 +01:00
picam2.set_controls({"AeEnable": 0})
confirmed = picam2.capture_metadata()["AeLocked"]
while confirmed != True:
2023-03-24 18:18:52 +01:00
confirmed = picam2.capture_metadata()["AeLocked"]
2023-03-24 18:14:44 +01:00
time.sleep(.1)
picam2.set_controls({"AnalogueGain": gain_start})
confirmed = picam2.capture_metadata()["AnalogueGain"]
2023-03-24 20:27:33 +01:00
while confirmed != gain_start in range(gain_start -1, gain_start +1):
2023-03-24 18:18:52 +01:00
confirmed = picam2.capture_metadata()["AnalogueGain"]
2023-03-24 15:37:52 +01:00
time.sleep(.1)
2023-03-24 18:14:44 +01:00
ev1 = picam2.capture_array()
#print("Picture one is done")
2023-03-24 16:51:40 +01:00
2023-03-24 18:14:44 +01:00
ev_low = int(exposure_start / factor)
picam2.set_controls({"ExposureTime": ev_low})
confirmed = picam2.capture_metadata()["ExposureTime"]
while confirmed not in range(ev_low -100, ev_low + 100 ):
2023-03-24 15:37:52 +01:00
confirmed = picam2.capture_metadata()["ExposureTime"]
time.sleep(.01)
2023-03-24 18:14:44 +01:00
#print("2",confirmed)
ev2 = picam2.capture_array()
#print("Picture 2 is captured to array")
2023-03-24 15:37:52 +01:00
2023-03-24 18:14:44 +01:00
ev_high = int(exposure_start * factor)
picam2.set_controls({"ExposureTime": ev_high})
confirmed = picam2.capture_metadata()["ExposureTime"]
while confirmed not in range(ev_high -100, ev_high + 100 ):
2023-03-24 15:37:52 +01:00
confirmed = picam2.capture_metadata()["ExposureTime"]
time.sleep(.01)
2023-03-24 18:14:44 +01:00
#print("3",confirmed)
ev3 = picam2.capture_array()
#print("Picture 3 is captured")
picam2.stop()
stack = [ev1,ev2,ev3]
return stack
2023-03-24 15:37:52 +01:00