コード例 #1
0
    def setUp(self, width=1280, height=1024):
        """Run before the test."""
        # Create an image in memory

        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGBA", (width, height), (255, 0, 0, 0))
        image.save(tmp_file, 'png')

        # create a new gif image
        self.image_gif = MultimediaImage(create_gif_from_frames([
            Image.new("RGB", (width, height), color)
            for color in ['blue', 'yellow', 'red', 'black', 'white']
        ]))
        self.width, self.height = width, height
        # Initialize it for our object and create and instance for
        # each test
        tmp_file.seek(0)
        self.image_resize = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_crop = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_rotate = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_errors = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_convert = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_save = MultimediaImage.from_string(tmp_file)

        # NOT RGBA
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGB", (width, height), (255, 0, 0, 0))
        image.save(tmp_file, 'jpeg')
        tmp_file.seek(0)
        self.image_not_rgba = MultimediaImage.from_string(tmp_file)

        # Image in P Mode
        tmp_file = BytesIO()
        image = Image.new("P", (width, height))
        image.save(tmp_file, 'gif')
        tmp_file.seek(0)
        self.image_p_mode = MultimediaImage.from_string(tmp_file)

        # TIFF Image
        tmp_file = BytesIO()
        image = Image.new("RGBA", (width, height), (255, 0, 0, 0))
        image.save(tmp_file, 'tiff')
        tmp_file.seek(0)
        self.image_tiff = MultimediaImage.from_string(tmp_file)
コード例 #2
0
    def setUp(self):
        """Run before the test."""
        # Create an image in memory
        from PIL import Image
        from flask_iiif.api import MultimediaImage
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGBA", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'png')

        # Initialize it for our object and create and instance for
        # each test
        tmp_file.seek(0)
        self.image_resize = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_crop = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_rotate = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_errors = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_convert = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_save = MultimediaImage.from_string(tmp_file)

        # NOT RGBA
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGB", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'jpeg')
        tmp_file.seek(0)
        self.image_not_rgba = MultimediaImage.from_string(tmp_file)
コード例 #3
0
    def setUp(self):
        """Run before the test."""
        # Create an image in memory
        from PIL import Image
        from flask_iiif.api import MultimediaImage
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGBA", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'png')

        # Initialize it for our object and create and instance for
        # each test
        tmp_file.seek(0)
        self.image_resize = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_crop = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_rotate = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_errors = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_convert = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_save = MultimediaImage.from_string(tmp_file)

        # NOT RGBA
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGB", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'jpeg')
        tmp_file.seek(0)
        self.image_not_rgba = MultimediaImage.from_string(tmp_file)
コード例 #4
0
    def test_image_recreation(self):
        """Test the image recreation from cache."""
        from flask_iiif.api import MultimediaImage

        # Seek position
        self.image_file.seek(0)
        # Add the image to cache
        self.cache.set('image_2', self.image_file.getvalue())
        # Get image from cache
        image_string = self.cache.get('image_2')
        # Create a ByteIO object
        cached_image = BytesIO(image_string)
        # Seek object to the right position
        cached_image.seek(0)
        # Create an image object form the stored string
        image = MultimediaImage.from_string(cached_image)
        # Check if the image is still the same
        self.assertEqual(str(image.size()), str((1280, 1024)))
コード例 #5
0
    def setUp(self):
        """Run before the test."""
        # Create an image in memory
        from PIL import Image
        from flask_iiif.api import MultimediaImage
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGBA", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'png')

        # create a new gif image
        self.image_gif = MultimediaImage(
            create_gif_from_frames([
                Image.new("RGB", (1280, 1024), color)
                for color in ['blue', 'yellow', 'red', 'black', 'white']
            ]))

        # Initialize it for our object and create and instance for
        # each test
        tmp_file.seek(0)
        self.image_resize = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_crop = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_rotate = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_errors = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_convert = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_save = MultimediaImage.from_string(tmp_file)

        # NOT RGBA
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGB", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'jpeg')
        tmp_file.seek(0)
        self.image_not_rgba = MultimediaImage.from_string(tmp_file)

        # Image in P Mode
        tmp_file = BytesIO()
        image = Image.new("P", (1280, 1024))
        image.save(tmp_file, 'gif')
        tmp_file.seek(0)
        self.image_p_mode = MultimediaImage.from_string(tmp_file)

        # TIFF Image
        tmp_file = BytesIO()
        image = Image.new("RGBA", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'tiff')
        tmp_file.seek(0)
        self.image_tiff = MultimediaImage.from_string(tmp_file)
コード例 #6
0
class TestMultimediaAPI(IIIFTestCase):
    """Multimedia Image API test case."""
    def setUp(self):
        """Run before the test."""
        # Create an image in memory
        from PIL import Image
        from flask_iiif.api import MultimediaImage
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGBA", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'png')

        # create a new gif image
        self.image_gif = MultimediaImage(
            create_gif_from_frames([
                Image.new("RGB", (1280, 1024), color)
                for color in ['blue', 'yellow', 'red', 'black', 'white']
            ]))

        # Initialize it for our object and create and instance for
        # each test
        tmp_file.seek(0)
        self.image_resize = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_crop = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_rotate = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_errors = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_convert = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_save = MultimediaImage.from_string(tmp_file)

        # NOT RGBA
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGB", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'jpeg')
        tmp_file.seek(0)
        self.image_not_rgba = MultimediaImage.from_string(tmp_file)

        # Image in P Mode
        tmp_file = BytesIO()
        image = Image.new("P", (1280, 1024))
        image.save(tmp_file, 'gif')
        tmp_file.seek(0)
        self.image_p_mode = MultimediaImage.from_string(tmp_file)

        # TIFF Image
        tmp_file = BytesIO()
        image = Image.new("RGBA", (1280, 1024), (255, 0, 0, 0))
        image.save(tmp_file, 'tiff')
        tmp_file.seek(0)
        self.image_tiff = MultimediaImage.from_string(tmp_file)

    def test_gif_resize(self):
        """Test image resize function on GIF images."""
        # Check original size and GIF properties
        self.assertEqual(self.image_gif.image.is_animated, True)
        self.assertEqual(self.image_gif.image.n_frames, 5)
        self.assertEqual(str(self.image_gif.size()), str((1280, 1024)))

        # Assert proper resize and preservation of GIF properties
        self.image_gif.resize('720,680')
        self.assertEqual(self.image_gif.image.is_animated, True)
        self.assertEqual(self.image_gif.image.n_frames, 5)
        self.assertEqual(str(self.image_gif.size()), str((720, 680)))

    def test_image_resize(self):
        """Test image resize function."""
        # Test image size before
        self.assertEqual(str(self.image_resize.size()), str((1280, 1024)))

        # Resize.image_resize to 720,680
        self.image_resize.resize('720,680')
        self.assertEqual(str(self.image_resize.size()), str((720, 680)))

        # Resize.image_resize to 300,
        self.image_resize.resize('300,')
        self.assertEqual(str(self.image_resize.size()), str((300, 283)))

        # Resize.image_resize to ,300
        self.image_resize.resize(',300')
        self.assertEqual(str(self.image_resize.size()), str((318, 300)))

        # Resize.image_resize to pct:90
        self.image_resize.resize('pct:90')
        self.assertEqual(str(self.image_resize.size()), str((286, 270)))

        # Resize.image_resize to !100,100
        self.image_resize.resize('!100,100')
        self.assertEqual(str(self.image_resize.size()), str((34, 34)))

    def test_errors(self):
        """Test errors."""
        from flask_iiif.errors import (MultimediaImageResizeError,
                                       MultimediaImageCropError,
                                       MultimediaImageNotFound,
                                       MultimediaImageQualityError,
                                       MultimediaImageFormatError)
        # Test resize errors
        self.assertRaises(MultimediaImageResizeError, self.image_errors.resize,
                          'pct:-12222')
        self.assertRaises(MultimediaImageResizeError, self.image_errors.resize,
                          '2')
        self.assertRaises(MultimediaImageResizeError, self.image_errors.resize,
                          '-22,100')
        # Test crop errors
        self.assertRaises(MultimediaImageCropError, self.image_errors.crop,
                          '22,100,222')
        self.assertRaises(MultimediaImageCropError, self.image_errors.crop,
                          '-22,100,222,323')
        self.assertRaises(MultimediaImageCropError, self.image_errors.crop,
                          'pct:222,100,222,323')
        self.assertRaises(MultimediaImageCropError, self.image_errors.crop,
                          '2000,2000,2000,2000')
        self.assertRaises(MultimediaImageNotFound, self.image_errors.from_file,
                          "unicorn")
        with self.app.app_context():
            self.assertRaises(MultimediaImageQualityError,
                              self.image_errors.quality, 'pct:222,100,222,323')
            self.assertRaises(MultimediaImageFormatError,
                              self.image_errors._prepare_for_output,
                              'pct:222,100,222,323')

    def test_image_crop(self):
        """Test the crop function."""
        # Crop image x,y,w,h
        self.image_crop.crop('20,20,400,300')
        self.assertEqual(str(self.image_crop.size()), str((400, 300)))

        # Crop image pct:x,y,w,h
        self.image_crop.crop('pct:20,20,40,30')
        self.assertEqual(str(self.image_crop.size()), str((160, 90)))

        # Check if exeeds image borders
        self.image_crop.crop('10,10,160,90')
        self.assertEqual(str(self.image_crop.size()), str((150, 80)))

    def test_image_rotate(self):
        """Test image rotate function."""
        self.image_rotate.rotate(90)
        self.assertEqual(str(self.image_rotate.size()), str((1024, 1280)))

        self.image_rotate.rotate(120)
        self.assertEqual(str(self.image_rotate.size()), str((1024, 1280)))

    def test_image_mode(self):
        """Test image mode."""
        self.image_not_rgba.quality('grey')
        self.assertEqual(self.image_not_rgba.image.mode, "L")

    def test_image_incompatible_modes(self):
        """Test P-incompatible image to RGB auto-convert."""
        tmp_file = BytesIO()
        self.image_p_mode.save(tmp_file)
        self.assertEqual(self.image_p_mode.image.mode, "RGB")

    def test_image_saving(self):
        """Test image saving."""
        tmp_file = BytesIO()
        compare_file = BytesIO()
        self.assertEqual(tmp_file.getvalue(), compare_file.getvalue())
        self.image_save.save(tmp_file)
        self.assertNotEqual(str(tmp_file.getvalue()), compare_file.getvalue())

    def test_image_tiff_support(self):
        """Test TIFF image support."""
        self.assertEqual(self.image_tiff.image.format, 'TIFF')
コード例 #7
0
class TestMultimediaAPI(IIIFTestCase):
    """Multimedia Image API test case."""

    @pytest.mark.parametrize("width, height",
                             [(1280, 1024),
                              (100, 100),
                              (1024, 1280),  # portrait
                              (200, 100),
                              (1280, 720),
                              ])
    def setUp(self, width=1280, height=1024):
        """Run before the test."""
        # Create an image in memory

        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGBA", (width, height), (255, 0, 0, 0))
        image.save(tmp_file, 'png')

        # create a new gif image
        self.image_gif = MultimediaImage(create_gif_from_frames([
            Image.new("RGB", (width, height), color)
            for color in ['blue', 'yellow', 'red', 'black', 'white']
        ]))
        self.width, self.height = width, height
        # Initialize it for our object and create and instance for
        # each test
        tmp_file.seek(0)
        self.image_resize = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_crop = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_rotate = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_errors = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_convert = MultimediaImage.from_string(tmp_file)
        tmp_file.seek(0)
        self.image_save = MultimediaImage.from_string(tmp_file)

        # NOT RGBA
        tmp_file = BytesIO()
        # create a new image
        image = Image.new("RGB", (width, height), (255, 0, 0, 0))
        image.save(tmp_file, 'jpeg')
        tmp_file.seek(0)
        self.image_not_rgba = MultimediaImage.from_string(tmp_file)

        # Image in P Mode
        tmp_file = BytesIO()
        image = Image.new("P", (width, height))
        image.save(tmp_file, 'gif')
        tmp_file.seek(0)
        self.image_p_mode = MultimediaImage.from_string(tmp_file)

        # TIFF Image
        tmp_file = BytesIO()
        image = Image.new("RGBA", (width, height), (255, 0, 0, 0))
        image.save(tmp_file, 'tiff')
        tmp_file.seek(0)
        self.image_tiff = MultimediaImage.from_string(tmp_file)

    def test_gif_resize(self):
        """Test image resize function on GIF images."""
        # Check original size and GIF properties
        self.assertEqual(self.image_gif.image.is_animated, True)
        self.assertEqual(self.image_gif.image.n_frames, 5)
        self.assertEqual(str(self.image_gif.size()),
                         str((self.width, self.height)))

        # Assert proper resize and preservation of GIF properties
        self.image_gif.resize('720,680')
        self.assertEqual(self.image_gif.image.is_animated, True)
        self.assertEqual(self.image_gif.image.n_frames, 5)
        self.assertEqual(str(self.image_gif.size()), str((720, 680)))

        # test gif resize best fit
        self.image_gif.resize('!400,220')
        self.assertEqual(self.image_gif.image.is_animated, True)
        self.assertEqual(self.image_gif.image.n_frames, 5)
        self.assertEqual(str(self.image_gif.size()), str((400, 220)))

    def test_image_resize(self):
        """Test image resize function."""
        # Test image size before
        self.assertEqual(str(self.image_resize.size()),
                         str((self.width, self.height)))

        # Resize.image_resize to 720,680
        self.image_resize.resize('720,680')
        self.assertEqual(str(self.image_resize.size()), str((720, 680)))

        # Resize.image_resize to 300,
        self.image_resize.resize('300,')
        self.assertEqual(str(self.image_resize.size()), str((300, 283)))

        # Resize.image_resize to ,300
        self.image_resize.resize(',300')
        self.assertEqual(str(self.image_resize.size()), str((318, 300)))

        # Resize.image_resize to pct:90
        self.image_resize.resize('pct:90')
        self.assertEqual(str(self.image_resize.size()), str((286, 270)))

        # Resize.image_resize to !100,100
        self.image_resize.resize('!100,100')
        self.assertEqual(str(self.image_resize.size()), str((100, 100)))

    def test_errors(self):
        """Test errors."""
        from flask_iiif.errors import (
            MultimediaImageResizeError, MultimediaImageCropError,
            MultimediaImageNotFound, MultimediaImageQualityError,
            MultimediaImageFormatError
        )
        # Test resize errors
        self.assertRaises(
            MultimediaImageResizeError,
            self.image_errors.resize,
            'pct:-12222'
        )
        self.assertRaises(
            MultimediaImageResizeError,
            self.image_errors.resize,
            '2'
        )
        self.assertRaises(
            MultimediaImageResizeError,
            self.image_errors.resize,
            '-22,100'
        )
        # Test crop errors
        self.assertRaises(
            MultimediaImageCropError,
            self.image_errors.crop,
            '22,100,222'
        )
        self.assertRaises(
            MultimediaImageCropError,
            self.image_errors.crop,
            '-22,100,222,323'
        )
        self.assertRaises(
            MultimediaImageCropError,
            self.image_errors.crop,
            'pct:222,100,222,323'
        )
        self.assertRaises(
            MultimediaImageCropError,
            self.image_errors.crop,
            '2000,2000,2000,2000'
        )
        self.assertRaises(
            MultimediaImageNotFound,
            self.image_errors.from_file,
            "unicorn"
        )
        with self.app.app_context():
            self.assertRaises(
                MultimediaImageQualityError,
                self.image_errors.quality,
                'pct:222,100,222,323'
            )
            self.assertRaises(
                MultimediaImageFormatError,
                self.image_errors._prepare_for_output,
                'pct:222,100,222,323'
            )

    def test_image_crop(self):
        """Test the crop function."""
        # Crop image x,y,w,h
        self.image_crop.crop('20,20,400,300')
        self.assertEqual(str(self.image_crop.size()), str((400, 300)))

        # Crop image pct:x,y,w,h
        self.image_crop.crop('pct:20,20,40,30')
        self.assertEqual(str(self.image_crop.size()), str((160, 90)))

        # Check if exeeds image borders
        self.image_crop.crop('10,10,160,90')
        self.assertEqual(str(self.image_crop.size()), str((150, 80)))

    def test_image_rotate(self):
        """Test image rotate function."""
        self.image_rotate.rotate(90)
        self.assertEqual(str(self.image_rotate.size()), str((1024, 1280)))

        self.image_rotate.rotate(120)
        self.assertEqual(str(self.image_rotate.size()), str((1024, 1280)))

    def test_image_mode(self):
        """Test image mode."""
        self.image_not_rgba.quality('grey')
        self.assertEqual(self.image_not_rgba.image.mode, "L")

    def test_image_incompatible_modes(self):
        """Test P-incompatible image to RGB auto-convert."""
        tmp_file = BytesIO()
        self.image_p_mode.save(tmp_file)
        self.assertEqual(self.image_p_mode.image.mode, "RGB")

    def test_image_saving(self):
        """Test image saving."""
        tmp_file = BytesIO()
        compare_file = BytesIO()
        self.assertEqual(tmp_file.getvalue(), compare_file.getvalue())
        self.image_save.save(tmp_file)
        self.assertNotEqual(str(tmp_file.getvalue()), compare_file.getvalue())

    def test_image_tiff_support(self):
        """Test TIFF image support."""
        self.assertEqual(self.image_tiff.image.format, 'TIFF')