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