-
Notifications
You must be signed in to change notification settings - Fork 59
/
preview-image.py
executable file
·79 lines (73 loc) · 2.92 KB
/
preview-image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
# python-gphoto2 - Python interface to libgphoto2
# http://github.com/jim-easterbrook/python-gphoto2
# Copyright (C) 2015-22 Jim Easterbrook jim@jim-easterbrook.me.uk
#
# This file is part of python-gphoto2.
#
# python-gphoto2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# python-gphoto2 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with python-gphoto2. If not, see
# <https://www.gnu.org/licenses/>.
import io
import locale
import logging
import os
import subprocess
import sys
from PIL import Image
import gphoto2 as gp
def main():
locale.setlocale(locale.LC_ALL, '')
logging.basicConfig(
format='%(levelname)s: %(name)s: %(message)s', level=logging.WARNING)
callback_obj = gp.check_result(gp.use_python_logging())
camera = gp.check_result(gp.gp_camera_new())
gp.check_result(gp.gp_camera_init(camera))
# required configuration will depend on camera type!
print('Checking camera config')
# get configuration tree
config = gp.check_result(gp.gp_camera_get_config(camera))
# find the image format config item
# camera dependent - 'imageformat' is 'imagequality' on some
OK, image_format = gp.gp_widget_get_child_by_name(config, 'imageformat')
if OK >= gp.GP_OK:
# get current setting
value = gp.check_result(gp.gp_widget_get_value(image_format))
# make sure it's not raw
if 'raw' in value.lower():
print('Cannot preview raw images')
return 1
# find the capture size class config item
# need to set this on my Canon 350d to get preview to work at all
OK, capture_size_class = gp.gp_widget_get_child_by_name(
config, 'capturesizeclass')
if OK >= gp.GP_OK:
# set value
value = gp.check_result(gp.gp_widget_get_choice(capture_size_class, 2))
gp.check_result(gp.gp_widget_set_value(capture_size_class, value))
# set config
gp.check_result(gp.gp_camera_set_config(camera, config))
# capture preview image (not saved to camera memory card)
print('Capturing preview image')
camera_file = gp.check_result(gp.gp_camera_capture_preview(camera))
file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file))
# display image
data = memoryview(file_data)
print(type(data), len(data))
print(data[:10].tolist())
image = Image.open(io.BytesIO(file_data))
image.show()
gp.check_result(gp.gp_camera_exit(camera))
return 0
if __name__ == "__main__":
sys.exit(main())