import os from PIL import Image from app.core._config import UPLOADS_DIR from app.core.logger import make_log import traceback class ImageContentMixin: def as_jpeg(self) -> str: tempfile_path = os.path.join(UPLOADS_DIR, f"temporary_{self.hash}.jpeg") if not os.path.exists(tempfile_path): cover_image = Image.open(self.filepath) cover_image = cover_image.convert('RGB') quality = 95 while quality > 10: cover_image.save(tempfile_path, 'JPEG', quality=quality) if os.path.getsize(tempfile_path) <= 200 * 1024: break quality -= 5 assert os.path.exists(tempfile_path), "Image not found" return tempfile_path