示例#1
0
    def test_tiff_depths(self):
        """
        Tests the conversions between all supported dtypes in tiff.
        """
        tif_file = tempfile.NamedTemporaryFile(suffix='.tif',
                                               prefix="ipol_",
                                               delete=True)
        for src_depth in ('8i', '16i', '32i', '16f', '32f'):
            for dst_depth in (
                    '8i', '16i',
                    '32i'):  # float as a destination is not strictly equal
                data = self.data_matrix(src_depth)
                im = Image(data=data)
                im.convert_depth(dst_depth)
                im.write(tif_file.name)

                im = Image(src=tif_file.name)
                data = self.data_matrix(dst_depth)
                self.assertTrue(
                    im.data.dtype is data.dtype,
                    msg="dtype expected={} found={}, conversion failed {} > {}"
                    .format(data.dtype, im.data.dtype, src_depth, dst_depth))
                self.assertTrue(
                    (im.data == data).all(),
                    msg="data alteration during conversion {} > {}.".format(
                        src_depth, dst_depth))
        tif_file.close()
示例#2
0
    def convert_image(input_file, input_desc, crop_info=None):
        """
        Convert image if needed
        """
        # Image has to be always loaded to test width and size
        im = Image(src=input_file)
        dst_file = os.path.splitext(input_file)[0] + input_desc.get('ext')

        program = [
            # Color conversion
            ConverterChannels(input_desc, im),
            # Depth conversion
            ConverterDepth(input_desc, im),
            # Crop before reducing image to max_pixels (or the crop box will be outside of scope)
            ConverterCrop(input_desc, im, crop_info),
            # Resize image if bigger than a max.
            ConverterMaxPixels(input_desc, im),
            # Re-encoding (ex: jpg > png)
            ConverterEncoding(input_desc, input_file, dst_file),
        ]
        messages = []
        lossy_modification = False
        modification = False

        for task in program:
            if not task.need_conversion():
                continue
            if not task.can_convert():
                return 2, []  # Conversion needed but forbidden

            info_loss = task.information_loss()
            message = task.convert()
            modification = True
            if info_loss:
                messages.append(message)
                lossy_modification = True

        # something have been done, write file
        if modification:
            im.write(dst_file)
        # Nothing done, ensure expected extension (.jpe > .jpeg; .tif > .tiff)
        elif input_file != dst_file:
            os.rename(input_file, dst_file)
        if lossy_modification:
            return 1, messages
        return 0, []
示例#3
0
 def test_8i_extensions(self):
     """
     Tests the conversions between 8 bits file formats.
     """
     data = self.data_matrix('8i')
     # gif is not supported by OpenCV, .jpg do not keep exact colors
     exts = {
         '.bmp': 'image/bmp',
         '.png': 'image/png',
         '.tif': 'image/tiff',
         '.TIFF': 'image/tiff',
     }
     for src_ext, src_mime in list(exts.items()):
         src_file = tempfile.NamedTemporaryFile(suffix=src_ext,
                                                prefix="ipol_",
                                                delete=True)
         src_im = Image(data=data)
         src_im.write(src_file.name)
         self.assertTrue(magic.from_file(src_file.name,
                                         mime=True) == src_mime,
                         msg="Bad file encoding, {} is not {}".format(
                             src_file.name, src_mime))
         src_im = Image(src=src_file.name)
         for dst_ext, dst_mime in list(exts.items()):
             dst_file = tempfile.NamedTemporaryFile(suffix=dst_ext,
                                                    prefix="ipol_",
                                                    delete=True)
             src_im.write(
                 dst_file.name)  # write to dest should encoding conversion
             self.assertTrue(magic.from_file(dst_file.name,
                                             mime=True) == dst_mime,
                             msg="Bad file encoding, {} is not {}".format(
                                 dst_file.name, dst_mime))
             dst_im = Image(src=dst_file.name)
             self.assertTrue((src_im.data == dst_im.data).all(),
                             msg="{} > {}".format(src_ext, dst_ext))
             dst_file.close()
         src_file.close()