示例#1
0
def main():
    #tests_xmp_core()
    from libxmp import utils

    xmpfile = XMPFiles()
    xmpfile.open_file('samples/img1.png', XMP_OPEN_FORUPDATE)
    xmp = xmpfile.get_xmp()
    #

    print utils.object_to_dict(xmp)
    print "---"
    print xmp.register_namespace('http://purl.org/dc/elements/1.1/', 'dc')
    #	xmpfile.close_file()
    #
    #
    #	xmpfile = XMPFiles()
    #	xmpfile.open_file( 'samples/img1.png', XMP_OPEN_READ )
    #	xmp = xmpfile.get_xmp()

    print xmp.get_prefix_for_namespace('http://purl.org/dc/elements/1.1/')
    print xmp.get_namespace_for_prefix('dc:')
示例#2
0
def loadXmp(inventory):
    for item in inventory:
        if 'xmp' in item:
            try:
                yield (item, object_to_dict(XMPMeta(xmp_str=item['xmp'])))
            except OSError:
                print("!!!WARN Failed to deserialize XMP data in {}".format(
                    item['name']))
                continue
        else:
            print(
                "!!!WARN: File metadata entry doesn't contain serialized xmp data, ignoring item from categories"
            )
def main():
	#tests_xmp_core()
	from libxmp import utils
	
	xmpfile = XMPFiles()
	xmpfile.open_file( 'samples/img1.png', XMP_OPEN_FORUPDATE )
	xmp = xmpfile.get_xmp()
#	


	print utils.object_to_dict(xmp)
	print "---"
	print xmp.register_namespace('http://purl.org/dc/elements/1.1/','dc')
#	xmpfile.close_file()
#
#
#	xmpfile = XMPFiles()
#	xmpfile.open_file( 'samples/img1.png', XMP_OPEN_READ )
#	xmp = xmpfile.get_xmp()
	

	print xmp.get_prefix_for_namespace('http://purl.org/dc/elements/1.1/')
	print xmp.get_namespace_for_prefix('dc:')
示例#4
0
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('')
	def test_object_to_dict_noxmp(self):
		self.assertEqual( object_to_dict( [] ), {} )
	def test_object_to_dict(self):
		for f,fmt in samplefiles.iteritems():
			xmpfile = XMPFiles( file_path=f )
			xmp = xmpfile.get_xmp()
			self.assert_( object_to_dict( xmp ), "Not an XMPMeta object" )
			xmpfile.close_file()
示例#7
0
#         os.utime(f.fileno() if os.utime in os.supports_fd else fname,
#                  dir_fd=None if os.supports_fd else dir_fd, **kwargs)

if __name__ == '__main__':

    xmpf = XMPFiles()
    xmpf.open_file("eric_john_berquist_etd.pdf")
    xmp = xmpf.get_xmp()
    xmpf.close_file()

    # Inspect the formatted XMP.
    xmpfilename = "eric_john_berquist_etd.xmp"
    with open(xmpfilename, 'w') as xmpfile:
        xmpfile.write(str(xmp))

    d = object_to_dict(xmp)
    d_json = dict()

    for k in d:
        print(EQUALS)
        print(k)
        print(DASHES)
        for subheader in d[k]:
            key, value = subheader[:2]
            print(key, value)
            assert len(subheader) == 3
            # This part is metadata about the metadata.
            assert isinstance(subheader[2], dict)

        # Make a JSON representation.
        d_json[k] = dict()
 def test_object_to_dict(self):
     for filename in self.samplefiles:
         xmpfile = XMPFiles( file_path=filename )
         xmp = xmpfile.get_xmp()
         self.assertTrue( object_to_dict( xmp ), "Not an XMPMeta object" )
         xmpfile.close_file()
示例#9
0
 def test_object_to_dict_noxmp(self):
     self.assertEqual(object_to_dict([]), {})
示例#10
0
 def test_object_to_dict(self):
     for filename in self.samplefiles:
         xmpfile = XMPFiles(file_path=filename)
         xmp = xmpfile.get_xmp()
         self.assertTrue(object_to_dict(xmp), "Not an XMPMeta object")
         xmpfile.close_file()
示例#11
0
 def test_object_to_dict(self):
     for f, fmt in samplefiles.iteritems():
         xmpfile = XMPFiles(file_path=f)
         xmp = xmpfile.get_xmp()
         self.assert_(object_to_dict(xmp), "Not an XMPMeta object")
         xmpfile.close_file()