|
| 1 | +# This software is open source software available under the BSD-3 license. |
| 2 | +# |
| 3 | +# Copyright (c) 2022 Triad National Security, LLC. All rights reserved. |
| 4 | +# Copyright (c) 2022 Lawrence Livermore National Security, LLC. All rights |
| 5 | +# reserved. |
| 6 | +# Copyright (c) 2022 UT-Battelle, LLC. All rights reserved. |
| 7 | +# |
| 8 | +# Additional copyright and license information can be found in the LICENSE file |
| 9 | +# distributed with this code, or at |
| 10 | +# https://raw.githubusercontent.com/MPAS-Dev/MPAS-Analysis/main/LICENSE |
| 11 | + |
| 12 | +import os |
| 13 | +import tempfile |
| 14 | +import unittest |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +from PIL import Image |
| 18 | + |
| 19 | + |
| 20 | +# Import the function directly to avoid mpas_analysis.test dependencies |
| 21 | +import sys |
| 22 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..')) |
| 23 | +from mpas_analysis.shared.html.image_xml import _generate_thumbnails |
| 24 | + |
| 25 | + |
| 26 | +class TestThumbnailGeneration(unittest.TestCase): |
| 27 | + """Test thumbnail generation with Pillow""" |
| 28 | + |
| 29 | + def test_generate_thumbnails_horizontal(self): |
| 30 | + """Test thumbnail generation for horizontal images""" |
| 31 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 32 | + # Create a horizontal test image |
| 33 | + test_image = Image.new('RGB', (1200, 600), color='blue') |
| 34 | + image_filename = 'test_horizontal.png' |
| 35 | + image_path = Path(tmpdir) / image_filename |
| 36 | + test_image.save(image_path) |
| 37 | + |
| 38 | + # Generate thumbnails |
| 39 | + imageSize, thumbnailSize, orientation = _generate_thumbnails( |
| 40 | + image_filename, tmpdir |
| 41 | + ) |
| 42 | + |
| 43 | + # Verify results |
| 44 | + self.assertEqual(imageSize, (1200, 600)) |
| 45 | + self.assertEqual(orientation, 'horiz') |
| 46 | + self.assertEqual(thumbnailSize[1], 120) # height should be 120 |
| 47 | + |
| 48 | + # Check thumbnail files exist |
| 49 | + thumbnail_dir = Path(tmpdir) / 'thumbnails' |
| 50 | + self.assertTrue(thumbnail_dir.exists()) |
| 51 | + self.assertTrue((thumbnail_dir / 'test_horizontal.jpg').exists()) |
| 52 | + self.assertTrue((thumbnail_dir / 'fixed_test_horizontal.jpg').exists()) |
| 53 | + |
| 54 | + def test_generate_thumbnails_vertical(self): |
| 55 | + """Test thumbnail generation for vertical images""" |
| 56 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 57 | + # Create a vertical test image |
| 58 | + test_image = Image.new('RGB', (400, 800), color='green') |
| 59 | + image_filename = 'test_vertical.png' |
| 60 | + image_path = Path(tmpdir) / image_filename |
| 61 | + test_image.save(image_path) |
| 62 | + |
| 63 | + # Generate thumbnails |
| 64 | + imageSize, thumbnailSize, orientation = _generate_thumbnails( |
| 65 | + image_filename, tmpdir |
| 66 | + ) |
| 67 | + |
| 68 | + # Verify results |
| 69 | + self.assertEqual(imageSize, (400, 800)) |
| 70 | + self.assertEqual(orientation, 'vert') |
| 71 | + self.assertEqual(thumbnailSize[1], 320) # height should be 320 |
| 72 | + |
| 73 | + # Check thumbnail files exist |
| 74 | + thumbnail_dir = Path(tmpdir) / 'thumbnails' |
| 75 | + self.assertTrue(thumbnail_dir.exists()) |
| 76 | + self.assertTrue((thumbnail_dir / 'test_vertical.jpg').exists()) |
| 77 | + self.assertTrue((thumbnail_dir / 'fixed_test_vertical.jpg').exists()) |
| 78 | + |
| 79 | + def test_image_lanczos_constant(self): |
| 80 | + """Test that Image.LANCZOS constant is available""" |
| 81 | + # This test ensures that Image.LANCZOS is available across |
| 82 | + # Pillow versions 10.x, 11.x, and 12.x |
| 83 | + self.assertTrue(hasattr(Image, 'LANCZOS')) |
| 84 | + self.assertIsNotNone(Image.LANCZOS) |
| 85 | + |
| 86 | + # Test that resize works with LANCZOS |
| 87 | + test_image = Image.new('RGB', (100, 100), color='red') |
| 88 | + resized = test_image.resize((50, 50), Image.LANCZOS) |
| 89 | + self.assertEqual(resized.size, (50, 50)) |
0 commit comments