diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 47f8b845e..dc239cc4c 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -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) @@ -49,3 +51,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") diff --git a/src/PIL/PpmImagePlugin.py b/src/PIL/PpmImagePlugin.py index 750454dc5..e3e411cad 100644 --- a/src/PIL/PpmImagePlugin.py +++ b/src/PIL/PpmImagePlugin.py @@ -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"]) + +Image.register_mime(PpmImageFile.format, "image/x-portable-anymap")