Beispiel #1
0
def image_metadata_matches(image_filepath1, image_filepath2):
    """
    Extracts xmp files for each file and compares them

    :param image_filepath1:
    :param image_filepath2:
    :return: True if the xmp files match
    """
    with tempfile.NamedTemporaryFile(suffix='.xmp') as xmp_file1_obj:
        with tempfile.NamedTemporaryFile(suffix='.xmp') as xmp_file2_obj:
            conversion.Converter().extract_xmp_to_sidecar_file(
                image_filepath1, xmp_file1_obj.name)
            conversion.Converter().extract_xmp_to_sidecar_file(
                image_filepath2, xmp_file2_obj.name)
            return filecmp.cmp(xmp_file1_obj.name, xmp_file2_obj.name)
Beispiel #2
0
    def __init__(
            self,
            kakadu_base_path=DEFAULT_KAKADU_BASE_PATH,
            jpg_high_quality_value=DEFAULT_JPG_HIGH_QUALITY_VALUE,
            jpg_thumbnail_resize_value=DEFAULT_JPG_THUMBNAIL_RESIZE_VALUE,
            kakadu_compress_options=kakadu.DEFAULT_LOSSLESS_COMPRESS_OPTIONS,
            use_default_filenames=True,
            require_icc_profile_for_greyscale=False,
            require_icc_profile_for_colour=True,
            exiftool_path=DEFAULT_EXIFTOOL_PATH):
        """

        :param kakadu_base_path: the location of the kdu_compress and kdu_expand executables
        :param jpg_high_quality_value: between 0 and 95
        :param jpg_thumbnail_resize_value: between 0 and 1
        :param kakadu_compress_options: options for kdu_compress to create a lossless jp2 file
        :param use_default_filenames: use the filenames specified in this module instead of using the original filename
        :param require_icc_profile_for_greyscale: raise an error if a greyscale image doesn't have an ICC profile.
            Note: bitonal images do not need ICC profiles even if this is true
        :param require_icc_profile_for_colour: raise an error if a colour image does not have an ICC profile
        :param exiftool_path: path to the exiftool executable
        """

        self.jpg_high_quality_value = jpg_high_quality_value
        self.jpg_thumbnail_resize_value = jpg_thumbnail_resize_value
        self.require_icc_profile_for_greyscale = require_icc_profile_for_greyscale
        self.require_icc_profile_for_colour = require_icc_profile_for_colour
        self.use_default_filenames = use_default_filenames
        self.kakadu_compress_options = kakadu_compress_options
        self.converter = conversion.Converter(exiftool_path=exiftool_path)

        self.kakadu = Kakadu(kakadu_base_path=kakadu_base_path)

        self.log = logging.getLogger(__name__)
Beispiel #3
0
 def test_converts_jpg_to_tiff(self):
     with temporary_folder() as output_folder:
         tiff_file = os.path.join(output_folder, 'test.tif')
         conversion.Converter().convert_to_tiff(filepaths.STANDARD_JPG,
                                                tiff_file)
         assert os.path.isfile(tiff_file)
         assert image_files_match(tiff_file,
                                  filepaths.TIF_FROM_STANDARD_JPG)
Beispiel #4
0
    def convert(self):
        if not os.path.isdir(self.output_dir):
            os.mkdir(self.output_dir)

        files_to_process = [
            os.path.join(self.input_dir, f) for f in os.listdir(self.input_dir)
            if self.__filter_fnames(f)
        ]
        invalids = []
        total = len(files_to_process)
        for image in files_to_process:
            filename = os.path.basename(image)
            filename, ext = os.path.splitext(filename)
            input_file = os.path.join(self.input_dir,
                                      "{0}{1}".format(filename, ext))
            output_file = os.path.join(self.output_dir,
                                       "{0}.jp2".format(filename))
            temp_tiff_filepath = None

            if ext == '.jpg':
                # convert jpg to a temp tiff (required by Kakadu)
                converter = conversion.Converter()
                with tempfile.NamedTemporaryFile(
                        prefix='image-processing_', suffix='.tif',
                        delete=False) as temp_tiff_file_obj:
                    temp_tiff_filepath = temp_tiff_file_obj.name
                    converter.convert_to_tiff(input_file, temp_tiff_filepath)
                    input_file = temp_tiff_filepath

            # openjpeg encoder selected
            if self.encoder == 'opj':
                opj = openjpeg.OpenJpeg(openjpeg_base_path=self.bin_path)
                opj.opj_compress(input_file,
                                 output_file,
                                 openjpeg_options=openjpeg.
                                 DEFAULT_LOSSLESS_COMPRESS_OPTIONS)
            # kakadu encoder selected
            elif self.encoder == 'kdu':
                kdu = kakadu.Kakadu(kakadu_base_path=self.bin_path)
                kdu.kdu_compress(
                    input_file,
                    output_file,
                    kakadu_options=kakadu.DEFAULT_LOSSLESS_COMPRESS_OPTIONS)
                #opts = kakadu.DEFAULT_COMPRESS_OPTIONS + kakadu.LOSSY_OPTIONS
                #kdu.kdu_compress(input_file, output_file, kakadu_options=opts)

            if self.validate_jp2:
                if not self.__is_valid_jp2(output_file):
                    invalids.append(output_file)

            # Remove the temp tiff file
            if temp_tiff_filepath is not None:
                os.unlink(temp_tiff_filepath)
                assert not os.path.exists(temp_tiff_filepath)

        self.__report_msg(total, invalids)
Beispiel #5
0
    def test_icc_conversion_catches_16_bit_errors(self):
        with temporary_folder() as output_folder:
            output_file = os.path.join(output_folder, 'output.tif')

            with pytest.raises(exceptions.ImageProcessingError):
                conversion.Converter().convert_icc_profile(
                    filepaths.TIF_16_BIT, output_file,
                    filepaths.SRGB_ICC_PROFILE)

            assert not os.path.isfile(output_file)
Beispiel #6
0
 def test_converts_tif_to_jpeg(self):
     with temporary_folder() as output_folder:
         output_file = os.path.join(output_folder, 'output.jpg')
         conversion.Converter().convert_to_jpg(
             filepaths.STANDARD_TIF,
             output_file,
             resize=None,
             quality=derivative_files_generator.
             DEFAULT_JPG_HIGH_QUALITY_VALUE)
         assert os.path.isfile(output_file)
         assert image_files_match(
             output_file, filepaths.HIGH_QUALITY_JPG_FROM_STANDARD_TIF)
Beispiel #7
0
    def test_icc_conversion(self):
        with temporary_folder() as output_folder:
            output_file = os.path.join(output_folder, 'output.tif')
            conversion.Converter().convert_icc_profile(
                filepaths.STANDARD_TIF, output_file,
                filepaths.SRGB_ICC_PROFILE)
            assert os.path.isfile(output_file)

            with Image.open(filepaths.STANDARD_TIF) as input_pil:
                old_icc = input_pil.info.get('icc_profile')
                f = io.BytesIO(old_icc)
                prf = ImageCms.ImageCmsProfile(f)
                assert prf.profile.profile_description == "Adobe RGB (1998)"

            with Image.open(output_file) as output_pil:
                new_icc = output_pil.info.get('icc_profile')
                f = io.BytesIO(new_icc)
                prf = ImageCms.ImageCmsProfile(f)
                assert prf.profile.profile_description == "sRGB v4 ICC preference perceptual intent beta"