Repository: ui/da-vinci (language: python)

Issue:
Add webp support



Discussion / hints:

Fix patch:
diff --git a/da_vinci/formats.py b/da_vinci/formats.py
index 3acec73..b1dd2f8 100644
--- a/da_vinci/formats.py
+++ b/da_vinci/formats.py
@@ -8,6 +8,7 @@
     'pdf': 'PDF',
     'tif': 'TIFF',
     'tiff': 'TIFF',
+    'webp': 'WEBP'
 }
 
 EXTENSIONS = {
@@ -16,5 +17,6 @@
     'PNG': 'png',
     'PDF': 'pdf',
     'TIFF': 'tif',
-    'BMP': 'bmp'
+    'BMP': 'bmp',
+    'WEBP': 'webp'
 }


Test patch:
diff --git a/tests/image.webp b/tests/image.webp
new file mode 100644
index 0000000..07c1809
Binary files /dev/null and b/tests/image.webp differ
diff --git a/tests/tests.py b/tests/tests.py
index 5cfe00d..0e97e51 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -179,6 +179,27 @@ def test_save(self):
         self.assertTrue(os.path.exists(new_filename))
         os.remove(new_filename)
 
+        # Ensure webp image can saved
+        image = images.from_file('tests/image.webp')
+        self.assertEqual(image._format, 'WEBP')
+        new_filename = 'tests/save.webp'
+        image.save(filename=new_filename)
+        self.assertEqual(image.filename, new_filename)
+        self.assertTrue(os.path.exists(new_filename))
+        os.remove(new_filename)
+
+        # Ensure webp image can be saved to a file like object
+        file_like_object = io.BytesIO()
+        self.assertFalse(file_like_object.getvalue())
+        image.save(file=file_like_object)
+        self.assertTrue(file_like_object.getvalue())
+
+        new_filename = 'tests/save2.webp'
+        image.filename = new_filename
+        image.save()
+        self.assertTrue(os.path.exists(new_filename))
+        os.remove(new_filename)
+
     def test_rotate(self):
         image = images.from_file('tests/10x20.jpg')
         self.assertEqual(image.width, 10)
@@ -196,6 +217,8 @@ def test_format_getter_setter(self):
         self.assertEqual(image.format, 'JPEG')
         image.format = 'png'
         self.assertEqual(image.format, 'PNG')
+        image.format = "webp"
+        self.assertEqual(image.format, 'WEBP')
 
     def test_get_filename(self):
         # get_filename return its original filename if there's no changes
@@ -207,6 +230,13 @@ def test_get_filename(self):
         # get_filename falls back to self.name
         image.filename = None
         self.assertEqual(image.get_filename(), '10x20.png')
+        # get_filename returns the right filename if format is changed (webp)
+        image = images.from_file('tests/10x20.jpg')
+        image.format = 'webp'
+        self.assertEqual(image.get_filename(), 'tests/10x20.webp')
+        # get_filename falls back to self.name (webp)
+        image.filename = None
+        self.assertEqual(image.get_filename(), '10x20.webp')
 
     def test_quality(self):
         # Setting quality parameter shouldn't mess up image.save
@@ -219,3 +249,8 @@ def test_quality(self):
         image.format = 'png'
         image.save(new_filename)
         os.remove(image.filename)
+
+        # Ensure webp can saved
+        image.format = 'webp'
+        image.save(new_filename)
+        os.remove(image.filename)