Example #1
0
 def test_gifsicle_throws_error(self, mock_popen):
     """Test gifsicle where Gifsicle throws an error"""
     mock_image = Mock()
     mock_popen.side_effect = OSError('Misc Exception')
     result, error = utils.resize_gif(mock_image, (300, 300))
     mock_popen.assert_called_once()
     self.assertEqual(result, '')
     self.assertEqual(error, True)
Example #2
0
    def test_gifsicle_empty_result(self, mock_popen):
        """Test a call to gifsicle that has an empty result"""
        mock_image = Mock()
        mock_process = Mock()
        mock_process.communicate.return_value = ('', False)
        mock_popen.return_value = mock_process
        result, error = utils.resize_gif(mock_image, (300, 300))
        mock_popen.assert_called_once_with(
            [
                'gifsicle',
                '--resize-fit',
                '300x300'
            ],
            stdin=self.mock_pipe, stdout=self.mock_pipe
        )
        self.assertEqual(mock_image.seek.call_count, 3)
        mock_process.communicate.assert_called_once_with(
            mock_image.read())

        self.assertEqual(result, '')
        self.assertEqual(error, True)
Example #3
0
    def _resize(self, size, filename_format=None):
        """Resizes the image"""

        if not filename_format:
            filename_format = '{name}_{width}x{height}.{extension}'

        # Re-open the image
        self.image.open()
        image_file = StringIO(self.image.read())
        image_file.seek(0)

        # Open the image
        original = PILImage.open(image_file)

        # Detect the image type
        pil_type = original.format
        if pil_type == 'JPEG':
            django_type = 'image/jpeg'
            file_extension = 'jpg'
        elif pil_type == 'PNG':
            django_type = 'image/png'
            file_extension = 'png'
        elif pil_type == 'GIF':
            django_type = 'image/gif'
            file_extension = 'gif'
        else:
            django_type = 'image/jpeg'
            file_extension = 'jpg'

        temp_handle = StringIO()

        if pil_type == 'GIF':
            # Process Gif
            result, error = resize_gif(image_file, size)
            if not error:
                temp_handle.write(result)
            else:
                # If something went wrong, use the original file
                temp_handle = image_file
        else:
            # Create the thumbnail
            original.thumbnail(size, PILImage.ANTIALIAS)
            original.save(temp_handle, pil_type)

        temp_handle.seek(0)

        # Create a SimpleUploadFile we can pass to the django ImageField
        suf = SimpleUploadedFile(
            name=os.path.split(self.image.name)[-1],
            content=temp_handle.read(),
            content_type=django_type
        )

        # Close out the files
        image_file.seek(0)
        temp_handle.close()
        image_file.close()

        # Generate the filename of our thumbnail
        filename = filename_format.format(
            name=os.path.splitext(suf.name)[0],
            width=size[0],
            height=size[1],
            extension=file_extension
        )

        return suf, filename
Example #4
0
 def test_gifsicle_only_allows_gif(self, mock_pil):
     """Test gifsicle where something other than a GIF is thrown in"""
     # Here a Mock() object will be passed in, which is obviously not 'GIF'
     with self.assertRaises(ValueError):
         utils.resize_gif(Mock(), (300, 300))