def factory(self): """Initalizes manifest factory and set properties""" factory = ManifestFactory() # instance of ManifestFactory self.setFactoryProperties( factory ) # set properties of ManifestFactory using global variables return factory
def create_manifest(self, imgfiles): """create manifest""" # to be set in sysargs or via imagemagick imageWidth = int(self.width) imageHeight = int(self.height) identifier = "test file import" # setup factory factory = ManifestFactory() factory.set_base_metadata_uri("http://www.example.org/metadata/") factory.set_base_image_uri("http://www.example.org/iiif/") factory.set_iiif_image_info(version="2.0", lvl="2") # setup manifest mf = factory.manifest(label="Manifest") mf.viewingHint = "paged" mf.set_metadata({ "test label": "test value", "next label": "next value" }) mf.attribution = "Provided by Bodleian Library, Oxford, using ManifestFactory code from the Houghton Library, Harvard University" mf.viewingHint = "paged" mf.description = "Description of Manuscript Goes Here" seq = mf.sequence() # loop through images in path for img in imgfiles: # get path, full image name and extension imgPath, imgFullName = os.path.split(img) imgName, imgExt = os.path.splitext(imgFullName) # Mostly identity will come from incrementing number (f1r, f1v,...) # or the image's identity cvs = seq.canvas(ident="c%s" % imgName, label="Canvas %s" % imgName) cvs.set_hw(imageWidth, imageHeight) anno = cvs.annotation() al = cvs.annotationList("foo") # for demo purposes adds choices img = factory.image(imgFullName, iiif=True) img2 = factory.image(imgName + 'b' + imgExt, iiif=True) chc = anno.choice(img, [img2]) json = mf.toString(compact=False) # write to text file text_file = open(identifier.replace(" ", "") + ".json", "w") text_file.write(json) text_file.close() return json
import json from factory import ManifestFactory import urllib import os, sys baseq = "http://data.fitzmuseum.cam.ac.uk/api/?size=1000&query=" q = "Marlay%20AND%20cutting%20AND%20Category:illuminated*" destdir = "/path/to/images/fitzwilliam" fac = ManifestFactory() fac.set_base_image_uri("http://iiif-dev.localhost/services/2.0") fac.set_iiif_image_info(version="2.0") basemd = "http://localhost/prezi/fitz/" basedir = "/path/to/htdocs/prezi/fitz/" fac.set_base_metadata_uri(basemd) fac.set_base_metadata_dir(basedir) fh = urllib.urlopen(baseq+q) data = fh.read() fh.close() results = json.loads(data) mfst = fac.manifest(label="Marlay Cuttings") seq = mfst.sequence() for res in results['results']: if not res.has_key('image'): continue ident = res.get('identifier')
def create_manifest(self): """create manifest""" # to be set in sysargs or via imagemagick imageWidth = int(self.width) imageHeight = int(self.height) identifier = "test" fh = file(self.path) data = fh.read() fh.close() dom = etree.XML(data) metsNS = 'http://www.loc.gov/METS/' modsNS = 'http://www.loc.gov/mods/v3' xlinkNS = 'http://www.w3.org/1999/xlink' ALLNS = {'mets': metsNS, 'mods': modsNS, 'xlink': xlinkNS} # Extract basic info identifier = dom.xpath( '/mets:mets/mets:dmdSec/mets:mdWrap[@MDTYPE="MODS"]/mets:xmlData/mods:mods/mods:recordInfo/mods:recordIdentifier/text()', namespaces=ALLNS)[0] mflabel = dom.xpath( '/mets:mets/mets:structMap[@TYPE="LOGICAL"]/mets:div[@TYPE="manuscript"]/@LABEL', namespaces=ALLNS)[0] manifestType = 'sc:Manifest' # Extract image info images = dom.xpath( '/mets:mets/mets:fileSec/mets:fileGrp/mets:file[@MIMETYPE="image/jpeg"]', namespaces=ALLNS) struct = dom.xpath( '/mets:mets/mets:structMap[@TYPE="PHYSICAL"]/mets:div[@TYPE="physSequence"]/mets:div', namespaces=ALLNS) imageHash = {} for img in images: imageHash[img.xpath('./@ID', namespaces=ALLNS)[0]] = img.xpath( './mets:FLocat/@xlink:href', namespaces=ALLNS)[0] # Configure the factory fac = ManifestFactory() fac.set_base_metadata_uri(self.baseimguri) fac.set_base_image_uri(self.basemetadatauri + identifier + '/') fac.set_iiif_image_conformance(1.1, 1) # Build the Manifest mf = fac.manifest(ident="manifest", label=mflabel) mf.attribution = "Provided by the Bodleian Library, Oxford University" mf.viewingHint = "paged" if manifestType == "PAGEDOBJECT" else "individuals" mf.description = "Description of Manuscript Goes Here" # And walk through the pages seq = mf.sequence(ident="normal", label="Normal Order") for st in struct: # Find label, and image ID label = st.xpath('./@ORDERLABEL', namespaces=ALLNS)[0] image = imageHash[st.xpath('./mets:fptr[1]/@FILEID', namespaces=ALLNS)[0]] # Build the Canvas cvs = seq.canvas(ident="c%s" % image, label=label) cvs.set_hw(imageHeight, imageWidth) # Build the Image Annotation anno = cvs.annotation(ident="a%s" % image) img = anno.image(ident="%s" % image, iiif=True) img.set_hw(imageHeight, imageWidth) # Serialize mfjs = mf.toJSON() srlzd = json.dumps(mfjs, sort_keys=True, indent=2) # write to textfile text_file = open(mflabel.replace(" ", "") + ".json", "w") text_file.write(srlzd) text_file.close() return srlzd
def do_record(rec, rx, host, s, cxn, sequence, lastrec): deleted = rec.xpath('./o:header/@status', namespaces=ALLNS) if deleted: return format = rec.xpath('./o:metadata/odc:dc/dc:format/text()', namespaces=ALLNS) # Strip out non images skip = 0 for f in format: if f.find('/') > -1 and (not f.startswith('image/') or f.endswith('/pdf')): skip = 1 break if skip: return # request info to see if we're compound object (cpd) # if we are, then work backwards from current ID to find records for individual pages # Then reverse the order and call it a Manifest :) identifier = rec.xpath('./o:header/o:identifier/text()', namespaces=ALLNS)[0] idx = int(identifier[identifier.rfind('/') + 1:]) ajaxinfo = get_json( 'http://%s/utils/ajaxhelper/?action=1&CISOROOT=%s&CISOPTR=%s' % (host, s, idx)) try: if not ajaxinfo['imageinfo'].has_key('type'): return except: print 'http://%s/utils/ajaxhelper/?action=1&CISOROOT=%s&CISOPTR=%s' % ( host, s, idx) print ajaxinfo raise if ajaxinfo['imageinfo']['type'] == 'cpd': # Compound Object to process separately # Walk backwards through images until hit previous entry in OAI list # make a folder... print "--- Compound Object: %s" % idx sys.stdout.flush() if os.path.exists('%s/%s/manifest.json' % (s, idx)): return try: os.mkdir("%s/%s" % (s, idx)) except: pass os.chdir("%s/%s" % (s, idx)) if rx == 0: # Need to check last record of previous chunk if lastrec: previd = lastrec.xpath('./o:header/o:identifier/text()', namespaces=ALLNS)[0] start_image = int(previd[previd.rfind('/') + 1:]) + 1 else: start_image = 1 else: prev = recs[rx - 1] previd = prev.xpath('./o:header/o:identifier/text()', namespaces=ALLNS)[0] start_image = int(previd[previd.rfind('/') + 1:]) + 1 pages = [] for imgid in range(start_image, idx): pinfo = {'id': imgid} # get H/W from ajax iajax = get_json( 'http://%s/utils/ajaxhelper/?action=1&CISOROOT=%s&CISOPTR=%s' % (host, s, imgid)) if not iajax['imageinfo'].has_key('height'): continue pinfo['h'] = iajax['imageinfo']['height'] pinfo['w'] = iajax['imageinfo']['width'] if (int(pinfo['h']) == 0 or int(pinfo['w']) == 0): continue try: pinfo['title'] = iajax['imageinfo']['title']['0'] except: pinfo['title'] = "Image %s" % imgid cxn.put("%s::%s::%s" % (host, s, imgid), "%s,%s" % (pinfo['w'], pinfo['h'])) pages.append(pinfo) if not pages: # back to host directory os.chdir('../..') return title = rec.xpath('./o:metadata/odc:dc/dc:title/text()', namespaces=ALLNS) creator = rec.xpath('./o:metadata/odc:dc/dc:creator/text()', namespaces=ALLNS) date = rec.xpath('./o:metadata/odc:dc/dc:date/text()', namespaces=ALLNS) description = rec.xpath('./o:metadata/odc:dc/dc:description/text()', namespaces=ALLNS) language = rec.xpath('./o:metadata/odc:dc/dc:language/text()', namespaces=ALLNS) # reinstantiate factory for subdir. not great but ... cfac = ManifestFactory() cfac.set_base_metadata_uri(BASE_MD_URL + "/%s/%s/%s/" % (host, s, idx)) cfac.set_base_image_uri(BASE_IMG_URL + "/%s/%s/" % (host, s)) cfac.set_base_metadata_dir(os.getcwd()) cfac.set_iiif_image_info("2.0", "1") fac.set_debug('error') cmanifest = cfac.manifest(label=title[0]) try: cmanifest.set_metadata({"Creator": creator[0]}) except: pass try: cmanifest.set_metadata({"Date": date[0]}) except: pass try: cmanifest.set_metadata({"Language": language[0]}) except: pass try: cmanifest.description = description[0] except: pass cmanifest.viewingHint = "paged" cseq = cmanifest.sequence() for p in pages: cvs = cseq.canvas(ident="p%s" % p['id'], label=p['title']) cvs.set_hw(int(p['h']), int(p['w'])) anno = cvs.annotation() img = anno.image(str(p['id']), iiif=True) img.height = p['h'] img.width = p['w'] try: cmanifest.toFile(compact=False) except: print "FAILED TO WRITE %s/%s/manifest.json" % (s, idx) # back to host directory os.chdir('../..') else: # We're just a collection of images h = ajaxinfo['imageinfo']['height'] w = ajaxinfo['imageinfo']['width'] if int(h) == 0 or int(w) == 0: return ttl = ajaxinfo['imageinfo']['title']['0'] cxn.put("%s::%s::%s" % (host, s, idx), "%s,%s" % (w, h)) cvs = sequence.canvas(ident="p%s" % idx, label=ttl) cvs.set_hw(int(h), int(w)) anno = cvs.annotation() img = anno.image(str(idx), iiif=True) img.height = h img.width = w