Example #1
0
def read_xmp(filename, field=None):
    """Reads a field, or the entire XMP file, and returns it. If the field
    cannot be found inside the file and there exists a sidecar file
    then the sidecar file will also be searched for the field.

    Args:
      filename (str): The full path to the base file to read
          (without .xmp extension)
      field (str|None)

    Returns:
      If ``field`` is None return the entire XMP content, else the
      value of the field specified or False if no value found.

    """
    xmpfile = XMPFiles(file_path=filename, open_onlyxmp=True)
    xmp = xmpfile.get_xmp()
    sidecar = '{0}.xmp'.format(filename)

    if field is None and xmp:
        return str(xmp)
    elif field is None and xmp is None:
        if os.path.exists(sidecar):
            return read_xmp_sidecar(sidecar, field=None)
        else:
            return False

    val = _read_xmp_field(xmp, field)
    if val is not None:
        return val
    else:
        if os.path.exists(sidecar):
            return read_xmp_sidecar(sidecar, field)
        else:
            return False
def write_to_file():

    filename = fileEdit.text()
    
    if os.path.isfile(filename) == False:
        print("Error: Media file or reference file missing")
    else:
        xmpfile = XMPFiles( file_path=filename, open_forupdate=True)

        xmp = XMPMeta()

        # Write creator
        xmp.append_array_item(consts.XMP_NS_DC, 'creator', escape(str(creatorEdit.text())), {'prop_array_is_ordered': True, 'prop_value_is_array': True})

        # Write title
        xmp.append_array_item(consts.XMP_NS_DC, 'title', escape(str(titleEdit.text())), {'prop_array_is_ordered': True, 'prop_value_is_array': True})

        # Write sources
        for line in referencesEdit.toPlainText().split("\n"):
            if (len(line) > 1):
                xmp.append_array_item(consts.XMP_NS_DC, 'source', escape(str(line)), {'prop_array_is_ordered': True, 'prop_value_is_array': True})
        """
        if (xmpfile.can_put_xmp(xmp) == False):
            editLabel.setText ('Error: Cannot write metadata to file.')
        else:
        """
        xmpfile.put_xmp(xmp)
        xmpfile.close_file()
        editLabel.setText ('Success! Metadata written to file.')
Example #3
0
    def test_text_property_450_file(self):
        files = ["fixtures/BlueSquare450.xmp", "fixtures/BlueSquare450.tif"]
        options = [
            'open_nooption',
            'open_read',
            'open_forupdate',
            'open_onlyxmp',
            'open_cachetnail',
            'open_strictly',
            'open_usesmarthandler',
            'open_usepacketscanning',
            'open_limitscanning',
        ]
        for f in files:
            for o in options:
                try:
                    xmpfile = XMPFiles(file_path=f, **{o: True})
                    xmp_data = xmpfile.get_xmp()
                    headline = xmp_data.get_property(NS_PHOTOSHOP, 'Headline')

                    self.assertEqual(headline[-5:], "=END=")
                    self.assertTrue(
                        len(headline) > 450, "Not all text was extracted.")
                except ((IOError, XMPError)):
                    pass
Example #4
0
    def verify_metadatas_are_removed(self, file_path):
        # helper function that verifies that the provided image doesn't contain any sensitive metadata
        # empty exif
        self.assertEqual(piexif.load(file_path)["0th"], {},
                         msg="sensitive exif data left")
        self.assertEqual(piexif.load(file_path)["Exif"], {},
                         msg="sensitive exif data left")
        self.assertEqual(piexif.load(file_path)["GPS"], {},
                         msg="sensitive exif data left")
        self.assertEqual(piexif.load(file_path)["1st"], {},
                         msg="sensitive exif data left")
        # Imagine the following scenario: An image contains sensitive information, it gets modified to hide these.
        # If there is an exif-thumbnail it might represent the previous image and hence could leak those information.
        self.assertEqual(piexif.load(file_path)["thumbnail"],
                         None,
                         msg="The exif thumbnail has not been removed.")

        # verify that xmp is also empty. Normally the xmp content is stored in within the rdf tag
        xmp_file = XMPFiles(file_path=file_path)
        xmp_content = str(xmp_file.get_xmp())
        # this won't match if there are any additional xmp elements left, because they would occur between the opening
        # of the rdf:Description tag and the closing of the rdf:RDF tag.
        sensitive_information = re.findall("  <rdf:Description.*\n </rdf:RDF>",
                                           xmp_content)
        self.assertEqual(
            len(sensitive_information),
            1,
            msg="There are sensitive xmp-tags left:\n\n{}".format(xmp_content))
        xmp_file.close_file()
Example #5
0
 def test_does_property_exist(self):
     filename = pkg_resources.resource_filename(
         __name__, "fixtures/BlueSquare450.tif")
     xmp = XMPFiles(file_path=filename)
     xmp_data = xmp.get_xmp()
     self.assertTrue(
         xmp_data.does_property_exist("http://ns.adobe.com/photoshop/1.0/",
                                      'Headline'))
 def test_open_open_limitscanning(self):
     """Verify this library failure."""
     # Issue 5
     filenames = [pkg.resource_filename(__name__, "samples/BlueSquare.pdf"),
                  pkg.resource_filename(__name__, "samples/BlueSquare.xmp")]
     xmpfile = XMPFiles()
     for filename in filenames:
         with self.assertRaises(XMPError):
             xmpfile.open_file(filename, open_limitscanning=True)
 def test_open_open_limitscanning(self):
     """Verify this library failure."""
     # Issue 5
     filenames = [
         pkg.resource_filename(__name__, "samples/BlueSquare.pdf"),
         pkg.resource_filename(__name__, "samples/BlueSquare.xmp")
     ]
     xmpfile = XMPFiles()
     for filename in filenames:
         with self.assertRaises(XMPError):
             xmpfile.open_file(filename, open_limitscanning=True)
def extract_caption(filename):
	caption = "Hmm. There doesn't seem to be any caption in this picture. Maybe you should download something from Media Express, RP or FotoStation and try again."
	try:
	    path_to_image = os.path.join(here, 'uploads/' + filename)
	    xmpfile = XMPFiles( file_path=path_to_image, open_forupdate=True )
	    xmp = xmpfile.get_xmp()
	    raw_caption = xmp.get_property( consts.XMP_NS_DC, 'description[1]' )
	    caption = raw_caption.split("REUTERS")[0]
	    return caption
	except Exception:
	   	return caption
Example #9
0
 def test_write_in_readonly(self):
     """If not "open_forupdate = True", should raise exception"""
     # Note, the file should have been opened with "open_forupdate = True"
     # so let's check if XMPMeta is raising an Exception.
     xmpfile = XMPFiles()
     filename = os.path.join(self.tempdir, 'sig05-002a.tif')
     xmpfile.open_file(filename)
     xmp_data = xmpfile.get_xmp()
     xmp_data.set_property( NS_PHOTOSHOP, 'Headline', "Some text")
     self.assertRaises( XMPError, xmpfile.put_xmp, xmp_data )
     self.assertEqual( xmpfile.can_put_xmp( xmp_data ), False )
Example #10
0
def inspo_xmp_to_dict(file):
    out = {}

    # get xmp from file
    xmpfile = XMPFiles(file_path=file, open_forupdate=True)
    xmp = xmpfile.get_xmp()

    for article in ARTICLES:
        if xmp.does_property_exist(INSPO_URI, article):
            out[article] = xmp.get_property(INSPO_URI, article)

    return out
Example #11
0
 def test_open_use_smarthandler(self):
     """Verify this library failure."""
     # Issue 5
     filenames = [
         pkg_resources.resource_filename(__name__,
                                         "samples/BlueSquare.pdf"),
         pkg_resources.resource_filename(__name__, "samples/BlueSquare.ai"),
         pkg_resources.resource_filename(__name__, "samples/BlueSquare.xmp")
     ]
     xmpfile = XMPFiles()
     for filename in filenames:
         with self.assertRaises(XMPError):
             xmpfile.open_file(filename, open_usesmarthandler=True)
    def test_print_bom(self):
        """Should be able to print XMP packets despite BOM."""
        # The BOM cannot be decoded from utf-8 into ascii, so a 2.7 XMPMeta
        # object's __repr__ function would error out on it.

        filename = pkg.resource_filename(__name__, "samples/BlueSquare.jpg")
        xmpf = XMPFiles()
        xmpf.open_file(file_path=filename)
        xmp = xmpf.get_xmp()
        with patch('sys.stdout', new=StringIO()) as fake_out:
            print(xmp)
            repr(xmp)
        self.assertTrue(True)
    def test_print_bom(self):
        """Should be able to print XMP packets despite BOM."""
        # The BOM cannot be decoded from utf-8 into ascii, so a 2.7 XMPMeta
        # object's __repr__ function would error out on it.

        filename = pkg.resource_filename(__name__, "samples/BlueSquare.jpg")
        xmpf = XMPFiles()
        xmpf.open_file(file_path=filename)
        xmp = xmpf.get_xmp()
        with patch('sys.stdout', new=StringIO()) as fake_out:
            print(xmp)
            repr(xmp)
        self.assertTrue(True)
Example #14
0
 def test_open_use_smarthandler(self):
     """Verify this library failure."""
     # Issue 5
     filenames = [pkg_resources.resource_filename(__name__,
                                                  "samples/BlueSquare.pdf"),
                  pkg_resources.resource_filename(__name__,
                                                  "samples/BlueSquare.ai"),
                  pkg_resources.resource_filename(__name__,
                                                  "samples/BlueSquare.xmp")]
     xmpfile = XMPFiles()
     for filename in filenames:
         with self.assertRaises(XMPError):
             xmpfile.open_file(filename, open_usesmarthandler=True)
def UpdateFileMetadata(filename, datetime):
    exif = GExiv2.Metadata(filename)
    if exif is not None:
        fileDate = GetDateFromExif(exif)
        if fileDate is not None:
            fileDate = datetime.strptime(fileDate, DATE_FORMAT)
            # date within acceptable limit. don't update
            if abs((fileDate - datetime).days) <= FILE_METADATA_DATE_TOLERANCE:
                return

    log('Updating exif: %s to date: %s', \
        filename, datetime.strftime(DATE_FORMAT))

    if DRY_RUN == False:
        if exif is not None:
            exif['Exif.Photo.DateTimeOriginal'] = datetime.strftime(
                DATE_FORMAT)
            exif['Exif.Photo.DateTimeDigitized'] = datetime.strftime(
                DATE_FORMAT)
            exif['Exif.Image.DateTime'] = datetime.strftime(DATE_FORMAT)
            exif['Exif.Image.DateTimeOriginal'] = datetime.strftime(
                DATE_FORMAT)
            exif.save_file()

        xmpfile = XMPFiles(file_path=filename, open_forupdate=True)
        xmp = xmpfile.get_xmp()
        if xmp is not None:
            for xmpConst in XMP_CONSTANTS:
                for dateConst in DATE_CONSTANTS:
                    if xmp.does_property_exist(xmpConst, dateConst):
                        xmp.set_property( \
                            xmpConst, dateConst, datetime.strftime(DATE_FORMAT_XMP))
            if (xmpfile.can_put_xmp(xmp)):
                xmpfile.put_xmp(xmp)
                xmpfile.close_file()
Example #16
0
def update_xmp(imgpath, keywords):
    """ updates the xmp data in the image, or creates a sidecar xmp """

    # Check if a sidecar file already exists
    if os.path.isfile(imgpath + '.xmp'):
        imgpath = imgpath + '.xmp'

    # NEF requires sidecar
    embeddedXmpFormats = ['jpg', 'png', 'tif', 'dng']

    if not imgpath.lower().endswith(tuple(embeddedXmpFormats)):
        # create and use sidecar file
        imgpath = imgpath + '.xmp'
        with open(imgpath, 'w+') as f:
            f.write(blank_xmp())
            print('wrote in' + imgpath)

    xmpfiledict = file_to_dict(imgpath)
    existing_keywords = []
    try:
        dc = []
        dc.append(xmpfiledict[consts.XMP_NS_DC])
        existing_keywords = [x[1] for x in dc]
    except:
        print('nothing')
    print('existing_keywords')
    print(existing_keywords)

    xmpfile = XMPFiles(file_path=imgpath, open_forupdate=True)
    xmp = xmpfile.get_xmp()
    print(xmp)
    keywords_to_add = [x for x in keywords if x not in existing_keywords]
    print('keywords to add')
    print(keywords_to_add)

    def add_keyword(k):
        """ helper func """
        xmp.append_array_item(consts.XMP_NS_DC, u'subject', k)

    _ = [add_keyword(x) for x in keywords_to_add]

    if xmpfile.can_put_xmp(xmp):
        xmpfile.put_xmp(xmp)

    else:
        xmpfile.close_file()
        raise Exception('Cannot write xmp to ' + imgpath)

    xmpfile.close_file()
    return 0
Example #17
0
def file_to_meta(fn):
    xmp_files = XMPFiles()

    with open(fn, 'r') as fp:
        header = fp.readline()
    if "xpacket" in header:
        # the file is already in a compatible format for the XMP parser.
        xmp_files.open_file(fn, open_read=True)
        xmp_obj = xmp_files.get_xmp()
    else:
        # need to wrap the file with a header and footer that allows
        # the XMP parser to parse the file into a dict.
        # we will only transform the data in a temporary file, leaving
        # the original file untouched.
        with NamedTemporaryFile(mode='w', delete=False) as fp,\
                open(fn, 'r') as raw_fp:
            temp_fn = fp.name
            fp.write(XMP_XPACKET_HEADER + "\n")
            for line in raw_fp:
                fp.write("{line}\n".format(line=line))
            fp.write(XMP_XPACKET_FOOTER + "\n")
        xmp_files.open_file(temp_fn, open_read=True, open_onlyxmp=True)
        xmp_obj = xmp_files.get_xmp()
        os.remove(temp_fn)

    return xmp_obj
Example #18
0
def recuperar_informacion(self, img_name, tag):
    xmpfile = XMPFiles(file_path=img_name)
    xmp = xmpfile.get_xmp()

    tipo = Image.open(img_name).format      # Obtenemos el tipo de la imagen.

    if tipo == 'JPEG':                      # Si la imagen es JPEG, obtenemos el contenido almacenado en los campos consts.XMP_NS_JPEG
        info = xmp.get_property(consts.XMP_NS_JPEG, tag)

    else:                                   # Si la imagen es PNG, obtenemos el contenido almacenado en los campos consts.XMP_NS_PNG
        info = xmp.get_property(consts.XMP_NS_PNG, tag)

    xmpfile.close_file()

    return info
    def test_repr(self):
        """Test __repr__ and __str__ on XMPFiles objects."""
        xmpf = XMPFiles()
        self.assertEqual(str(xmpf), 'XMPFiles()')
        self.assertEqual(repr(xmpf), 'XMPFiles()')

        # If the XMPFiles object has a file associated with it, then use a
        # regular expression to match the output.
        filename = pkg.resource_filename(__name__, "samples/BlueSquare.jpg")
        xmpf.open_file(file_path=filename)
        actual_value = str(xmpf)

        regex = re.compile(r"""XMPFiles\(file_path=""", re.VERBOSE)
        self.assertIsNotNone(regex.match(actual_value))
        self.assertTrue(actual_value.endswith("BlueSquare.jpg')"))
    def test_repr(self):
        """Test __repr__ and __str__ on XMPFiles objects."""
        xmpf = XMPFiles()
        self.assertEqual(str(xmpf), 'XMPFiles()')
        self.assertEqual(repr(xmpf), 'XMPFiles()')

        # If the XMPFiles object has a file associated with it, then use a
        # regular expression to match the output.
        filename = pkg.resource_filename(__name__, "samples/BlueSquare.jpg")
        xmpf.open_file(file_path=filename)
        actual_value = str(xmpf)

        regex = re.compile(r"""XMPFiles\(file_path=""", re.VERBOSE)
        self.assertIsNotNone(regex.match(actual_value))
        self.assertTrue(actual_value.endswith("BlueSquare.jpg')"))
Example #21
0
 def test_get_xmp(self):
     for flg in open_flags:
         kwargs = { flg: True }
         for filename, fmt in zip(self.samplefiles, self.formats):
             # See test_exempi_error()
             if not self.flg_fmt_combi(flg, fmt):
                 xmpfile = XMPFiles( file_path=filename, **kwargs )
                 try:
                     xmp = xmpfile.get_xmp()
                     self.assertTrue(isinstance(xmp, XMPMeta),
                                     "Not an XMPMeta object" )
                 except XMPError:
                     print(filename)
                     print(flg)
                     print(fmt)
                 xmpfile.close_file()
 def test_get_xmp(self):
     for flg in open_flags:
         kwargs = {flg: True}
         for filename, fmt in zip(self.samplefiles, self.formats):
             # See test_exempi_error()
             if not self.flg_fmt_combi(flg, fmt):
                 xmpfile = XMPFiles(file_path=filename, **kwargs)
                 try:
                     xmp = xmpfile.get_xmp()
                     self.assertTrue(isinstance(xmp, XMPMeta),
                                     "Not an XMPMeta object")
                 except XMPError:
                     print(filename)
                     print(flg)
                     print(fmt)
                 xmpfile.close_file()
 def test_exempi_bad_combinations(self):
     """
     Verify bad combinations of formats and open flags.
     """
     # Certain combinations of formats and open flags will raise an XMPError
     # when you try to open the XMP
     for flg in open_flags:
         kwargs = {flg: True}
         for filename, fmt in zip(self.samplefiles, self.formats):
             if not self.flg_fmt_combi(flg, fmt):
                 xmpfile = XMPFiles()
                 xmpfile.open_file(filename, **kwargs)
                 xmpfile.get_xmp()
             else:
                 xmpfile = XMPFiles()
                 with self.assertRaises(XMPError):
                     xmpfile.open_file(filename, **kwargs)
Example #24
0
    def test_open_file_with_options(self):
        """Try all open options"""
        for flg in open_flags:
            kwargs = { flg: True }

            for filename in self.samplefiles:
                if flg == 'open_usesmarthandler':
                    # We know these are problematic.
                    suffices = ('.ai', '.pdf', '.xmp')
                    if filename.lower().endswith(suffices):
                        continue
                if flg == 'open_limitscanning':
                    # We know these are problematic.
                    suffices = ('.pdf', '.xmp')
                    if filename.lower().endswith(suffices):
                        continue
                xmpfile = XMPFiles()
                xmpfile.open_file(filename, **kwargs)
    def test_open_file_with_options(self):
        """Try all open options"""
        for flg in open_flags:
            kwargs = {flg: True}

            for filename in self.samplefiles:
                if flg == 'open_usesmarthandler':
                    # We know these are problematic.
                    suffices = ('.ai', '.pdf', '.xmp')
                    if filename.lower().endswith(suffices):
                        continue
                if flg == 'open_limitscanning':
                    # We know these are problematic.
                    suffices = ('.pdf', '.xmp')
                    if filename.lower().endswith(suffices):
                        continue
                xmpfile = XMPFiles()
                xmpfile.open_file(filename, **kwargs)
    def test_text_property_450_file(self):
        files = ["fixtures/BlueSquare450.xmp",
                 "fixtures/BlueSquare450.tif"]
        options = ['open_nooption',        'open_read',
                   'open_forupdate',       'open_onlyxmp',
                   'open_cachetnail',      'open_strictly',
                   'open_usesmarthandler', 'open_usepacketscanning',
                   'open_limitscanning',]
        for f in files:
            for o in options:
                try:
                    xmpfile = XMPFiles( file_path=f, ** { o : True } )
                    xmp_data = xmpfile.get_xmp()
                    headline = xmp_data.get_property(NS_PHOTOSHOP, 'Headline')

                    self.assertEqual(headline[-5:], "=END=")
                    self.assertTrue(len(headline) > 450,
                                    "Not all text was extracted."  )
                except ((IOError, XMPError)):
                    pass
Example #27
0
def embed_xmp(filename, dt):
    t = calendar.timegm(dt.timetuple())
    os.utime(filename, (t, t))
    os.system('SetFile -d "%s" "%s"' % (
        dt.strftime("%m/%d/%Y 00:00:00"),
        filename.encode('utf-8'),
    ))

    xmpfile = XMPFiles(file_path=filename, open_forupdate=True)
    xmp = xmpfile.get_xmp()

    xmp.set_property(consts.XMP_NS_XMP, "CreateDate", dt.isoformat())

    if xmpfile.can_put_xmp(xmp):
        xmpfile.put_xmp(xmp)
        xmpfile.close_file()
        return True
    else:
        xmpfile.close()
        return False
Example #28
0
def compressToJP2(fileIn, fileOut, parameters, extractMetadataFlag):
    """
    Compress image to JP2
    if extractMetadataFlag is True, metadata is extractede from fileIn, 
    which is subsequently written to an XMP sidecar file and then
    embedded in an XML box (as per KB specs). However, by default Kakadu
    already does the metadata extraction natively, but it uses the uuid box!
    """

    # Add command-line arguments
    args = [kdu_compress]
    args.append("-i")
    args.append(fileIn)
    args.append("-o")
    args.append(fileOut)

    for p in parameters:
        args.append(p)

    if extractMetadataFlag and sys.platform != "win32":
        # Metadata extraction and embedding. Skipped if OS is Windows,
        # because Windows does not support LibXMP!

        # Extract metadata from fileIn to XMP
        xmpData = str(XMPFiles(file_path=fileIn).get_xmp())
        # Prepend xmpData with string "xml "
        xmpData = "xml " + xmpData

        # Write xmpData to temporary file
        xmpFNameTemp = str(uuid.uuid1())
        with io.open(xmpFNameTemp, "w", encoding="utf-8") as fXMP:
            fXMP.write(xmpData)
        fXMP.close()

        args.append("-jp2_box")
        args.append(xmpFNameTemp)

    # Command line as string (used for logging purposes only)
    cmdStr = " ".join(args)

    status, out, err = launchSubProcess(args)

    # Main results to dictionary
    dictOut = {}
    dictOut["cmdStr"] = cmdStr
    dictOut["status"] = status
    dictOut["stdout"] = out
    dictOut["stderr"] = err

    if extractMetadataFlag:
        os.remove(xmpFNameTemp)

    return dictOut
Example #29
0
def main():
    options = getoptions()
    logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
    logger = logging.getLogger()
    vars = {
        'UsePanoramaViewer': 'True',
        'ProjectionType': 'equirectangular',
    }
    for vstr in options.vars:
        for elem in vstr.split(','):
            key, value = elem.split('=')
            if key in namespace:
                vars[key] = value
            else:
                logger.warn("Key {0} is invalid.".format(key))
    for filename in options.filenames:
        logger.info("Filename: {0}".format(filename))
        xmpfile = XMPFiles(file_path=filename, open_forupdate=True)
        xmp = xmpfile.get_xmp()
        try:
            assert xmp is not None
        except AssertionError:
            logger.warn("Metadata is not available for {0}.".format(filename))
            continue
        xmp.register_namespace(GPano, 'GPano')

        width, height = imagesize.get(filename)
        width = "{0}".format(width)
        height = "{0}".format(height)
        # width  = xmp.get_property(consts.XMP_NS_EXIF, 'PixelXDimension' )
        # height = xmp.get_property(consts.XMP_NS_EXIF, 'PixelYDimension' )
        # assert width  != '', "No width read."
        # assert height != '', "No height read."
        for name in namespace:
            if name in vars:
                if xmp.does_property_exist(GPano, name):
                    logger.info("Overwrite: {0} {1}->{2}".format(
                        name, xmp.get_property(GPano, name), vars[name]))
                else:
                    logger.info("Set: {0} {1}".format(name, vars[name]))
                xmp.set_property(GPano, name, vars[name])
            elif name in [
                    'FullPanoWidthPixels', 'CroppedAreaImageWidthPixels'
            ]:
                if not xmp.does_property_exist(GPano, name):
                    logger.info("Set default: {0} {1}".format(name, width))
                    xmp.set_property(GPano, name, width)
            elif name in [
                    'FullPanoHeightPixels', 'CroppedAreaImageHeightPixels'
            ]:
                if not xmp.does_property_exist(GPano, name):
                    logger.info("Set default: {0} {1}".format(name, height))
                    xmp.set_property(GPano, name, height)
        xmpfile.put_xmp(xmp)
        xmpfile.close_file()
Example #30
0
    def plot_file(self,date,p,var,dirout):
        '''
        Red line is reference (biofloat, mooring or vessel)
        Blue line is model
        '''
        floatname = p.name()
        filename =dirout+"/"+date.strftime('%Y%m%d') +"_"+floatname+"_"+var+ ".png"
        pl.figure()
        pl.plot(self.Model,self.Depth,label="Model")
        pl.plot(self.Ref,self.Depth,label="Float")
        pl.legend(loc='upper right')
        pl.ylabel("depth")
        figtitle = var+" date="+date.strftime('%Y/%m/%d')+" float="+floatname
        pl.title(figtitle)
        pl.gca().invert_yaxis()
        pl.savefig(filename)
        pl.close()
        import libxmp
        import  libxmp.utils
        from libxmp import XMPFiles, consts
        xmpfile = XMPFiles( file_path=filename, open_forupdate=True )
        xmp = xmpfile.get_xmp()

        if xmp is None:
            xmp = libxmp.XMPMeta()

        xmp.set_property(consts.XMP_NS_DC, 'float', floatname )
        xmp.set_property(consts.XMP_NS_DC, 'date', date.strftime('%Y%m%d') )
        xmp.set_property(consts.XMP_NS_DC, 'hour', date.strftime('%H:%M:%S') )
        xmp.set_property(consts.XMP_NS_DC, 'var', var )
        xmp.set_property(consts.XMP_NS_DC, 'position.lat',str(p.lat)+"N")
        xmp.set_property(consts.XMP_NS_DC, 'position.lon',str(p.lon)+"E")
        xmpfile.put_xmp(xmp)
        xmpfile.close_file()
Example #31
0
def remove_XMPMeta(file):

    xmpfile = XMPFiles(file_path=file, open_forupdate=True)
    xmp = xmpfile.get_xmp()
    xmp.set_property(consts.XMP_NS_PDF, 'pdf:Producer', 'Document Converter')
    xmp.set_property(consts.XMP_NS_XMP, 'xmp:CreatorTool', 'Document Converter')
    xmp.set_property(consts.XMP_NS_XMP_MM , 'xmpMM:DocumentID', '')

    xmp.delete_property(consts.XMP_NS_DC, 'dc:format')
    xmp.delete_property(consts.XMP_NS_DC, 'dc:title')
    xmp.delete_property(consts.XMP_NS_DC, 'dc:creator')
    xmp.delete_property(consts.XMP_NS_DC, 'dc:description')
    try:
        xmpfile.put_xmp(xmp)
    except:
        xmpfile.close_file()
        return
    xmpfile.close_file()
Example #32
0
 def test_descriptive_metadata_from_xmp(self):
     path = join('tests', 'data', 'src', 'IMG_4107_XMP.png')
     xmp = XMPFiles(file_path=path).get_xmp()
     m = DescriptiveMetadata(xmp=xmp)
     assert_equal(m.titles[0].value, 'Moontown Cotton')
     assert_equal(m.agents[0].names[0].full_name, 'Tom Elliott')
     assert_equal(
         m.descriptions[0].value,
         'A picture of cotton alongside a gravel road in Moontown, '
         'Alabama.')
     assert_equal(
         [k.value for k in sorted(m.keywords, key=lambda k: k.sort_val)],
         ['Alabama', 'cotton'])
Example #33
0
 def _import_original(self, path: str):
     d = self.components['original'] = {}
     d['accession_path'] = realpath(expanduser(expandvars(normpath(path))))
     d['filename'] = basename(d['accession_path'])
     fn, ext = splitext(d['filename'])
     target_path = join(self.path, 'data', d['filename'])
     shutil.copy2(d['accession_path'], target_path)
     with ExifTool() as et:
         meta = et.get_metadata(target_path)
     pprint(meta)
     xmp = XMPFiles(file_path=target_path).get_xmp()
     pprint(xmp)
     self._update(manifests=True)
Example #34
0
def check_xmp_writable(file):
    # get xmp from file
    xmpfile = XMPFiles(file_path=file, open_forupdate=True)
    xmp = xmpfile.get_xmp()

    try:
        #if you can write new xmp
        if xmpfile.can_put_xmp(xmp) == True:
            xmpfile.close_file()
            return True
        else:
            xmpfile.close_file()
            return False
    except Exception as e:
        print("Error: " + str(e))
Example #35
0
 def get_tags_from_xmp(self, file_path):
     xmpfile = XMPFiles(file_path=file_path, open_forupdate=True)
     xmp = xmpfile.get_xmp()
     property_was_removed = False
     for index in range(5, 0, -1):
         property_was_removed |= self.remove_geo_xmp_property(
             xmp, consts.XMP_NS_DC, 'subject[%s]' % (index))
     if property_was_removed:
         print('Updating %s' % file_path)
         xmpfile.put_xmp(xmp)
     xmpfile.close_file()
     return
Example #36
0
def ocultar_informacion(self, img_name, tag, content):
    xmpfile = XMPFiles(file_path=img_name, open_forupdate=True)

    tipo = Image.open(img_name).format      # Obtenemos el tipo de la imagen.
        
    if tipo == 'JPEG':                      # Si la imagen es JPEG, almacenamos el contenido en los campos consts.XMP_NS_JPEG
        xmp = xmpfile.get_xmp()
        xmp.set_property(consts.XMP_NS_JPEG, tag, content)
        
    else:                                   # Si la imagen es PNG
        if xmpfile.get_xmp() is None:       # Comprobamos si tiene campos XMP, sino, creamos uno vacío.
            xmp = XMPMeta()
        else:                               # Si tiene campos XMP, los cogemos.
            xmp = xmpfile.get_xmp()
        
        xmp.set_property(consts.XMP_NS_PNG, tag, content)   # Almacenamos el contenido en los campos consts.XMP_NS_PNG
        
    xmpfile.put_xmp(xmp)
    xmpfile.close_file()
def internal_to_sidecar():

    if os.path.isfile(fileEdit.text()) == False:
        editLabel.setText ("Please enter a filepath into the file field")
        return

    filename = fileEdit.text()
    print(filename)
    
    xmpfile = XMPFiles( file_path=filename, open_forupdate=False)
    xmp = xmpfile.get_xmp()
    
    # Create external file and write to it
    handle = open (os.path.basename(filename) + '.xml', 'w')
    handle.write(str(xmp))
    handle.close()
    
    xmpfile.close_file()

    fulltextEdit.setText (str(xmp))
    editLabel.setText ("Loaded " + filename + " and created sidecar file at " + filename + ".xml")

    loadxmp_to_editor()
    def test_cannot_inject_xmp_info_pdf(self):
        """Verify behavior of not being able to inject XMP into barren PDF"""
        # See issue 40
        srcfile = pkg.resource_filename(__name__,
                                        os.path.join("fixtures", "zeros.pdf"))

        with tempfile.NamedTemporaryFile() as tfile:
            shutil.copyfile(srcfile, tfile.name)

            xmpf = XMPFiles()
            xmpf.open_file(tfile.name, open_forupdate=True)
            xmp = XMPMeta()
            xmp.set_property(NS_PHOTOSHOP, "ICCProfile", "foo")
            with self.assertRaises(XMPError):
                xmpf.put_xmp(xmp)
            xmpf.close_file()
    def test_non_ascii_filename(self):
        """
        repr must not fail on files with non-ascii characters

        See issue 36
        """
        # Rename one of the test files to use non-ascii characters.
        relpath = os.path.join('samples', 'BlueSquare.tif')
        srcfile = pkg.resource_filename(__name__, relpath)
        tdir = tempfile.mkdtemp()
        destdir = os.path.join(tdir, u"éà*çc! teeest!!")
        os.makedirs(destdir)

        with tempfile.NamedTemporaryFile(dir=destdir,
                                         prefix="image",
                                         suffix=".tif") as tfile:
            with open(srcfile, 'rb') as srcfile:
                tfile.write(srcfile.read())
                tfile.seek(0)
            xf = XMPFiles()
            xf.open_file(file_path=tfile.name)

            # This was the point of failure in python2
            actual = repr(xf)

            self.assertTrue(actual.startswith("XMPFiles(file_path="))

            if sys.hexversion < 0x03000000:
                # The directory path (up to the non-ascii portion) should be
                # in the output.
                self.assertIn(tdir, actual)
            else:
                # The complete directory path should be present in the repr-ed
                # string under python3.
                self.assertIn(destdir, actual)

        shutil.rmtree(tdir)
    def test_non_ascii_filename(self):
        """
        repr must not fail on files with non-ascii characters

        See issue 36
        """
        # Rename one of the test files to use non-ascii characters.
        relpath = os.path.join('samples', 'BlueSquare.tif')
        srcfile = pkg.resource_filename(__name__, relpath)
        tdir = tempfile.mkdtemp()
        destdir = os.path.join(tdir, u"éà*çc! teeest!!")
        os.makedirs(destdir)

        with tempfile.NamedTemporaryFile(dir=destdir,
                                         prefix="image",
                                         suffix=".tif") as tfile:
            with open(srcfile, 'rb') as srcfile:
                tfile.write(srcfile.read())
                tfile.seek(0)
            xf = XMPFiles()
            xf.open_file(file_path=tfile.name)

            # This was the point of failure in python2
            actual = repr(xf)

            self.assertTrue(actual.startswith("XMPFiles(file_path="))

            if sys.hexversion < 0x03000000:
                # The directory path (up to the non-ascii portion) should be
                # in the output.
                self.assertIn(tdir, actual)
            else:
                # The complete directory path should be present in the repr-ed
                # string under python3.
                self.assertIn(destdir, actual)

        shutil.rmtree(tdir)
Example #41
0
def check_inspo_xmp(file):

    #get xmp from file
    xmpfile = XMPFiles(file_path=file, open_forupdate=True)
    xmp = xmpfile.get_xmp()

    #check to see
    if xmp.does_property_exist(INSPO_URI, 'id') == True:
        xmpfile.close_file()
        return True
    else:
        xmpfile.close_file()
        return False
Example #42
0
 def test_exempi_bad_combinations(self):
     """
     Verify bad combinations of formats and open flags.
     """
     # Certain combinations of formats and open flags will raise an XMPError
     # when you try to open the XMP
     for flg in open_flags:
         kwargs = { flg: True }
         for filename, fmt in zip(self.samplefiles, self.formats):
             if not self.flg_fmt_combi(flg, fmt):
                 xmpfile = XMPFiles()
                 xmpfile.open_file( filename, **kwargs )
                 xmpfile.get_xmp()
             else:
                 xmpfile = XMPFiles()
                 with self.assertRaises(XMPError):
                     xmpfile.open_file( filename, **kwargs )
 def test_can_put_xmp(self):
     for flg in open_flags:
         kwargs = {flg: True}
         for filename, fmt in zip(self.samplefiles, self.formats):
             # See test_exempi_error()
             if (((not self.flg_fmt_combi(flg, fmt))
                  and (not self.exempi_problem(flg, fmt)))):
                 xmpfile = XMPFiles()
                 xmpfile.open_file(filename, **kwargs)
                 xmp = xmpfile.get_xmp()
                 if flg == 'open_forupdate':
                     self.assertTrue(xmpfile.can_put_xmp(xmp))
                 else:
                     self.assertFalse(xmpfile.can_put_xmp(xmp))
    def exempi_error(self):
        """
        Test case that exposes an Exempi bug.

        Seems like xmp_files_can_put_xmp in exempi is missing a try/catch block.

        So loading a sidecar file and call can_put_xmp will kill python
        interpreter since a C++ exception is thrown.
        """
        filename = pkg.resource_filename(__name__, "samples/sig05-002a.xmp")
        xmpfile = XMPFiles()
        xmpfile.open_file(filename, open_forupdate=True)
        xmp = xmpfile.get_xmp()
        xmpfile.can_put_xmp(xmp)
    def test_cannot_inject_xmp_info_pdf(self):
        """Verify behavior of not being able to inject XMP into barren PDF"""
        # See issue 40
        srcfile = pkg.resource_filename(__name__, os.path.join("fixtures",
                                                               "zeros.pdf"))

        with tempfile.NamedTemporaryFile() as tfile:
            shutil.copyfile(srcfile, tfile.name)

            xmpf = XMPFiles()
            xmpf.open_file(tfile.name, open_forupdate=True)
            xmp = XMPMeta()
            xmp.set_property(NS_PHOTOSHOP, "ICCProfile", "foo")
            with self.assertRaises(XMPError):
                xmpf.put_xmp(xmp)
            xmpf.close_file()
Example #46
0
def embed_xmp(filename, dt):
    t = calendar.timegm(dt.timetuple())
    os.utime(filename, (t, t))
    os.system('SetFile -d "%s" "%s"' % (
        dt.strftime("%m/%d/%Y 00:00:00"),
        filename.encode('utf-8'),
    ))

    xmpfile = XMPFiles(file_path=filename, open_forupdate=True)
    xmp = xmpfile.get_xmp()

    xmp.set_property(
        consts.XMP_NS_XMP,
        "CreateDate",
        dt.isoformat()
    )

    if xmpfile.can_put_xmp(xmp):
        xmpfile.put_xmp(xmp)
        xmpfile.close_file()
        return True
    else:
        xmpfile.close()
        return False
    def exempi_error(self):
        """
        Test case that exposes an Exempi bug.

        Seems like xmp_files_can_put_xmp in exempi is missing a try/catch block.

        So loading a sidecar file and call can_put_xmp will kill python
        interpreter since a C++ exception is thrown.
        """
        filename = pkg.resource_filename(__name__, "samples/sig05-002a.xmp")
        xmpfile = XMPFiles()
        xmpfile.open_file(filename, open_forupdate = True )
        xmp = xmpfile.get_xmp()
        xmpfile.can_put_xmp( xmp )
Example #48
0
 def test_can_put_xmp(self):
     for flg in open_flags:
         kwargs = { flg: True }
         for filename, fmt in zip(self.samplefiles, self.formats):
             # See test_exempi_error()
             if (((not self.flg_fmt_combi(flg, fmt)) and
                  (not self.exempi_problem(flg, fmt)))):
                 xmpfile = XMPFiles()
                 xmpfile.open_file( filename, **kwargs )
                 xmp = xmpfile.get_xmp()
                 if flg == 'open_forupdate':
                     self.assertTrue( xmpfile.can_put_xmp( xmp ) )
                 else:
                     self.assertFalse( xmpfile.can_put_xmp( xmp ) )
Example #49
0
def tag_images(docker_image):
    subprocess.call(['mkdir', '-p', 'figures'])
    subprocess.call("docker run --rm -v {}/figures:/figures "
    "{} make figures".format(os.getcwd(), docker_image), shell=True)

    figures = glob.glob('{}/figures/*.png'.format(os.getcwd()))
    for filename in figures:
        xmpfile = XMPFiles(file_path=filename, open_forupdate=True)
        xmp = xmpfile.get_xmp()
        if xmp == None:
            xmp = XMPMeta()
        xmp.append_array_item(libxmp.consts.XMP_NS_DC, 'creator', '{{ "im2sim" : "{}" }}'.format(docker_image), {'prop_array_is_ordered': True, 'prop_value_is_array': True})
        xmpfile.put_xmp(xmp)
        xmpfile.close_file()

    return None
Example #50
0
File: views.py Project: crs4/ACTIVE
 def get(self, request, pk, format=None):
     try:
         # retrieve the item from the file system
         item = Item.objects.get(pk=pk)
         item_path = os.path.join(settings.MEDIA_ROOT, str(item.file))
         # extract the XMP metadata
         xmpfile = XMPFiles()
         xmpfile.open_file(item_path)
         metadata = xmpfile.get_xmp()
         xmpfile.close_file()
         # save a new XMPMetadata object with the association
         obj = XMPMetadata()
         obj.item = item
         obj.metadata = metadata
         obj.save()
         # return the extracted metadata
         serializer = XMPMetadataSerializer(obj)
         return Response(serializer.data)
     except Exception as e:
         print e
         return Response(status=status.HTTP_404_NOT_FOUND)
 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()
    def test_jpeg(self):
        """Create XMP from scratch to store in a jpeg."""

        srcfile = pkg_resources.resource_filename(__name__,
                                                  "samples/BlueSquare.jpg")
        with tempfile.NamedTemporaryFile(suffix='.tif', mode='wb') as tfile:

            # Do some surgery on the file, remove existing xmp.
            # The APP1 marker segment in question starts at byte 2156, has
            # length of 4813
            with open(srcfile, 'rb') as infptr:

                # Write the SOI marker
                tfile.write(infptr.read(2))

                # Skip over ALL the APP0, APP1 segments.
                infptr.seek(21619)
                tfile.write(infptr.read())
                tfile.flush()

            xmpf = XMPFiles()
            xmpf.open_file(file_path=tfile.name, open_forupdate=True)
            xmp = xmpf.get_xmp()

            xmp.set_property(NS_DC, "Title", u'Stürm und Drang')

            # Construct the properties that would have been filled in had the
            # APP0 segment been left in place.
            xmp.set_property(NS_TIFF, "Orientation", "1")
            xmp.set_property(NS_TIFF, "XResolution", "720000/10000")
            xmp.set_property(NS_TIFF, "YResolution", "720000/10000")
            xmp.set_property(NS_TIFF, "ResolutionUnit", "2")
            xmpf.put_xmp(xmp)
            xmpf.close_file()

            xmpf = XMPFiles()
            xmpf.open_file(file_path=tfile.name)
            xmp = xmpf.get_xmp()

            prop = xmp.get_property(NS_DC, "Title")
            self.assertEqual(prop, u'Stürm und Drang')

            prop = xmp.get_property(NS_TIFF, "Orientation")
            self.assertEqual(prop, "1")

            prop = xmp.get_property(NS_TIFF, "XResolution")
            self.assertEqual(prop, "720000/10000")

            prop = xmp.get_property(NS_TIFF, "YResolution")
            self.assertEqual(prop, "720000/10000")

            prop = xmp.get_property(NS_TIFF, "ResolutionUnit")
            self.assertEqual(prop, "2")

            xmpf.close_file()
    def test_sturm_und_drang(self):
        """Should be able to write a property which includes umlauts."""
        srcfile = pkg_resources.resource_filename(__name__,
                                                  "fixtures/zeros.tif")
        with tempfile.NamedTemporaryFile(suffix='.tif') as tfile:
            shutil.copyfile(srcfile, tfile.name)

            expected_value = u'Stürm und Drang'

            xmpf = XMPFiles()
            xmpf.open_file(file_path=tfile.name, open_forupdate=True)
            xmp = xmpf.get_xmp()
            xmp.set_property(NS_DC, "Title", expected_value)
            xmpf.put_xmp(xmp)
            xmpf.close_file()

            xmpf = XMPFiles()
            xmpf.open_file(file_path=tfile.name)
            xmp = xmpf.get_xmp()
            actual_value = xmp.get_property(NS_DC, "Title")
            xmpf.close_file()

            self.assertEqual(actual_value, expected_value)
    def test_tiff(self):
        """Write to a TIFF that does not already have the XMP tag."""
        srcfile = pkg_resources.resource_filename(__name__,
                                                  "fixtures/zeros.tif")
        with tempfile.NamedTemporaryFile(suffix='.tif') as tfile:
            shutil.copyfile(srcfile, tfile.name)

            xmpf = XMPFiles()
            xmpf.open_file(file_path=tfile.name, open_forupdate=True)

            # Since it's a TIFF, it already has everything possible from the
            # TIFF IFD filled in into the TIFF namespace.
            xmp = xmpf.get_xmp()
            xmp.set_property(NS_DC, "rights", "no one in particular")
            xmpf.put_xmp(xmp)
            xmpf.close_file()

            xmpf.open_file(file_path=tfile.name)
            xmp = xmpf.get_xmp()
            xmpf.close_file()

            # TODO:  explain why this happened.
            prop = xmp.get_property(NS_DC, "rights")
            prop2 = xmp.get_localized_text(NS_DC, "rights", None, "x-default")
            self.assertEqual(prop2, "no one in particular")
Example #55
0
def parse_meta(filename):
	slide_file = parse_slide(filename)
	if slide_file is None: return None


	with ZipFile(filename, 'r') as z:
		with(z.open(slide_file[0])) as f:
			slide_type = slide_file[1]

			title_string = None
			description_string = None

			if slide_type == ".htm" or slide_type == ".html":
				soup = BeautifulSoup(f.read(), "lxml")

				title = soup.find('meta', {'name':'veeva_title'})
				if title is not None:
					title_string = title.get('content', None)

				description = soup.find('meta', {'name':'veeva_description'})
				if description is not None:
					description_string = description.get('content', None)

			if slide_type == ".pdf":
				doc = PDFDocument()
				parser = PDFParser(f)

				# omfg this is so janky, there needs to be a better library
				parser.set_document(doc)
				doc.set_parser(parser)

				metadata = doc.info
				if len(metadata) > 0:
					latest = metadata[-1]
					try:
						if latest['Title'] != '': title_string = latest['Title']
					except KeyError:
						title_string = None

					try:
						if latest['Subject'] != '': description_string = latest['Subject']
					except KeyError:
						description_string = None

			if slide_type == ".jpg" or slide_type == ".jpeg":
				tmp_file_name = str(uuid.uuid1()) + ".jpg"
				with open(tmp_file_name, 'wb') as tf:
					tf.write(f.read())

				xmpfile = XMPFiles(file_path=tmp_file_name)
				xmp = xmpfile.get_xmp()
				xmpfile.close_file()

				try:
					title_string = xmp.get_localized_text(consts.XMP_NS_DC, 'title', None, 'x-default')
				except XMPError:
					title_string = None

				try:
					description_string = xmp.get_localized_text(consts.XMP_NS_DC, 'description', None, 'x-default')
				except XMPError:
					description_string = None

				os.remove(tmp_file_name)

	return {"filename": filename, "veeva_title": title_string, "veeva_description": description_string}
 def test_does_property_exist(self):
     filename = pkg_resources.resource_filename(__name__,
                                                "fixtures/BlueSquare450.tif")
     xmp = XMPFiles(file_path=filename)
     xmp_data = xmp.get_xmp()
     self.assertTrue( xmp_data.does_property_exist( "http://ns.adobe.com/photoshop/1.0/", 'Headline' ) )
image_height = int(sys.argv[4])
if not os.path.isfile(limage_path):
    print('Left image path ({0}) is not exists.'.format(limage_path))
    exit()
if not os.path.isfile(rimage_path):
    print('Right image path ({0}) is not exists.'.format(rimage_path))
    exit()

# Copy left image file
limage_dir = os.path.split(limage_path)[0]
limage_fname = os.path.splitext(os.path.split(limage_path)[1])[0]
vrimage_path = os.path.join(limage_dir, limage_fname + '.vr.jpg')
shutil.copyfile(limage_path, vrimage_path)

# Load image's xmp
vrimage_file = XMPFiles(file_path=vrimage_path, open_forupdate=True)
lxmp = vrimage_file.get_xmp()
#print(lxmp)

# Google's namespace
XMP_GIMAGE = 'http://ns.google.com/photos/1.0/image/'
XMP_GPANO = 'http://ns.google.com/photos/1.0/panorama/'
XMPMeta.register_namespace(XMP_GIMAGE, 'GImage')
XMPMeta.register_namespace(XMP_GPANO, 'GPano')

# Set GPano properties
lxmp.set_property(XMP_GPANO, 'ProjectionType', 'equirectangular')
lxmp.set_property_int(XMP_GPANO, 'CroppedAreaLeftPixels', image_width/2)
lxmp.set_property_int(XMP_GPANO, 'CroppedAreaTopPixels', 0)
lxmp.set_property_int(XMP_GPANO, 'CroppedAreaImageWidthPixels', image_width)
lxmp.set_property_int(XMP_GPANO, 'CroppedAreaImageHeightPixels', image_height)
Example #58
0
    def test_tiff_smarthandler(self):
        """Verify action of TIFF smarthandler when tag length > 255"""
        # See issue 12
        srcfile = pkg_resources.resource_filename(__name__,
                                                  "fixtures/zeros.tif")
        with tempfile.NamedTemporaryFile(suffix='.tif') as tfile:
            shutil.copyfile(srcfile, tfile.name)

            # Create a tag with 280 chars.
            xmpf = XMPFiles()
            xmpf.open_file(tfile.name, open_forupdate=True)
            xmp = xmpf.get_xmp()
            blurb = "Some really long text blurb "
            xmp.set_property(NS_PHOTOSHOP, 'Headline', blurb * 10)
            xmpf.put_xmp(xmp)
            xmpf.close_file()

            xmpf.open_file(tfile.name, usesmarthandler=True)
            xmp = xmpf.get_xmp()
            prop = xmp.get_property(NS_PHOTOSHOP, "Headline")
            xmpf.close_file()

            self.assertEqual(prop, blurb * 10)
Example #59
0
def readmetadata(fname):
    #read metadata from tiff
    xf = XMPFiles()
    xf.open_file(file)
    xmp = xf.get_xmp()
 def test_get_xmp(self):
     for f in self.samplefiles:
         xmpfile = XMPFiles( file_path=f )
         xmp = xmpfile.get_xmp()
         self.assertTrue( isinstance(xmp, XMPMeta), "Not an XMPMeta object" )
         xmpfile.close_file()