-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilecontrol.py
284 lines (228 loc) · 9.21 KB
/
filecontrol.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# ///////////////////////////////////////////////////////////////
#
# Hecho por: Jairo Antonio Melo Flórez
# Realizado con: Qt Designer y PySide6
# © 2021 Fundación Histórica Neogranadina
# V: 0.1.0-alpha
# 2022-02-07
#
# filecontrol:
# módulo para leer y guardar las capturas de la cámara
#
# ///////////////////////////////////////////////////////////////
import json
import shutil
import sys
import chdkptp.util as util
import os
import datetime
from db.db_handler import getElementIdByMetadata, wrap_imageWithElement
from exif import Image
import configparser
import bagit
from utils.logcontrol import LogControl as log
import utils.fixJSON as fixJSON
from json import JSONDecodeError
# config
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'setup/config.cfg')
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
# Check compatibility and return images directory
if sys.platform == 'linux':
IMGDIR = config['DEFAULT']['img_dir']
elif sys.platform == 'win32':
IMGDIR = config['DEFAULT']['images_dir_windows']
else:
ctypes.windll.user32.MessageBoxW(
0, "Sistema operativo no soportado", "Error", 0)
sys.exit(0)
class DescargarIMGS:
def __init__(self, imgdata, nombre_proyecto, folio, dev) -> None:
'''
imgdata(bin) = datos binarios de la imagen capturada (jpg)
nombre_proyecto(str) = nombre del proyecto
dev(object) = objeto chdkptp.ChdkDevice()
'''
self.dev = dev # necesario para comunicarse con los archivos remotos
self.imgdata = imgdata
self.nombre_proyecto = nombre_proyecto
self.folio = folio
def img_dir(self, tipo_img):
'''
tipo_img(str) = tipo de imagen (jpg, dng)
Crea directorio si no existe y lo configura como BagIt
return dirección del subdirectorio para la subida de imagen por tipo
'''
img_dir = os.path.join(IMGDIR, self.nombre_proyecto)
os.makedirs(img_dir, exist_ok=True)
# if img_dir is empty create bag:
if not os.path.exists(f'{img_dir}/bagit.txt'):
bag = bagit.make_bag(img_dir, {
'Contact-Name': 'Fundación histórica Neogranadina',
'Contact-Email': '[email protected]',
'Website': 'neogranadina.org',
'Nombre proyecto': self.nombre_proyecto})
# aditional files
# create metadata.json if not exists
if not os.path.exists(f'{img_dir}/metadata.json'):
with open(f'{img_dir}/metadata.json', 'w', encoding='utf-8') as fp:
json.dump({}, fp)
# move metadata.json to bagit root
if not img_dir.endswith('test_config'):
shutil.move(f'{img_dir}/data/config_project.json', f'{img_dir}/config_project.json')
img_dir = os.path.join(IMGDIR, self.nombre_proyecto, 'data', f"{tipo_img.upper()}")
os.makedirs(img_dir, exist_ok=True)
return img_dir
def image_name(self, img_path):
'''
nombre de la imagen
'''
return img_path.split("/")[-1]
def descarga_jpg(self, orientacion):
'''
crea imagen jpg a partir del binario (imgdata)
'''
jpg_path = os.path.join(self.img_dir('jpg'), f"{self.folio}.jpg")
with open(jpg_path, 'wb') as fp:
fp.write(self.imgdata)
self.associateImageWithElement(
self.nombre_proyecto, jpg_path, 'jpg')
if orientacion == "vertical":
img_num = int(jpg_path.split("/")[-1].split(".")[0])
orient = [6 if img_num % 2 == 0 else 8][0]
elif orientacion == "horizontal":
orient = 1
# asegurar orientación correcta
with open(jpg_path, 'rb') as fr: # V0.1.710 fixed error "str" object has no "read" attribute
img = Image(fr)
img.orientation = orient
with open(jpg_path, 'wb') as fp:
fp.write(img.get_file())
print(f"descargada img {jpg_path}")
def descarga_dng(self):
'''
copia la imagen dng desde la memoria de la cámara
'''
img_path = self.dng_remoto()
raw_img = self.dev.download_file(img_path)
if img_path.endswith(".DNG"):
dng_path = os.path.join(self.img_dir('dng'), f"{self.folio}.dng")
if not os.path.exists(dng_path):
with open(dng_path, "wb") as fp:
fp.write(raw_img)
self.associateImageWithElement(
self.nombre_proyecto, dng_path, 'dng')
print(f"descargada img {dng_path}")
self.dev.delete_files(img_path)
def imgs_camara(self, detailed=True):
'''
lista de archivos guardados en la cámara remota
'''
ruta_dirCam = self.dev.list_files()[-1:][0][:-1]
rp = util.to_camerapath(ruta_dirCam)
flist = self.dev._lua.call("con:listdir", rp, dirsonly=False,
stat="*" if detailed else "/")
return flist
def dng_remoto(self):
'''
ruta a la última imagen tomada por la cámara
'''
flist = self.imgs_camara()
ruta_dirCam = self.dev.list_files()[-1:][0][:-1]
ruta_camara = util.to_camerapath(ruta_dirCam)
for l in flist.values():
listac = [f"{ruta_camara}/{v}" for k,
v in l.items() if k == 'name']
return listac[-1]
def imageMetadata(self, image_path):
'''
obtiene los metadatos de una imagen
'''
with open(image_path, 'rb') as fp:
img = Image(fp)
# Agregar exif labels de acuerdo con img.list_all()
imgmetadata = {
'image_description': img.get('image_description'),
'datetime_original': img.get('datetime_original'),
'ResolutionUnit': img.get('resolution_unit'),
'Make': img.get('make'),
'Model': img.get('model'),
'Orientation': img.get('orientation'),
'ShutterSpeedValue': img.get('shutter_speed_value'),
'FlashFired': f"{img.get('flash.flash_fired')}",
'RedEyeReduction': f"{img.get('flash.red_eye_reduction_supported')}",
'WhiteBalance': img.get('white_balance'),
'ExposureMode': img.get('exposure_mode'),
'FocalLength': img.get('focal_length'),
'ExposureTime': img.get('exposure_time'),
'ExifVersion': img.get('exif_version'),
'ApertureValue': img.get('aperture_value'),
}
return imgmetadata
def write_metadata_json(self, img_path, metadata):
'''
escribe los metadatos en un archivo json
'''
print(img_path)
metadata_path = os.path.join(img_path[:-18], "metadata.json")
if not os.path.exists(metadata_path):
with open(metadata_path, 'w', encoding='utf-8') as fp:
json.dump({}, fp)
try:
with open(metadata_path, 'r+', encoding='utf-8') as fp:
data = json.load(fp)
data[self.image_name(img_path)] = metadata
fp.seek(0)
json.dump(data, fp, indent=4, ensure_ascii=False)
except JSONDecodeError as e:
print("JSONDecodeError")
log.log(f"JSONDecodeError: {e} en {metadata_path}")
fixJSON.fixJSON(metadata_path)
with open(metadata_path, 'r+', encoding='utf-8') as fp:
data = json.load(fp)
data[self.image_name(img_path)] = metadata
fp.seek(0)
json.dump(data, fp, indent=4, ensure_ascii=False)
def associateImageWithElement(self, metadata_text, img_path, tipo_img):
'''
get the metadata of each image and associate it with the element
and write it in database
'''
element_id = getElementIdByMetadata(metadata_text)
if tipo_img == 'jpg':
lista = os.listdir(self.img_dir('jpg'))
order = len(lista)
elif tipo_img == 'dng':
lista = os.listdir(self.img_dir('dng'))
order = len(lista)
size = os.path.getsize(img_path)
mime_type = 'image/jpeg' if tipo_img == 'jpg' else 'image/x-adobe-dng'
filename = os.path.basename(img_path)
path = os.path.dirname(img_path)
img_timestamp = datetime.datetime.now()
img_modified_ts = datetime.datetime.now()
img_metadata = self.imageMetadata(img_path)
# metadatos de la imagen
dcmetadata = {
'element_id': element_id,
'order': order,
'img_path': img_path,
'tipo_img': tipo_img,
'size': size,
'mime_type': mime_type,
'filename': filename,
'path': path,
}
# metadatos de la imagen
dcmetadata.update(img_metadata)
self.write_metadata_json(img_path, dcmetadata)
wrap_imageWithElement(element_id, order, size, mime_type, filename,
path, img_timestamp, img_modified_ts,
img_metadata)
class UpdateFiles:
def __init__(self, nombre_proyecto):
self.nombre_proyecto = nombre_proyecto
def finish_and_update(self):
# update bag
bag = bagit.Bag(os.path.join(IMGDIR, self.nombre_proyecto))
bag.save(manifests=True)