import tempfile from pathlib import Path import unittest from PIL import Image from app.transform import transform class ImageRules(unittest.TestCase): def test_small_transparent_source(self): with tempfile.TemporaryDirectory() as d: src=Path(d)/'input.png' Image.new('RGBA',(16,8),(255,0,0,0)).save(src) rows=transform(src,'image/png',d) self.assertEqual([(v['width'],v['height']) for v in rows],[(16,8),(128,128),(16,8)]) with Image.open(Path(d)/'thumbnail') as im: self.assertGreater(min(im.getpixel((0,0))),250) def test_exif_orientation_and_metadata(self): with tempfile.TemporaryDirectory() as d: src=Path(d)/'input.jpg' exif=Image.Exif();exif[274]=6;exif[315]='remove this author' Image.new('RGB',(320,200),'blue').save(src,exif=exif) rows=transform(src,'image/jpeg',d) self.assertEqual((rows[0]['width'],rows[0]['height']),(160,256)) for name in ('thumbnail','crop','optimized'): with Image.open(Path(d)/name) as im:self.assertFalse(im.getexif()) if __name__=='__main__':unittest.main()