thumbnailer.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import PIL.Image as Image
  16. from io import BytesIO
  17. class Thumbnailer(object):
  18. FORMATS = {
  19. "image/jpeg": "JPEG",
  20. "image/png": "PNG",
  21. }
  22. def __init__(self, input_path):
  23. self.image = Image.open(input_path)
  24. self.width, self.height = self.image.size
  25. def aspect(self, max_width, max_height):
  26. """Calculate the largest size that preserves aspect ratio which
  27. fits within the given rectangle::
  28. (w_in / h_in) = (w_out / h_out)
  29. w_out = min(w_max, h_max * (w_in / h_in))
  30. h_out = min(h_max, w_max * (h_in / w_in))
  31. Args:
  32. max_width: The largest possible width.
  33. max_height: The larget possible height.
  34. """
  35. if max_width * self.height < max_height * self.width:
  36. return (max_width, (max_width * self.height) // self.width)
  37. else:
  38. return ((max_height * self.width) // self.height, max_height)
  39. def scale(self, output_path, width, height, output_type):
  40. """Rescales the image to the given dimensions"""
  41. scaled = self.image.resize((width, height), Image.BILINEAR)
  42. return self.save_image(scaled, output_type, output_path)
  43. def crop(self, output_path, width, height, output_type):
  44. """Rescales and crops the image to the given dimensions preserving
  45. aspect::
  46. (w_in / h_in) = (w_scaled / h_scaled)
  47. w_scaled = max(w_out, h_out * (w_in / h_in))
  48. h_scaled = max(h_out, w_out * (h_in / w_in))
  49. Args:
  50. max_width: The largest possible width.
  51. max_height: The larget possible height.
  52. """
  53. if width * self.height > height * self.width:
  54. scaled_height = (width * self.height) // self.width
  55. scaled_image = self.image.resize(
  56. (width, scaled_height), Image.BILINEAR
  57. )
  58. crop_top = (scaled_height - height) // 2
  59. crop_bottom = height + crop_top
  60. cropped = scaled_image.crop((0, crop_top, width, crop_bottom))
  61. else:
  62. scaled_width = (height * self.width) // self.height
  63. scaled_image = self.image.resize(
  64. (scaled_width, height), Image.BILINEAR
  65. )
  66. crop_left = (scaled_width - width) // 2
  67. crop_right = width + crop_left
  68. cropped = scaled_image.crop((crop_left, 0, crop_right, height))
  69. return self.save_image(cropped, output_type, output_path)
  70. def save_image(self, output_image, output_type, output_path):
  71. output_bytes_io = BytesIO()
  72. output_image.save(output_bytes_io, self.FORMATS[output_type])
  73. output_bytes = output_bytes_io.getvalue()
  74. with open(output_path, "wb") as output_file:
  75. output_file.write(output_bytes)
  76. return len(output_bytes)