Detect whether mimetype is image/png or image/apng

This commit is contained in:
Andrew Murray 2019-01-02 14:13:10 +11:00
parent e32e3fdbeb
commit 97ae0c42b1
3 changed files with 9 additions and 2 deletions

View File

@ -86,6 +86,7 @@ class TestFilePng(PillowTestCase):
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "PNG")
self.assertEqual(im.get_format_mimetype(), 'image/png')
hopper("1").save(test_file)
Image.open(test_file)
@ -589,6 +590,8 @@ class TestFilePng(PillowTestCase):
"WebP support not installed with animation")
def test_apng(self):
im = Image.open("Tests/images/iss634.apng")
self.assertEqual(im.get_format_mimetype(), 'image/apng')
expected = Image.open("Tests/images/iss634.webp")
self.assert_image_similar(im, expected, 0.23)

View File

@ -79,6 +79,8 @@ class ImageFile(Image.Image):
self._min_frame = 0
self.custom_mimetype = None
self.tile = None
self.readonly = 1 # until we know better
@ -120,7 +122,7 @@ class ImageFile(Image.Image):
def get_format_mimetype(self):
if self.format is None:
return
return Image.MIME.get(self.format.upper())
return self.custom_mimetype or Image.MIME.get(self.format.upper())
def verify(self):
"Check file integrity"

View File

@ -296,6 +296,7 @@ class PngStream(ChunkStream):
self.im_mode = None
self.im_tile = None
self.im_palette = None
self.im_custom_mimetype = None
self.text_memory = 0
@ -529,6 +530,7 @@ class PngStream(ChunkStream):
# APNG chunks
def chunk_acTL(self, pos, length):
s = ImageFile._safe_read(self.fp, length)
self.im_custom_mimetype = 'image/apng'
return s
def chunk_fcTL(self, pos, length):
@ -594,6 +596,7 @@ class PngImageFile(ImageFile.ImageFile):
self.info = self.png.im_info
self._text = None
self.tile = self.png.im_tile
self.custom_mimetype = self.png.im_custom_mimetype
if self.png.im_palette:
rawmode, data = self.png.im_palette
@ -908,4 +911,3 @@ Image.register_save(PngImageFile.format, _save)
Image.register_extensions(PngImageFile.format, [".png", ".apng"])
Image.register_mime(PngImageFile.format, "image/png")
Image.register_mime(PngImageFile.format, "image/apng")