def dimensions_and_resize(max_dim: int, vault: Vault, sub_path: str, image_to_write: Bean) -> Optional[str]: """ Get dimensions from given image, return a string with the error in case of issue. It is assumed that the image is valid, i.e. did not throw an exception in above validate() """ im = PIL_Image.open(vault.path_to(sub_path)) image_to_write.width = im.size[0] image_to_write.height = im.size[1] # Generate a thumbnail if image is too large if (im.size[0] > max_dim) or (im.size[1] > max_dim): if im.mode == 'P' or im.mode[0] == 'I': # (8-bit pixels, mapped to any other mode using a color palette) # from https://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes # Tested using a PNG with palette im = im.convert("RGB") im.thumbnail((max_dim, max_dim)) thumb_relative_path, thumb_full_path = vault.thumbnail_paths(image_to_write.imgid) im.save(thumb_full_path) image_to_write.thumb_file_name = thumb_relative_path image_to_write.thumb_width = im.size[0] image_to_write.thumb_height = im.size[1] else: # Close the PIL image, when resized it was done during im.save # Otherwise there is a FD exhaustion on PyPy im.close() # Need empty fields for bulk insert image_to_write.thumb_file_name = None image_to_write.thumb_width = None image_to_write.thumb_height = None return None
def dimensions_and_resize(max_dim: int, vault: Vault, sub_path: str, image_to_write: Bean) -> Optional[str]: try: im = PIL_Image.open(vault.path_to(sub_path)) except DecompressionBombError: return "Image too large: %s" % sub_path image_to_write.width = im.size[0] image_to_write.height = im.size[1] # Generate a thumbnail if image is too large if (im.size[0] > max_dim) or (im.size[1] > max_dim): im.thumbnail((max_dim, max_dim)) if im.mode == 'P': # (8-bit pixels, mapped to any other mode using a color palette) # from https://pillow.readthedocs.io/en/latest/handbook/concepts.html#modes # Tested using a PNG with palette im = im.convert("RGB") thumb_relative_path, thumb_full_path = vault.thumbnail_paths( image_to_write.imgid) im.save(thumb_full_path) image_to_write.thumb_file_name = thumb_relative_path image_to_write.thumb_width = im.size[0] image_to_write.thumb_height = im.size[1] else: # Close the PIL image, when resized it was done during im.save # Otherwise there is a FD exhaustion on PyPy im.close() # Need empty fields for bulk insert image_to_write.thumb_file_name = None image_to_write.thumb_width = None image_to_write.thumb_height = None return None