|
| 1 | +import unittest |
| 2 | +import io |
| 3 | +import imghdr |
| 4 | + |
| 5 | +from PIL import Image as PILImage |
| 6 | + |
| 7 | +from willow.image import ( |
| 8 | + GIFImageFile, BadImageOperationError, WebMVP9ImageFile, OggTheoraImageFile, MP4H264ImageFile |
| 9 | +) |
| 10 | +from willow.plugins.ffmpeg import FFMpegLazyVideo, probe |
| 11 | + |
| 12 | + |
| 13 | +class TestFFMpegOperations(unittest.TestCase): |
| 14 | + def setUp(self): |
| 15 | + self.f = open('tests/images/newtons_cradle.gif', 'rb') |
| 16 | + self.image = FFMpegLazyVideo.open(GIFImageFile(self.f)) |
| 17 | + |
| 18 | + def tearDown(self): |
| 19 | + self.f.close() |
| 20 | + |
| 21 | + def test_get_size(self): |
| 22 | + width, height = self.image.get_size() |
| 23 | + self.assertEqual(width, 480) |
| 24 | + self.assertEqual(height, 360) |
| 25 | + |
| 26 | + def test_get_frame_count(self): |
| 27 | + frames = self.image.get_frame_count() |
| 28 | + self.assertEqual(frames, 34) |
| 29 | + |
| 30 | + def test_resize(self): |
| 31 | + resized_image = self.image.resize((100, 75)) |
| 32 | + self.assertEqual(resized_image.get_size(), (100, 75)) |
| 33 | + |
| 34 | + def test_crop(self): |
| 35 | + cropped_image = self.image.crop((10, 10, 100, 100)) |
| 36 | + self.assertEqual(cropped_image.get_size(), (90, 90)) |
| 37 | + |
| 38 | + def test_crop_out_of_bounds(self): |
| 39 | + # crop rectangle should be clamped to the image boundaries |
| 40 | + bottom_right_cropped_image = self.image.crop((150, 100, 250, 200)) |
| 41 | + self.assertEqual(bottom_right_cropped_image.get_size(), (50, 50)) |
| 42 | + |
| 43 | + top_left_cropped_image = self.image.crop((-50, -50, 50, 50)) |
| 44 | + self.assertEqual(top_left_cropped_image.get_size(), (50, 50)) |
| 45 | + |
| 46 | + # fail if the crop rectangle is entirely to the left of the image |
| 47 | + with self.assertRaises(BadImageOperationError): |
| 48 | + self.image.crop((-100, 50, -50, 100)) |
| 49 | + # right edge of crop rectangle is exclusive, so 0 is also invalid |
| 50 | + with self.assertRaises(BadImageOperationError): |
| 51 | + self.image.crop((-50, 50, 0, 100)) |
| 52 | + |
| 53 | + # fail if the crop rectangle is entirely above the image |
| 54 | + with self.assertRaises(BadImageOperationError): |
| 55 | + self.image.crop((50, -100, 100, -50)) |
| 56 | + # bottom edge of crop rectangle is exclusive, so 0 is also invalid |
| 57 | + with self.assertRaises(BadImageOperationError): |
| 58 | + self.image.crop((50, -50, 100, 0)) |
| 59 | + |
| 60 | + # fail if the crop rectangle is entirely to the right of the image |
| 61 | + with self.assertRaises(BadImageOperationError): |
| 62 | + self.image.crop((250, 50, 300, 100)) |
| 63 | + with self.assertRaises(BadImageOperationError): |
| 64 | + self.image.crop((200, 50, 250, 100)) |
| 65 | + |
| 66 | + # fail if the crop rectangle is entirely below the image |
| 67 | + with self.assertRaises(BadImageOperationError): |
| 68 | + self.image.crop((50, 200, 100, 250)) |
| 69 | + with self.assertRaises(BadImageOperationError): |
| 70 | + self.image.crop((50, 150, 100, 200)) |
| 71 | + |
| 72 | + # fail if left edge >= right edge |
| 73 | + with self.assertRaises(BadImageOperationError): |
| 74 | + self.image.crop((125, 25, 25, 125)) |
| 75 | + with self.assertRaises(BadImageOperationError): |
| 76 | + self.image.crop((100, 25, 100, 125)) |
| 77 | + |
| 78 | + # fail if bottom edge >= top edge |
| 79 | + with self.assertRaises(BadImageOperationError): |
| 80 | + self.image.crop((25, 125, 125, 25)) |
| 81 | + with self.assertRaises(BadImageOperationError): |
| 82 | + self.image.crop((25, 100, 125, 100)) |
| 83 | + |
| 84 | + def test_rotate(self): |
| 85 | + rotated_image = self.image.rotate(90) |
| 86 | + width, height = rotated_image.get_size() |
| 87 | + self.assertEqual((width, height), (150, 200)) |
| 88 | + |
| 89 | + def test_rotate_without_multiple_of_90(self): |
| 90 | + with self.assertRaises(UnsupportedRotation) as e: |
| 91 | + rotated_image = self.image.rotate(45) |
| 92 | + |
| 93 | + def test_rotate_greater_than_360(self): |
| 94 | + # 450 should end up the same as a 90 rotation |
| 95 | + rotated_image = self.image.rotate(450) |
| 96 | + width, height = rotated_image.get_size() |
| 97 | + self.assertEqual((width, height), (150, 200)) |
| 98 | + |
| 99 | + def test_rotate_multiple_of_360(self): |
| 100 | + rotated_image = self.image.rotate(720) |
| 101 | + width, height = rotated_image.get_size() |
| 102 | + self.assertEqual((width, height), (200, 150)) |
| 103 | + |
| 104 | + def test_set_background_color_rgb(self): |
| 105 | + red_background_image = self.image.set_background_color_rgb((255, 0, 0)) |
| 106 | + self.assertFalse(red_background_image.has_alpha()) |
| 107 | + self.assertEqual(red_background_image.image.getpixel((10, 10)), (255, 0, 0)) |
| 108 | + |
| 109 | + def test_set_background_color_rgb_color_argument_check(self): |
| 110 | + with self.assertRaises(TypeError) as e: |
| 111 | + self.image.set_background_color_rgb('rgb(255, 0, 0)') |
| 112 | + |
| 113 | + self.assertEqual(str(e.exception), "the 'color' argument must be a 3-element tuple or list") |
| 114 | + |
| 115 | + def test_save_as_webm_vp9(self): |
| 116 | + output = io.BytesIO() |
| 117 | + return_value = self.image.save_as_webm_vp9(output) |
| 118 | + output.seek(0) |
| 119 | + |
| 120 | + probe_data = probe(output) |
| 121 | + |
| 122 | + self.assertEqual(probe_data['format']['format_name'], 'matroska,webm') |
| 123 | + self.assertEqual(probe_data['streams'][0]['codec_name'], 'vp9') |
| 124 | + self.assertIsInstance(return_value, WebMVP9ImageFile) |
| 125 | + self.assertEqual(return_value.f, output) |
| 126 | + |
| 127 | + def test_save_as_ogg_theora(self): |
| 128 | + output = io.BytesIO() |
| 129 | + return_value = self.image.save_as_ogg_theora(output) |
| 130 | + output.seek(0) |
| 131 | + |
| 132 | + probe_data = probe(output) |
| 133 | + |
| 134 | + self.assertEqual(probe_data['format']['format_name'], 'ogg') |
| 135 | + self.assertEqual(probe_data['streams'][0]['codec_name'], 'theora') |
| 136 | + self.assertIsInstance(return_value, OggTheoraImageFile) |
| 137 | + self.assertEqual(return_value.f, output) |
| 138 | + |
| 139 | + def test_save_as_mp4_h264(self): |
| 140 | + output = io.BytesIO() |
| 141 | + return_value = self. image.save_as_mp4_h264(output) |
| 142 | + output.seek(0) |
| 143 | + |
| 144 | + probe_data = probe(output) |
| 145 | + |
| 146 | + self.assertEqual(probe_data['format']['format_name'], 'mov,mp4,m4a,3gp,3g2,mj2') |
| 147 | + self.assertEqual(probe_data['streams'][0]['codec_name'], 'h264') |
| 148 | + self.assertIsInstance(return_value, MP4H264ImageFile) |
| 149 | + self.assertEqual(return_value.f, output) |
| 150 | + |
| 151 | + def test_has_alpha(self): |
| 152 | + has_alpha = self.image.has_alpha() |
| 153 | + self.assertFalse(has_alpha) |
| 154 | + |
| 155 | + def test_has_animation(self): |
| 156 | + has_animation = self.image.has_animation() |
| 157 | + self.assertTrue(has_animation) |
| 158 | + |
| 159 | + def test_transparent_gif(self): |
| 160 | + with open('tests/images/transparent.gif', 'rb') as f: |
| 161 | + image = FFMpegLazyVideo.open(GIFImageFile(f)) |
| 162 | + |
| 163 | + # Transparency not supported |
| 164 | + self.assertFalse(image.has_alpha()) |
0 commit comments