summary refs log tree commit diff
path: root/synapse/media/v1/thumbnailer.py
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2014-12-10 14:46:55 +0000
committerMark Haines <mark.haines@matrix.org>2014-12-10 15:40:52 +0000
commitcc84d3ea78eaf50c20ad84b3df99ecf4547e08a8 (patch)
tree6099f833203e419f84a889a792120588657ffa7e /synapse/media/v1/thumbnailer.py
parentAdd a method field to thumbnail storage (diff)
downloadsynapse-cc84d3ea78eaf50c20ad84b3df99ecf4547e08a8.tar.xz
Thumbnail uploaded and cached images
Diffstat (limited to 'synapse/media/v1/thumbnailer.py')
-rw-r--r--synapse/media/v1/thumbnailer.py37
1 files changed, 24 insertions, 13 deletions
diff --git a/synapse/media/v1/thumbnailer.py b/synapse/media/v1/thumbnailer.py
index ed09283b27..47160721e7 100644
--- a/synapse/media/v1/thumbnailer.py
+++ b/synapse/media/v1/thumbnailer.py
@@ -13,18 +13,22 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import PIL.Image
+import Image
+from io import BytesIO
+
 
 class Thumbnailer(object):
 
-    FORMAT_JPEG="JPEG"
-    FORMAT_PNG="PNG"
+    FORMATS = {
+        "image/jpeg": "JPEG",
+        "image/png": "PNG",
+    }
 
     def __init__(self, input_path):
-        self.image = PIL.Image.open(input_path)
+        self.image = Image.open(input_path)
         self.width, self.height = self.image.size
 
-    def size_preserve(self, max_width, max_height):
+    def aspect(self, max_width, max_height):
         """Calculate the largest size that preserves aspect ratio which
         fits within the given rectangle::
 
@@ -42,12 +46,12 @@ class Thumbnailer(object):
         else:
             return ((max_height * self.width) // self.height, max_height)
 
-    def thumbnail_scale(self, output_path, output_format, width, height):
+    def scale(self, output_path, width, height, output_type):
         """Rescales the image to the given dimensions"""
-        output = self.image.resize((width, height), PIL.Image.BILINEAR)
-        output.save(output_path, output_format)
+        scaled = self.image.resize((width, height), Image.BILINEAR)
+        return self.save_image(scaled, output_type, output_path)
 
-    def thumbnail_crop(self, output_path, output_format, width, height):
+    def crop(self, output_path, width, height, output_type):
         """Rescales and crops the image to the given dimensions preserving
         aspect::
             (w_in / h_in) = (w_scaled / h_scaled)
@@ -61,18 +65,25 @@ class Thumbnailer(object):
         if width * self.height > height * self.width:
             scaled_height = (width * self.height) // self.width
             scaled_image = self.image.resize(
-                (width, scaled_height), PIL.Image.BILINEAR
+                (width, scaled_height), Image.BILINEAR
             )
             crop_top = (scaled_height - height) // 2
             crop_bottom = height + crop_top
             cropped = scaled_image.crop((0, crop_top, width, crop_bottom))
-            cropped.save(output_path, output_format)
         else:
             scaled_width = (height * self.width) // self.height
             scaled_image = self.image.resize(
-                (scaled_width, height), PIL.Image.BILINEAR
+                (scaled_width, height), Image.BILINEAR
             )
             crop_left = (scaled_width - width) // 2
             crop_right = width + crop_left
             cropped = scaled_image.crop((crop_left, 0, crop_right, height))
-            cropped.save(output_path, output_format)
+        return self.save_image(cropped, output_type, output_path)
+
+    def save_image(self, output_image, output_type, output_path):
+        output_bytes_io = BytesIO()
+        output_image.save(output_bytes_io, self.FORMATS[output_type])
+        output_bytes = output_bytes_io.getvalue()
+        with open(output_path, "wb") as output_file:
+            output_file.write(output_bytes)
+        return len(output_bytes)