'''
Capture-10-images-to-DNG

Open camera, capture 10 raw images and store each one into a DNG file, using
the xiapi_dng helper module (Samples/_libs/xiAPI-Python/xiapi_dng.py), which
wraps the xiapi_dng_store native library.
'''

import os
import sys

_script_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(_script_dir, '..', '..', '_libs', 'xiAPI-Python'))

from ximea import xiapi
import xiapi_dng

image_path = os.path.join(_script_dir, 'images')
image_count = 10

# create directory for images
os.makedirs(image_path, exist_ok=True)

cam = xiapi.Camera()

print('Opening first camera...')
cam.open_device()

# settings
cam.set_imgdataformat('XI_RAW8')
cam.set_exposure(100000)

img = xiapi.Image()

print('Starting data acquisition...')
cam.start_acquisition()

meta = xiapi_dng.Metadata(camera=cam)

for i in range(image_count):
    cam.get_image(img)

    filename = os.path.join(image_path, 'image{:02d}.dng'.format(i))
    xiapi_dng.store_image(filename, img, meta)

    print('Image number: ' + str(i))
    print('Image width (pixels):  ' + str(img.width))
    print('Image height (pixels): ' + str(img.height))
    print('Saved to: ' + filename)
    print('')

print('Stopping acquisition...')
cam.stop_acquisition()

cam.close_device()

print('Done.')
