-
Notifications
You must be signed in to change notification settings - Fork 5
/
group_versions.py
executable file
·66 lines (55 loc) · 2.2 KB
/
group_versions.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
#! /usr/bin/env python3
import json
import progressbar
import os
import sys
import sqlite3
import glob
from argparse import ArgumentParser
# Digikam format
# edited_id original_id 2
# Determine the ID of an image in the Digikam database
def image_id(db, name):
c = db.cursor()
c.execute('SELECT * FROM Images WHERE name=?', [os.path.basename(name)])
rows = c.fetchall()
if len(rows) != 1:
raise RuntimeError('Too many matches for "%s": %i' % (name, len(rows)))
return rows[0]['id']
def run(digikam_dir, photos_dir):
db_path = os.path.join(digikam_dir, 'digikam4.db')
try:
db = sqlite3.connect(db_path)
db.row_factory = sqlite3.Row
except Exception as error:
print("Error connecting to DIGIKAM DB: ", error)
sys.exit(1)
bar = progressbar.ProgressBar()
for photo_file in bar(os.listdir(photos_dir)):
found_ext = os.path.splitext(photo_file)[1]
if found_ext != '.json' and found_ext != '.db':
json_file = os.path.join(
photos_dir, os.path.splitext(photo_file)[0] + '.json')
img_file = os.path.join(photos_dir, photo_file)
with open(json_file) as data_file:
data = json.load(data_file)
derived_from = data['derived_from']
if derived_from is not None:
edited_id = image_id(db, img_file)
possible_originals = glob.glob(
os.path.join(photos_dir, derived_from) + '.*')
possible_originals = [
item for item in possible_originals if os.path.splitext(item)[1] != '.json']
if len(possible_originals) != 1:
raise RuntimeError(
'Ambiguous match: %s', possible_originals)
original_id = image_id(db, possible_originals[0])
c = db.cursor()
c.execute(
'INSERT INTO ImageRelations VALUES (?, ?, ?)', [
edited_id, original_id, 2])
db.commit()
db.close()
# Usage: ./group_version.py <digikam_dir> <photos_dir>
if __name__ == '__main__':
run(sys.argv[1], sys.argv[2])