def optimize_jpg( img: JpegImageFile, tmp_file: Any, quality: Optional[int] = DEFAULT_JPEG_QUALITY) -> JpegImageFile: if not quality: quality = DEFAULT_JPEG_QUALITY elif quality > 95: quality = 95 # 95 is the recommended upper limit on quality for JPEGs in PIL img.save(tmp_file.name, "JPEG", quality=quality, optimize=True, progressive=True) return Image.open(tmp_file.name)
def image_to_binary(image: JpegImageFile) -> ByteString: with io.BytesIO() as output: image.save(output, format="PNG") return output.getvalue()
def save_image_file_locally(image: JpegImageFile, image_name: str) -> None: image_directory = app.config["IMAGE_DIRECTORY"] image_location = os.path.join(image_directory, image_name) image.save(image_location)