Merge branch 'master' into mime-types

This commit is contained in:
Andrew Murray 2019-03-06 07:08:00 +11:00 committed by GitHub
commit 7d3b8e8cea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -14,12 +14,14 @@ class TestFilePpm(PillowTestCase):
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "PPM")
self.assertEqual(im.get_format_mimetype(), "image/x-portable-pixmap")
def test_16bit_pgm(self):
im = Image.open('Tests/images/16_bit_binary.pgm')
im.load()
self.assertEqual(im.mode, 'I')
self.assertEqual(im.size, (20, 100))
self.assertEqual(im.get_format_mimetype(), "image/x-portable-graymap")
tgt = Image.open('Tests/images/16_bit_binary_pgm.png')
self.assert_image_equal(im, tgt)
@ -59,3 +61,16 @@ class TestFilePpm(PillowTestCase):
with self.assertRaises(IOError):
Image.open('Tests/images/negative_size.ppm')
def test_mimetypes(self):
path = self.tempfile('temp.pgm')
with open(path, 'w') as f:
f.write("P4\n128 128\n255")
im = Image.open(path)
self.assertEqual(im.get_format_mimetype(), "image/x-portable-bitmap")
with open(path, 'w') as f:
f.write("PyCMYK\n128 128\n255")
im = Image.open(path)
self.assertEqual(im.get_format_mimetype(), "image/x-portable-anymap")

View File

@ -70,7 +70,14 @@ class PpmImageFile(ImageFile.ImageFile):
s = self.fp.read(1)
if s != b"P":
raise SyntaxError("not a PPM file")
mode = MODES[self._token(s)]
magic_number = self._token(s)
mode = MODES[magic_number]
self.custom_mimetype = {
b"P4": "image/x-portable-bitmap",
b"P5": "image/x-portable-graymap",
b"P6": "image/x-portable-pixmap",
}.get(magic_number)
if mode == "1":
self.mode = "1"
@ -158,3 +165,5 @@ Image.register_open(PpmImageFile.format, PpmImageFile, _accept)
Image.register_save(PpmImageFile.format, _save)
Image.register_extensions(PpmImageFile.format, [".pbm", ".pgm", ".ppm", ".pnm"])
Image.register_mime(PpmImageFile.format, "image/x-portable-anymap")