def convert(filename): name,extension = os.path.splitext(filename) if extension in ('.xml', '.mei'): return xmltomei.xmltomei(filename) elif extension in ('.json', '.meij'): return jsontomei.jsontomei(filename) else: raise MeiNotYetImplementedError("Support for that file type is not yet implemented.") return None
def highlight_from_mei(input_mei, input_image, output_folder, num_page): mdoc = xmltomei.xmltomei(input_mei) neumes = mdoc.search('neume') clefs = mdoc.search('clef') divisions = mdoc.search('division') custos = mdoc.search('custos') systems = mdoc.search('system') img = load_image(input_image) if img.pixel_type_name != "OneBit": img = img.to_onebit() rgb = Image(img, RGB) neumecolour = RGBPixel(255, 0, 0) clefcolour = RGBPixel(0, 255, 0) divisioncolour = RGBPixel(0, 0, 255) custoscolour = RGBPixel(128, 128, 0) systemscolour = RGBPixel(200, 200, 200) for system in systems: facs = mdoc.get_by_facs(system.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), systemscolour) for neume in neumes: facs = mdoc.get_by_facs(neume.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), neumecolour) note_string = '-'.join([note.pitchname for note in neume.descendants_by_name('note')]) rgb.draw_text((int(facs.ulx) - 0, int(facs.uly) - 20), note_string, RGBPixel(0,0,0), size=10, bold=True, halign="left") # rgb.draw_text((int(facs.ulx) - 20, int(facs.uly) - 50), neume.attribute_by_name('name').value, RGBPixel(0,0,0), size=10, bold=True, halign="left") for clef in clefs: facs = mdoc.get_by_facs(clef.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), clefcolour) for division in divisions: facs = mdoc.get_by_facs(division.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), divisioncolour) for custo in custos: facs = mdoc.get_by_facs(custo.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), custoscolour) rgb.highlight(img, RGBPixel(0,0,0)) filename = num_page + '_pitch_find.png' output_file = os.path.join(output_folder, filename) # print output_file rgb.save_PNG(output_file)
def get(self, pgno): page_number = pgno.rjust(4, "0") page_mei_file = os.path.join(conf.MEI_FILE_PATH, "{0}_corr.mei".format(page_number)) headers = self.request.headers.get("Accept", "").split(",") print headers if "application/json" in headers: print "json" self.set_header("Content-Type", "application/json") mei = xmltomei.xmltomei(page_mei_file) response = meitojson.meitojson(mei) else: print "xml" self.set_header("Content-Type", "application/xml") f = open(page_mei_file, 'r') response = f.read() f.close() self.write(response)
desc = """ This test script runs against the sample MEI documents in a folder and determines whether it can parse them or not. Used to make sure our script is able to handle "real-world" MEI.""" p = OptionParser(usage=usage, description=desc, version="%prog 0.1a") p.add_option("-f", "--folder", action="store", help="Path to folder containing the sample documents") p.add_option("-s", "--stop", action="store_true", help="Stop on errors.") (options, args) = p.parse_args() for f in os.listdir(options.folder): if os.path.isdir(os.path.join(options.folder, f)): continue if os.path.splitext(f)[-1] != ".mei": continue try: x = xmltomei.xmltomei(os.path.join(options.folder, f)) del x except Exception, e: lg.debug("********************************************") lg.debug("*** Failure on {0} ***".format(f)) lg.debug("The error was: {0}".format(e)) lg.debug("********************************************") if options.stop: lg.debug("Stopping!") sys.exit() else: continue
from optparse import OptionParser from pymei.Import import xmltomei if __name__ == "__main__": usage = "usage: %prog [options] input_ground_truth__file input_file_to_compare output_folder" opts = OptionParser(usage) (options, args) = opts.parse_args() gt_file = args[0] comp_file = args[1] output_folder = args[2] ground_truth = xmltomei.xmltomei(gt_file) comparison = xmltomei.xmltomei(comp_file) # use the "search" method to find all <note> elements in the document, and # then create an array of pitches using n.pitchname gt_notes = [n.pitchname for n in ground_truth.search('note')] cp_notes = [c.pitchname for c in comparison.search('note')] print "\nThe number of notes in the ground truth is: {0}".format(len(gt_notes)) print "The number of notes in the comparison is {0}".format(len(cp_notes)) for neume in ground_truth.search('neume'): print neume, neume.attribute_by_name('name').value, for note in neume.children: print note.attribute_by_name('pname').value, print
def highlight_for_testing(outdir, testingdir): for dirpath, dirnames, filenames in os.walk(outdir): if dirpath == outdir: continue lg.debug(dirpath) if ".git" in dirpath.split("/"): continue folder_no = os.path.basename(dirpath) pnum = int(folder_no) lg.debug("Highlighting {0}".format(pnum)) if "{0}_corr.mei".format(folder_no.zfill(4)) in filenames: mfile = "{0}_corr.mei".format(folder_no.zfill(4)) elif "{0}_uncorr.mei".format(folder_no.zfill(4)) in filenames: mfile = "{0}_uncorr.mei".format(folder_no.zfill(4)) else: lg.debug("No mei file found. Continue.") continue mdoc = xmltomei.xmltomei(os.path.join(dirpath, mfile)) neumes = mdoc.search('neume') clefs = mdoc.search('clef') divisions = mdoc.search('division') custos = mdoc.search('custos') systems = mdoc.search('system') img = load_image(os.path.join(dirpath, "{0}_original_image.tiff".format(folder_no.zfill(4)))) if img.pixel_type_name != "OneBit": img = img.to_onebit() rgb = Image(img, RGB) neumecolour = RGBPixel(255, 0, 0) clefcolour = RGBPixel(0, 255, 0) divisioncolour = RGBPixel(0, 0, 255) custoscolour = RGBPixel(128, 128, 0) systemscolour = RGBPixel(200, 200, 200) for system in systems: facs = mdoc.get_by_facs(system.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), systemscolour) for neume in neumes: facs = mdoc.get_by_facs(neume.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), neumecolour) # note_string = '-'.join([note.pitchname for note in neume.children_by_name('note')]) # rgb.draw_text((int(facs.ulx) - 0, int(facs.uly) - 20), note_string, RGBPixel(0,0,0), size=10, bold=True, halign="left") # rgb.draw_text((int(facs.ulx) - 20, int(facs.uly) - 50), neume.attribute_by_name('name').value, RGBPixel(0,0,0), size=10, bold=True, halign="left") for clef in clefs: facs = mdoc.get_by_facs(clef.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), clefcolour) for division in divisions: facs = mdoc.get_by_facs(division.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), divisioncolour) for custo in custos: facs = mdoc.get_by_facs(custo.facs)[0] rgb.draw_filled_rect((int(facs.ulx) - 5, int(facs.uly) - 5), (int(facs.lrx) + 5, int(facs.lry) + 5), custoscolour) rgb.highlight(img, RGBPixel(0,0,0)) rgb.save_PNG(os.path.join(testingdir, '{0}_highlight.png'.format(folder_no.zfill(4))))
mei.add_children([meihead, music]) meifile.addelement(mei) return meifile # import hocr and mei files into lists and strip extension where useful hocrfiles=[x.split('.')[0] for x in glob.glob('????.html')] allmeifiles=glob.glob('*.mei') meifiles=[x.split('_')[0] for x in allmeifiles] # for each hocr file: if corresponding mei file exists, open mei and edit - if not, create new mei if options.corrected: for hocrfile in hocrfiles: output_name='%s_corr.mei' % (hocrfile,) if '%s_corr.mei' % (hocrfile,) in allmeifiles else '%s_uncorr.mei' % (hocrfile,) meifile=xmltomei.xmltomei(output_name) if hocrfile in meifiles else create_mei(hocrfile) surface=meifile.search('surface')[0] section=meifile.search('section')[0] add_text_lines(hocrfile, surface, section) meitoxml.meitoxml(meifile, '../mei_corrtxt/%s' % (output_name,)) else: for hocrfile in hocrfiles: meifile=MeiDocument.MeiDocument() mei=mod.mei_() surface=mod.surface_() section=mod.section_() mei.add_children([surface, section]) add_text_lines(hocrfile, surface, section) meifile.addelement(mei) meitoxml.meitoxml(meifile, '../mei_uncorrtxt/%s_mei_fragment.mei' % (hocrfile,))
from pymei.Import import xmltomei from gamera.core import * import PIL, os init_gamera() from optparse import OptionParser if __name__ == "__main__": usage = "usage: %prog [options] input_mei_file input_image_file output_folder" opts = OptionParser(usage) (options, args) = opts.parse_args() input_file = args[0] output_folder = args[2] mdoc = xmltomei.xmltomei(input_file) neumes = mdoc.search('neume') clefs = mdoc.search('clef') divisions = mdoc.search('division') custos = mdoc.search('custos') systems = mdoc.search('system') img = load_image(args[1]) if img.pixel_type_name != "OneBit": img = img.to_onebit() rgb = Image(img, RGB) neumecolour = RGBPixel(255, 0, 0) clefcolour = RGBPixel(0, 255, 0)