diff options
author | Andrew Morgan <andrew@amorgan.xyz> | 2020-02-26 12:19:51 +0000 |
---|---|---|
committer | Andrew Morgan <andrew@amorgan.xyz> | 2020-02-26 12:19:51 +0000 |
commit | d0ddfb50adb96d4b5aeb91f646564d057911a7c5 (patch) | |
tree | 643a7d1d09e64afaa6e2396d202db18c9eb6a8bc /synapse | |
parent | add some metrics on the federation sender (#6160) (diff) | |
parent | media/thumbnailer: Better quality for 1-bit / 8-bit color palette images (#2142) (diff) | |
download | synapse-d0ddfb50adb96d4b5aeb91f646564d057911a7c5.tar.xz |
media/thumbnailer: Better quality for 1-bit / 8-bit color palette images (#2142)
Diffstat (limited to 'synapse')
-rw-r--r-- | synapse/rest/media/v1/thumbnailer.py | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/synapse/rest/media/v1/thumbnailer.py b/synapse/rest/media/v1/thumbnailer.py index c995d7e043..8cf415e29d 100644 --- a/synapse/rest/media/v1/thumbnailer.py +++ b/synapse/rest/media/v1/thumbnailer.py @@ -82,13 +82,21 @@ class Thumbnailer(object): else: return (max_height * self.width) // self.height, max_height + def _resize(self, width, height): + # 1-bit or 8-bit color palette images need converting to RGB + # otherwise they will be scaled using nearest neighbour which + # looks awful + if self.image.mode in ["1", "P"]: + self.image = self.image.convert("RGB") + return self.image.resize((width, height), Image.ANTIALIAS) + def scale(self, width, height, output_type): """Rescales the image to the given dimensions. Returns: BytesIO: the bytes of the encoded image ready to be written to disk """ - scaled = self.image.resize((width, height), Image.ANTIALIAS) + scaled = self._resize(width, height) return self._encode_image(scaled, output_type) def crop(self, width, height, output_type): @@ -107,13 +115,13 @@ 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), Image.ANTIALIAS) + scaled_image = self._resize(width, scaled_height) crop_top = (scaled_height - height) // 2 crop_bottom = height + crop_top cropped = scaled_image.crop((0, crop_top, width, crop_bottom)) else: scaled_width = (height * self.width) // self.height - scaled_image = self.image.resize((scaled_width, height), Image.ANTIALIAS) + scaled_image = self._resize(scaled_width, height) crop_left = (scaled_width - width) // 2 crop_right = width + crop_left cropped = scaled_image.crop((crop_left, 0, crop_right, height)) |