def test_insert_command_builder(monkeypatch):
    DUMMY_EXIV2_PATH = "/usr/bin/exiv2"
    src_file = "/Users/Documents/sample.jp2"
    expected_cmd = ['/usr/bin/exiv2', '-iaX', '/Users/Documents/sample.jp2']

    # Because the location of exiv2 is faked for this example, we need to
    # ignore the checks
    monkeypatch.setattr(os.path, "exists", lambda x: True)

    command_builder = Exiv2CommandBuilder(builder=builders.InsertIPTCCommand(),
                                          program_path=DUMMY_EXIV2_PATH)

    command = command_builder.build_command(src=src_file)

    assert command == expected_cmd
示例#2
0
    def convert_tiff_access_folder2(source: str,
                                    destination,
                                    overwrite_existing=True,
                                    remove_on_success=False):
        logger = logging.getLogger(__name__)
        image_convert_command = ImagemagickCommandBuilder(
            builder=im_cb.Standard())
        metadata_extractor = Exiv2CommandBuilder(exi2_cb.ExtractIPTCCommand())
        metadata_injector = Exiv2CommandBuilder(exi2_cb.InsertIPTCCommand())

        with tempfile.TemporaryDirectory(prefix="convert_") as tmp_folder:
            command_runner = CommandRunner()
            tiffs = list(get_tiffs(source))
            total_files_converted = 0

            for i, tiff in enumerate(tiffs):
                tmp_access_tif = os.path.join(tmp_folder,
                                              os.path.basename(tiff))
                tmp_access_jp2 = os.path.splitext(tmp_access_tif)[0] + ".jp2"
                final_access_jp2 = os.path.join(
                    destination, os.path.basename(tmp_access_jp2))

                if not overwrite_existing:
                    if os.path.exists(final_access_jp2):
                        logger.warning("{} already exists. skipping".format(
                            final_access_jp2))
                        continue

                logger.info("Converting part {} of {}: From {} to {} ".format(
                    i + 1, len(tiffs), os.path.basename(tiff),
                    os.path.basename(final_access_jp2)))

                logger.debug("Copying \"{}\" to temp folder \"{}\"".format(
                    tiff, tmp_folder))

                shutil.copyfile(tiff, tmp_access_tif)

                logger.debug("Using \"{}\" to create \"{}\"".format(
                    tmp_access_tif, tmp_access_jp2))

                im_command = image_convert_command.build_command(
                    tmp_access_tif, tmp_access_jp2)

                try:

                    command_runner.run(im_command)
                    if remove_on_success:
                        # remove the read only attribute first
                        os.chmod(tiff, stat.S_IWRITE)

                        logger.info("Deleting file, \"{}\".".format(tiff))
                        os.remove(tiff)

                except RuntimeError as e:
                    logger.error(str(e))
                    raise
                finally:
                    stdout, stderr = command_runner.get_output()
                    if stdout:
                        logger.info(stdout)
                    if stderr:
                        logger.warning(stderr)

                # HACK: Clean up from Imagemagick because it seems to have a
                # problem with embedded thumbnails
                _cleanup_multiple(os.path.basename(tmp_access_jp2), tmp_folder)

                # Create sidecar metadata files
                logger.debug(
                    "Extracting metadata from \"{}\"".format(tmp_access_tif))

                mde_command = metadata_extractor.build_command(tmp_access_tif)
                try:
                    command_runner.run(mde_command)

                except RuntimeError as e:
                    logger.error(str(e))
                    raise
                finally:
                    stdout, stderr = command_runner.get_output()
                    if stdout:
                        logger.info(stdout)
                    if stderr:
                        logger.warning(stderr)

                # Insert sidecar metadata files into jp2
                logger.debug(
                    "Injecting metadata into \"{}\"".format(tmp_access_jp2))

                mdi_command = metadata_injector.build_command(tmp_access_jp2)
                try:
                    command_runner.run(mdi_command)

                except RuntimeError as e:
                    logger.error(str(e))
                    raise
                finally:
                    stdout, stderr = command_runner.get_output()
                    if stdout:
                        logger.info(stdout)
                    if stderr:
                        logger.warning(stderr)

                logger.debug("Moving \"{}\" into \"{}\"".format(
                    tmp_access_jp2, destination))
                if not os.path.exists(destination):
                    os.makedirs(destination)
                shutil.move(tmp_access_jp2, final_access_jp2)
                total_files_converted += 1
            logger.info("Created {} file(s) in {}.".format(
                total_files_converted, destination))
示例#3
0
def convert_tiff_access_folder(path: str,
                               overwrite_existing=True,
                               remove_on_success=False):
    """
    Converts all tiff files located in that folder into JP2000 .jp2 files and
    migrated all the metadata from the tiff file into the newly produced jp2
    file. All new files are saved in the same place that as their source files.

    Args:
        path: The path to the folder containing tiff files to be converter.
        overwrite_existing: If an existing jp2 file already exists in the same
         folder, overwrite it with a new file.
        remove_on_success: Delete access tiff file afterwards if successfully converted.


    """
    warn("Use convert_tiff_access_folder2 instead", DeprecationWarning)
    logger = logging.getLogger(__name__)
    image_convert_command = ImagemagickCommandBuilder(builder=im_cb.Standard())
    metadata_extractor = Exiv2CommandBuilder(exi2_cb.ExtractIPTCCommand())
    metadata_injector = Exiv2CommandBuilder(exi2_cb.InsertIPTCCommand())

    with tempfile.TemporaryDirectory(prefix="convert_") as tmp_folder:
        command_runner = CommandRunner()
        tiffs = list(get_tiffs(path))
        total_files_converted = 0

        for i, tiff in enumerate(tiffs):
            tmp_access_tif = os.path.join(tmp_folder, os.path.basename(tiff))
            tmp_access_jp2 = os.path.splitext(tmp_access_tif)[0] + ".jp2"
            final_access_jp2 = os.path.join(path,
                                            os.path.basename(tmp_access_jp2))

            if not overwrite_existing:
                if os.path.exists(final_access_jp2):
                    logger.warning(
                        "{} already exists. skipping".format(final_access_jp2))
                    continue

            logger.info("Converting part {} of {}: From {} to {} ".format(
                i + 1, len(tiffs), os.path.basename(tiff),
                os.path.basename(final_access_jp2)))

            logger.debug("Copying \"{}\" to temp folder \"{}\"".format(
                tiff, path))

            shutil.copyfile(tiff, tmp_access_tif)

            logger.debug("Using \"{}\" to create \"{}\"".format(
                tmp_access_tif, tmp_access_jp2))

            im_command = image_convert_command.build_command(
                tmp_access_tif, tmp_access_jp2)

            try:

                command_runner.run(im_command)
                if remove_on_success:
                    # remove the read only attribute first
                    os.chmod(tiff, stat.S_IWRITE)

                    logger.info("Deleting file, \"{}\".".format(tiff))
                    os.remove(tiff)

            except RuntimeError as e:
                logger.error(str(e))
                raise
            finally:
                stdout, stderr = command_runner.get_output()
                if stdout:
                    logger.info(stdout)
                if stderr:
                    logger.warning(stderr)

            # HACK: Clean up from Imagemagick because it seems to have a
            # problem with embedded thumbnails
            _cleanup_multiple(os.path.basename(tmp_access_jp2), tmp_folder)

            # Create sidecar metadata files
            logger.debug(
                "Extracting metadata from \"{}\"".format(tmp_access_tif))

            mde_command = metadata_extractor.build_command(tmp_access_tif)
            try:
                command_runner.run(mde_command)

            except RuntimeError as e:
                logger.error(str(e))
                raise
            finally:
                stdout, stderr = command_runner.get_output()
                if stdout:
                    logger.info(stdout)
                if stderr:
                    logger.warning(stderr)

            # Insert sidecar metadata files into jp2
            logger.debug(
                "Injecting metadata into \"{}\"".format(tmp_access_jp2))

            mdi_command = metadata_injector.build_command(tmp_access_jp2)
            try:
                command_runner.run(mdi_command)

            except RuntimeError as e:
                logger.error(str(e))
                raise
            finally:
                stdout, stderr = command_runner.get_output()
                if stdout:
                    logger.info(stdout)
                if stderr:
                    logger.warning(stderr)

            logger.debug("Moving \"{}\" into \"{}\"".format(
                tmp_access_jp2, path))
            shutil.move(tmp_access_jp2, final_access_jp2)
            total_files_converted += 1
        logger.info("Converted {} file(s) in {}.".format(
            total_files_converted, path))
示例#4
0
    def convert_tiff_access_folder(path: str,
                                   overwrite_existing=True,
                                   remove_on_success=False):
        warn("Use convert_tiff_access_folder instead", DeprecationWarning)
        logger = logging.getLogger(__name__)
        total_files_converted = 0
        image_convert_command = KakaduCommandBuilder(
            builder=kd_cb.HathiPreset())
        metadata_extractor = Exiv2CommandBuilder(exi2_cb.ExtractIPTCCommand())
        metadata_injector = Exiv2CommandBuilder(exi2_cb.InsertIPTCCommand())
        with tempfile.TemporaryDirectory(prefix="convert_") as tmp_folder:
            command_runner = CommandRunner()
            tiffs = list(get_tiffs(path))
            for i, tiff in enumerate(tiffs):
                tmp_access_tif = os.path.join(tmp_folder,
                                              os.path.basename(tiff))
                tmp_access_jp2 = os.path.splitext(tmp_access_tif)[0] + ".jp2"
                final_access_jp2 = os.path.join(
                    path, os.path.basename(tmp_access_jp2))

                if not overwrite_existing:
                    if os.path.exists(final_access_jp2):
                        logger.warning("{} already exists. skipping".format(
                            final_access_jp2))
                        continue

                logger.info("Converting part {} of {}: From {} to {} ".format(
                    i + 1, len(tiffs), os.path.basename(tiff),
                    os.path.basename(final_access_jp2)))

                logger.debug("Copying \"{}\" to temp folder \"{}\"".format(
                    tiff, path))

                shutil.copyfile(tiff, tmp_access_tif)

                logger.debug("Using \"{}\" to create \"{}\"".format(
                    tmp_access_tif, tmp_access_jp2))

                im_command = image_convert_command.build_command(
                    tmp_access_tif, tmp_access_jp2)

                try:
                    command_runner.run(im_command)
                    if remove_on_success:
                        logger.info("Deleting file, \"{}\".".format(tiff))
                        os.remove(tiff)

                except RuntimeError as e:
                    logger.error(str(e))
                    raise
                finally:
                    pass
                    stdout, stderr = command_runner.get_output()
                    if stdout:
                        logger.info(stdout)
                    if stderr:
                        logger.warning(stderr)

                logger.debug(
                    "Extracting metadata from \"{}\"".format(tmp_access_tif))

                mde_command = metadata_extractor.build_command(tmp_access_tif)
                try:
                    command_runner.run(mde_command)

                except RuntimeError as e:
                    logger.error(str(e))
                    raise
                finally:
                    stdout, stderr = command_runner.get_output()
                    if stdout:
                        logger.info(stdout)
                    if stderr:
                        logger.warning(stderr)

                # Insert sidecar metadata files into jp2
                logger.debug(
                    "Injecting metadata into \"{}\"".format(tmp_access_jp2))

                mdi_command = metadata_injector.build_command(tmp_access_jp2)
                try:
                    command_runner.run(mdi_command)

                except RuntimeError as e:
                    logger.error(str(e))
                    raise
                finally:
                    stdout, stderr = command_runner.get_output()
                    if stdout:
                        logger.info(stdout)
                    if stderr:
                        logger.warning(stderr)

                logger.debug("Moving \"{}\" into \"{}\"".format(
                    tmp_access_jp2, path))
                shutil.move(tmp_access_jp2, final_access_jp2)
                total_files_converted += 1
            logger.info("Converted {} file(s) in {}.".format(
                total_files_converted, path))