mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
Merge branch 'master' into mime-types
This commit is contained in:
commit
7d3b8e8cea
|
@ -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")
|
||||
|
|
|
@ -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")
|
Loading…
Reference in New Issue
Block a user