|
2 | 2 |
|
3 | 3 | from PIL import Image, TiffImagePlugin |
4 | 4 | from PIL._util import py3 |
| 5 | + |
| 6 | +from io import BytesIO |
5 | 7 | import os |
6 | 8 | import sys |
7 | 9 | import shutil |
@@ -63,8 +65,7 @@ def test_width_height(self): |
63 | 65 |
|
64 | 66 | def test_invalid_image(self): |
65 | 67 | if py3: |
66 | | - import io |
67 | | - im = io.BytesIO(b'') |
| 68 | + im = BytesIO(b'') |
68 | 69 | else: |
69 | 70 | import StringIO |
70 | 71 | im = StringIO.StringIO('') |
@@ -334,14 +335,54 @@ def test_registered_extensions(self): |
334 | 335 | for ext in ['.cur', '.icns', '.tif', '.tiff']: |
335 | 336 | self.assertIn(ext, extensions) |
336 | 337 |
|
337 | | - def test_no_convert_mode(self): |
338 | | - self.assertTrue(not hasattr(TiffImagePlugin, '_convert_mode')) |
| 338 | + def test_supported_modes(self): |
| 339 | + for format in Image.MIME.keys(): |
| 340 | + try: |
| 341 | + save_handler = Image.SAVE[format] |
| 342 | + except KeyError: |
| 343 | + continue |
| 344 | + plugin = sys.modules[save_handler.__module__] |
| 345 | + if not hasattr(plugin, '_supported_modes'): |
| 346 | + continue |
| 347 | + |
| 348 | + # Check that the supported modes list is accurate |
| 349 | + supported_modes = plugin._supported_modes() |
| 350 | + for mode in ['1', 'L', 'P', 'RGB', 'RGBA', 'CMYK', 'YCbCr', 'LAB', |
| 351 | + 'HSV', 'I', 'F', 'LA', 'La', 'RGBX', 'RGBa']: |
| 352 | + out = BytesIO() |
| 353 | + im = Image.new(mode, (100, 100)) |
| 354 | + if mode in supported_modes: |
| 355 | + im.save(out, format) |
| 356 | + else: |
| 357 | + self.assertRaises(Exception, im.save, out, format) |
| 358 | + |
| 359 | + def test_no_supported_modes_method(self): |
| 360 | + self.assertTrue(not hasattr(TiffImagePlugin, '_supported_modes')) |
339 | 361 |
|
340 | 362 | temp_file = self.tempfile("temp.tiff") |
341 | 363 |
|
342 | 364 | im = hopper() |
343 | 365 | im.save(temp_file, convert_mode=True) |
344 | 366 |
|
| 367 | + def test_convert_mode(self): |
| 368 | + for mode, modes in [ |
| 369 | + ['P', []], # no modes |
| 370 | + ['P', ['P']] # same mode |
| 371 | + ]: |
| 372 | + im = Image.new(mode, (100, 100)) |
| 373 | + self.assertIsNone(im._convert_mode(modes)) |
| 374 | + |
| 375 | + for mode, modes in [ |
| 376 | + ['P', ['RGB']], |
| 377 | + ['P', ['L']], # converting to a non-preferred mode |
| 378 | + ['LA', ['P']], |
| 379 | + ['I', ['L']], |
| 380 | + ['RGB', ['L']], |
| 381 | + ['RGB', ['CMYK']] |
| 382 | + ]: |
| 383 | + im = Image.new(mode, (100, 100)) |
| 384 | + self.assertIsNotNone(im._convert_mode(modes)) |
| 385 | + |
345 | 386 | def test_effect_mandelbrot(self): |
346 | 387 | # Arrange |
347 | 388 | size = (512, 512) |
|
0 commit comments