def processImages(): files = globFiles() numFiles = len(files) i = 1 print('Processing images...') for meta in files: metafile = meta with open(meta, 'r') as f: meta = json.load(f) if not meta['dropbox']['outdated']: print("({}/{}) [up to date]\r".format(i, numFiles), end='') yield meta else: meta['dropbox']['outdated'] = False f = 'img/raw/{}.jpg'.format(meta['name']) try: with Image.open(f) as img: exif = img._getexif() xmpObj = XMPFiles(file_path=f, open_forupdate=False).get_xmp() xmp = utils.object_to_dict(xmpObj) if consts.XMP_NS_DC in xmp: xmp = xmp[consts.XMP_NS_DC] purlOrg = {} for k, v, _ in xmp: purlOrg[k] = v purlTitleKey = "dc:title[1]" purlTitle = None if purlTitleKey in purlOrg: purlTitle = purlOrg[purlTitleKey] if purlTitle.strip() == "": purlTitle = None else: purlOrg = None displayname = meta['name'] if purlTitle: displayname = purlTitle print("Using XMP title {}".format(displayname)) w, h = img.size tag = lambda x: exif[TAGS_NR[x]] if TAGS_NR[ x] in exif else None avg = np.round(np.mean(np.array(img), axis=(0, 1))) id = meta['dropbox']['id'] avghex = ('#%02x%02x%02x' % tuple(avg.astype(int))) date = tag('DateTimeOriginal') if not date: date = tag('DateTimeDigitized') if not date: date = tag('DateTime') if not date: raise 'Image file contains no date information!' obj = { 'name': meta['name'], 'displayname': displayname, 'dropbox': meta['dropbox'], 'date': date, 'xmp': xmpObj.serialize_to_str(), 'rating': tag('Rating'), 'view': viewerPath.format(id), 'Copyright': tag('Copyright'), 'colour': avghex, 'original': { 'path': f, 'width': img.width, 'height': img.height } } for (n, o) in genThumbnails(id, img): obj[n] = o with open(metafile, 'w') as f: json.dump(obj, f) yield obj print("({}/{}) [ ok ]\r".format(i, numFiles), end='') except FileNotFoundError as e: removeMeta(meta) # The meta data failed for some reason, # Remove it to force reload raise e i = i + 1 print('')