One_Piece_Bounty_Bot / src /utils /image_utils.py
github-actions[bot]
Automated sync from GitHub
71e71e8
Raw
History Blame Contribute Delete
837 Bytes
import os
import validators
from PIL import Image
from src.utils.download_utils import download_temp_file
def compress_image(path: str, quality: int) -> str:
"""
Reduces an image to the required max file size
:param path: Path to the original (unchanged) image
:type path: str
:param quality: Quality of the compressed image (0-100)
:type quality: int
:return: an image path to the limited image.
:rtype: str
"""
# Download image if path is an url
if validators.url(path):
path = download_temp_file(path)
with Image.open(path) as image:
compressed_image_path = os.path.join(
os.path.dirname(path), f"compressed_{os.path.basename(path)}"
)
image.save(compressed_image_path, quality=quality, optimize=True)
return compressed_image_path