示例#1
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
    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")
示例#3
0
    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")
    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()
示例#5
0
    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()
示例#6
0
def dictonary_write(file, dict):
    # get xmp from file
    xmpfile = XMPFiles(file_path=file, open_forupdate=True)
    xmp = xmpfile.get_xmp()

    if check_xmp_writable(file) == True:  #if xmp can be written to file

        if check_inspo_xmp(file) == False:  #if INSPO xmp does not exist

            # register or update INSPO namespace URI
            xmp.register_namespace(INSPO_URI, "INSPO")

            # for each possible article
            for article in ARTICLES:
                try:
                    value = dict[article]
                    xmp.set_property(INSPO_URI, article, value)
                except:
                    pass

            xmpfile.put_xmp(xmp)
            xmpfile.close_file()
            return

        else:
            xmpfile.close_file()
            print("INSPO DATA ALREADY EXISTS")
            return

    else:
        xmpfile.close_file()
        print("XMP NOT WRITEABLE")
        return
示例#7
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()
示例#8
0
def terminal_write(file):
    # get xmp from file
    xmpfile = XMPFiles(file_path=file, open_forupdate=True)
    xmp = xmpfile.get_xmp()

    if check_xmp_writable(file) == True: #if xmp can be written to file

        if check_inspo_xmp(file) == False: #if INSPO xmp does not exist

            # register or update INSPO namespace URI
            xmp.register_namespace(INSPO_URI, "INSPO")

            # for each possible article
            for article in ARTICLES:

                value = input("What " + article + " are they wearing?: ")  # ask if they are wearing a certain article of clothing

                if value == "":  # if the value is empty then pass
                    pass
                else:  # else set that article of clothing
                    xmp.set_property(INSPO_URI, article, value)

            xmpfile.put_xmp(xmp)
            xmpfile.close_file()

        else:
            xmpfile.close_file()
            print("INSPO DATA ALREADY EXISTS")
            return

    else:
        xmpfile.close_file()
        print("XMP NOT WRITEABLE")
        return
示例#9
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
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()
示例#11
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
示例#12
0
文件: matchup.py 项目: inogs/bit.sea
    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()
示例#13
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_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)
示例#15
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 )
示例#16
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()
示例#17
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()
示例#18
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 )
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
 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)
示例#21
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
示例#22
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
    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)
示例#24
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
    def test_tiff_smarthandler(self):
        """Verify action of TIFF smarthandler when tag length > 255"""
        # See issue 12
        srcfile = pkg.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)
    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_tiff_smarthandler(self):
        """Verify action of TIFF smarthandler when tag length > 255"""
        # See issue 12
        srcfile = pkg.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)
 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))
示例#29
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
示例#30
0
    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 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 )
示例#32
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 ) )
    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_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)
示例#35
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
示例#36
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))
示例#37
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()
示例#38
0
文件: im2sim.py 项目: IanHawke/im2sim
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
 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()
示例#40
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()
示例#41
0
    def mutate(self, info, input):
        abs_path = Path(BASE_DIR, input["path"])
        if not abs_path.exists():
            raise Exception("Image not found")

        if not (isinstance(input["rating"], int)
                and -1 <= input["rating"] <= 5):
            raise Exception("Rating must be an integer from 1 to 5")

        image = {"path": abs_path}
        xmpfile = XMPFiles(file_path=str(abs_path), open_forupdate=True)
        xmp = xmpfile.get_xmp()
        # WISHLIST don't write if rating didn't change
        xmp.set_property(XMP_NS_XMP, "xmp:Rating", str(input["rating"]))
        if not xmpfile.can_put_xmp(xmp):
            raise Exception("Can't write xmp metadata back to image")

        xmpfile.put_xmp(xmp)
        xmpfile.close_file()
        return SetRatingPayload(image=image)
    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
示例#43
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
示例#44
0
文件: views.py 项目: 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)
示例#45
0
def main():
    path = cli()
    proj = path.parts[-1]
    stamp = path.joinpath(proj + "_PPKStamp.csv")

    cols = ["name","lat","lon","elev"]
    st = pd.read_csv(stamp, names=cols, comment='#')
    
    for i in range(len(st)):
        jpg = path.joinpath(st["name"][i] + ".JPG")
        exif = piexif.load(str(jpg))

        xmpfile = XMPFiles(file_path=str(jpg), open_forupdate=True)
        xmp = xmpfile.get_xmp()
        DJIns = xmp.get_namespace_for_prefix("drone-dji")

        xmp.set_property(DJIns, 'AbsoluteAltitude', "+%.4f" % st["elev"][i])
        xmp.set_property(DJIns, 'GpsLatitude', "%.10f" % st["lat"][i])
        xmp.set_property(DJIns, 'GpsLongitude', "%.10f" % st["lon"][i])
        #xmp.set_property(DJIns, 'GpsLongtitude', "+%.10f" % st["lon"][i])

        xmpfile.put_xmp(xmp)
        xmpfile.close_file()

        lat = abs(st["lat"][i])
        latd = int(lat)
        latm = int((lat-latd)*60)
        lats = (lat-latd-latm/60)*3600

        lon = abs(st["lon"][i])
        lond = int(lon)
        lonm = int((lon-lond)*60)
        lons = (lon-lond-lonm/60)*3600

        exif["GPS"][2] = ((latd, 1), (latm, 1), (int(lats*1000000), 1000000))
        exif["GPS"][4] = ((lond, 1), (lonm, 1), (int(lons*1000000), 1000000))
        exif["GPS"][6] = (int(st["elev"][i]*1000), 1000)

        exifBytes = piexif.dump(exif)
        piexif.insert(exifBytes, str(jpg))
    def test_can_inject_xmp_info_png(self):
        """Verify behavior of being able to inject XMP into barren PNG"""
        # See issue 40
        srcfile = pkg.resource_filename(__name__, os.path.join("fixtures",
                                                               "zeros.png"))

        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")
            xmpf.put_xmp(xmp)
            xmpf.close_file()

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

            self.assertEqual(prop, "foo")
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()
示例#48
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 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_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()
示例#51
0
文件: ctls.py 项目: gacomm/VELVEEVA
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}
from libxmp import XMPFiles
import uuid

start = time.time()

files = glob("*IMAGE*/*.tif*")

nfiles = len(files)

for file in files:
    # name for jp2 image
    fjp2 = "{}.jp2".format(file.split(".tif")[0])
    # read metadata from tiff
    xf = XMPFiles()
    xf.open_file(file)
    xmp = xf.get_xmp()
    # read bitmap from tiff
    image = io.imread(file, plugin="tifffile")
    # write bitmap to jp2 image
    jp2 = glymur.Jp2k(fjp2, "wb")
    jp2[:] = image
    # append metadata in uuid box
    xmp_uuid = uuid.UUID("be7acfcb-97a9-42e8-9c71-999491e3afac")
    box = glymur.jp2box.UUIDBox(xmp_uuid, str(xmp))
    jp2.append(box)
    # write second resolution as thumbnail
    jt = glymur.Jp2k("thumb" + fjp2, "wb")
    jt[:] = jp2.read(rlevel=2)

end = time.time()
print "Time:{:.0e}s Files:{} Time/Files:{:.1e}s/file".format(end - start, nfiles, (end - start) / nfiles)
 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()
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)
lxmp.set_property_int(XMP_GPANO, 'FullPanoWidthPixels', image_width*2)
示例#55
0
def readmetadata(fname):
    #read metadata from tiff
    xf = XMPFiles()
    xf.open_file(file)
    xmp = xf.get_xmp()