Пример #1
0
    def test_raises_error_on_invalid_header(self):
        import imghdr

        f = io.BytesIO()
        f.write(b'Not an image')
        f.seek(0)

        with self.assertRaises(UnrecognisedImageFormatError) as e:
            Image.open(f)
Пример #2
0
    def test_raises_error_on_invalid_header(self):
        import imghdr

        f = io.BytesIO()
        f.write(b'Not an image')
        f.seek(0)

        with self.assertRaises(UnrecognisedImageFormatError) as e:
            Image.open(f)
Пример #3
0
def has_jpeg_support():
    wagtail_jpg = os.path.join(os.path.dirname(__file__), 'check_files', 'wagtail.jpg')
    succeeded = True

    with open(wagtail_jpg, 'rb') as f:
        try:
            Image.open(f)
        except (IOError, Image.LoaderError):
            succeeded = False

    return succeeded
Пример #4
0
def has_jpeg_support():
    wagtail_jpg = os.path.join(os.path.dirname(__file__), 'check_files', 'wagtail.jpg')
    succeeded = True

    with open(wagtail_jpg, 'rb') as f:
        try:
            Image.open(f)
        except (IOError, Image.LoaderError):
            succeeded = False

    return succeeded
Пример #5
0
def has_png_support():
    tuiuiu_png = os.path.join(os.path.dirname(__file__), 'check_files',
                              'tuiuiu.png')
    succeeded = True

    with open(tuiuiu_png, 'rb') as f:
        try:
            Image.open(f)
        except (IOError, Image.LoaderError):
            succeeded = False

    return succeeded
Пример #6
0
def has_png_support():
    wagtail_png = os.path.join(os.path.dirname(__file__), "check_files",
                               "wagtail.png")
    succeeded = True

    with open(wagtail_png, "rb") as f:
        try:
            Image.open(f)
        except (IOError, Image.LoaderError):
            succeeded = False

    return succeeded
Пример #7
0
    def test_image_format_detection(self):
        Image.loaders = {
            'png': [
                (0, self.FakeBackend),
            ],
            'jpeg': [
                (100, self.AnotherFakeBackend),
            ],
        }

        self.assertIsInstance(Image.open('tests/images/transparent.png').backend, self.FakeBackend)
        self.assertIsInstance(Image.open('tests/images/flower.jpg').backend, self.AnotherFakeBackend)
Пример #8
0
    def test_image_detect_stream(self):
        Image.loaders = {
            'png': [
                (0, self.FakeBackend),
            ],
            'jpeg': [
                (100, self.AnotherFakeBackend),
            ],
        }

        with open('tests/images/transparent.png', 'rb') as f:
            self.assertIsInstance(Image.open(f).backend, self.FakeBackend)
        with open('tests/images/flower.jpg', 'rb') as f:
            self.assertIsInstance(Image.open(f).backend, self.AnotherFakeBackend)
Пример #9
0
    def test_image_detect_stream(self):
        Image.loaders = {
            'png': [
                (0, self.FakeBackend),
            ],
            'jpeg': [
                (100, self.AnotherFakeBackend),
            ],
        }

        with open('tests/images/transparent.png', 'rb') as f:
            self.assertIsInstance(Image.open(f).backend, self.FakeBackend)
        with open('tests/images/flower.jpg', 'rb') as f:
            self.assertIsInstance(
                Image.open(f).backend, self.AnotherFakeBackend)
Пример #10
0
    def test_image_format_detection(self):
        Image.loaders = {
            'png': [
                (0, self.FakeBackend),
            ],
            'jpeg': [
                (100, self.AnotherFakeBackend),
            ],
        }

        self.assertIsInstance(
            Image.open('tests/images/transparent.png').backend,
            self.FakeBackend)
        self.assertIsInstance(
            Image.open('tests/images/flower.jpg').backend,
            self.AnotherFakeBackend)
Пример #11
0
    def get_willow_image(self):
        # Open file if it is closed
        close_file = False
        try:
            image_file = self.file

            if self.file.closed:
                # Reopen the file
                if self.is_stored_locally():
                    self.file.open('rb')
                else:
                    # Some external storage backends don't allow reopening
                    # the file. Get a fresh file instance. #1397
                    storage = self._meta.get_field('file').storage
                    image_file = storage.open(self.file.name, 'rb')

                close_file = True
        except IOError as e:
            # re-throw this as a SourceImageIOError so that calling code can distinguish
            # these from IOErrors elsewhere in the process
            raise SourceImageIOError(text_type(e))

        # Seek to beginning
        image_file.seek(0)

        try:
            yield WillowImage.open(image_file)
        finally:
            if close_file:
                image_file.close()
Пример #12
0
    def __init__(self, path):
        fp = open(path, 'rb')
        img = Image.open(fp)
        dimensions = img.get_size()

        self.width = dimensions[0]
        self.height = dimensions[1]
Пример #13
0
    def get_willow_image(self):
        # Open file if it is closed
        close_file = False
        try:
            image_file = self.file

            if self.file.closed:
                # Reopen the file
                if self.is_stored_locally():
                    self.file.open('rb')
                else:
                    # Some external storage backends don't allow reopening
                    # the file. Get a fresh file instance. #1397
                    storage = self._meta.get_field('file').storage
                    image_file = storage.open(self.file.name, 'rb')

                close_file = True
        except IOError as e:
            # re-throw this as a SourceImageIOError so that calling code can distinguish
            # these from IOErrors elsewhere in the process
            raise SourceImageIOError(text_type(e))

        # Seek to beginning
        image_file.seek(0)

        try:
            yield WillowImage.open(image_file)
        finally:
            if close_file:
                image_file.close()
Пример #14
0
    def test_tiff(self):
        with open('tests/images/cameraman.tif', 'rb') as f:
            image = Image.open(f)
            width, height = image.get_size()

        self.assertIsInstance(image, TIFFImageFile)
        self.assertEqual(width, 256)
        self.assertEqual(height, 256)
Пример #15
0
    def test_bmp(self):
        with open('tests/images/sails.bmp', 'rb') as f:
            image = Image.open(f)
            width, height = image.get_size()

        self.assertIsInstance(image, BMPImageFile)
        self.assertEqual(width, 768)
        self.assertEqual(height, 512)
Пример #16
0
 def convert_bmp_image(self, file_path):
     new_file_path = file_path.replace("bmp", "jpg")
     with open(file_path, 'rb') as f:
         img = WillowImage.open(f)
         output = open(new_file_path, 'wb')
         img.save_as_jpeg(output)
         output.close()
         return new_file_path
Пример #17
0
    def test_opens_jpeg(self):
        import imghdr

        f = io.BytesIO()
        f.write(b'\xff\xd8\xff\xe0\x00\x10JFIF\x00')
        f.seek(0)

        image = Image.open(f)
        self.assertIsInstance(image, JPEGImageFile)
        self.assertEqual(image.format_name, 'jpeg')
        self.assertEqual(image.original_format, 'jpeg')
Пример #18
0
    def test_opens_jpeg(self):
        import imghdr

        f = io.BytesIO()
        f.write(b'\xff\xd8\xff\xe0\x00\x10JFIF\x00')
        f.seek(0)

        image = Image.open(f)
        self.assertIsInstance(image, JPEGImageFile)
        self.assertEqual(image.format_name, 'jpeg')
        self.assertEqual(image.original_format, 'jpeg')
Пример #19
0
    def test_opens_png(self):
        import imghdr

        f = io.BytesIO()
        f.write(b'\x89PNG\x0d\x0a\x1a\x0a')
        f.seek(0)

        image = Image.open(f)
        self.assertIsInstance(image, PNGImageFile)
        self.assertEqual(image.format_name, 'png')
        self.assertEqual(image.original_format, 'png')
Пример #20
0
    def test_opens_png(self):
        import imghdr

        f = io.BytesIO()
        f.write(b'\x89PNG\x0d\x0a\x1a\x0a')
        f.seek(0)

        image = Image.open(f)
        self.assertIsInstance(image, PNGImageFile)
        self.assertEqual(image.format_name, 'png')
        self.assertEqual(image.original_format, 'png')
Пример #21
0
    def test_opens_gif(self):
        import imghdr

        f = io.BytesIO()
        f.write(b'GIF89a')
        f.seek(0)

        image = Image.open(f)
        self.assertIsInstance(image, GIFImageFile)
        self.assertEqual(image.format_name, 'gif')
        self.assertEqual(image.original_format, 'gif')
Пример #22
0
    def test_opens_gif(self):
        import imghdr

        f = io.BytesIO()
        f.write(b'GIF89a')
        f.seek(0)

        image = Image.open(f)
        self.assertIsInstance(image, GIFImageFile)
        self.assertEqual(image.format_name, 'gif')
        self.assertEqual(image.original_format, 'gif')
Пример #23
0
    def get_willow_image(self):
        try:
            image_file = self.file.file  # triggers a call to self.storage.open, so IOErrors from missing files will be raised at this point
        except IOError as e:
            # re-throw this as a SourceImageIOError so that calling code can distinguish
            # these from IOErrors elsewhere in the process
            raise SourceImageIOError(text_type(e))

        image_file.open('rb')
        image_file.seek(0)

        return WillowImage.open(image_file)
Пример #24
0
    def get_willow_image(self):
        # Open file if it is closed
        close_file = False
        try:
            if self.file.closed:
                self.file.open('rb')
                close_file = True
        except IOError as e:
            # re-throw this as a SourceImageIOError so that calling code can distinguish
            # these from IOErrors elsewhere in the process
            raise SourceImageIOError(text_type(e))

        # Seek to beginning
        self.file.seek(0)

        try:
            yield WillowImage.open(self.file)
        finally:
            if close_file:
                self.file.close()
Пример #25
0
    def get_willow_image(self):
        # Open file if it is closed
        close_file = False
        try:
            if self.file.closed:
                self.file.open('rb')
                close_file = True
        except IOError as e:
            # re-throw this as a SourceImageIOError so that calling code can distinguish
            # these from IOErrors elsewhere in the process
            raise SourceImageIOError(text_type(e))

        # Seek to beginning
        self.file.seek(0)

        try:
            yield WillowImage.open(self.file)
        finally:
            if close_file:
                self.file.close()
Пример #26
0
def test():
    with open('scientists.jpg', 'rb') as f:
        faces = Image.open(f).detect_faces()

        print(list(faces))
Пример #27
0
 def get_willow_image(self):
     with self.open_file() as image_file:
         yield WillowImage.open(image_file)
Пример #28
0
 def get_willow_image(self):
     with self.open_file() as image_file:
         yield WillowImage.open(image_file)