From 6793b5bbd5d2020feaab09589e26c9e7e4bbeae5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 30 Jun 2018 21:08:41 +1000 Subject: [PATCH] Added ImageFile get_format_mimetype method --- Tests/test_file_jpeg.py | 1 + Tests/test_file_pixar.py | 1 + Tests/test_imagefile.py | 7 +++++++ src/PIL/ImageFile.py | 5 +++++ 4 files changed, 14 insertions(+) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 9804c2676..2d9a9a091 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -48,6 +48,7 @@ class TestFileJpeg(PillowTestCase): self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "JPEG") + self.assertEqual(im.get_format_mimetype(), "image/jpeg") def test_app(self): # Test APP/COM reader (@PIL135) diff --git a/Tests/test_file_pixar.py b/Tests/test_file_pixar.py index ae8c7d5f5..3b00c710e 100644 --- a/Tests/test_file_pixar.py +++ b/Tests/test_file_pixar.py @@ -13,6 +13,7 @@ class TestFilePixar(PillowTestCase): self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "PIXAR") + self.assertIsNone(im.get_format_mimetype()) im2 = hopper() self.assert_image_similar(im, im2, 4.8) diff --git a/Tests/test_imagefile.py b/Tests/test_imagefile.py index 32e44a0e1..4b750af0d 100644 --- a/Tests/test_imagefile.py +++ b/Tests/test_imagefile.py @@ -223,6 +223,13 @@ class TestPyDecoder(PillowTestCase): im.tile = [("MOCK", (xoff, yoff, xoff+xsize, yoff+ysize + 100), 32, None)] self.assertRaises(ValueError, im.load) + def test_no_format(self): + buf = BytesIO(b'\x00'*255) + + im = MockImageFile(buf) + self.assertIsNone(im.format) + self.assertIsNone(im.get_format_mimetype()) + if __name__ == '__main__': unittest.main() diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index 1a3c4aa94..875fc50db 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -118,6 +118,11 @@ class ImageFile(Image.Image): pass + def get_format_mimetype(self): + if self.format is None: + return + return Image.MIME.get(self.format.upper()) + def verify(self): "Check file integrity"