Skip to content

Commit

Permalink
feat: Change OpenAI Image Format to JPG (#4117)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-genson authored Aug 30, 2024
1 parent 2ad6e1b commit a3f474e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
39 changes: 30 additions & 9 deletions mealie/pkgs/img/minify.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@
register_heif_opener()
register_avif_opener()

WEBP = ".webp"
FORMAT = "WEBP"

@dataclass
class ImageFormat:
suffix: str
format: str
modes: list[str]
"""If the image is not in the correct mode, it will be converted to the first mode in the list"""


JPG = ImageFormat(".jpg", "JPEG", ["RGB"])
WEBP = ImageFormat(".webp", "WEBP", ["RGB", "RGBA"])
IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".heic", ".avif"}


Expand Down Expand Up @@ -57,24 +65,37 @@ def purge(self, image: Path):
return

for file in image.parent.glob("*.*"):
if file.suffix != WEBP:
if file.suffix != WEBP.suffix:
file.unlink()


class PillowMinifier(ABCMinifier):
@staticmethod
def to_webp(image_file: Path, dest: Path | None = None, quality: int = 100) -> Path:
def _convert_image(
image_file: Path, image_format: ImageFormat, dest: Path | None = None, quality: int = 100
) -> Path:
"""
Converts an image to the webp format in-place. The original image is not
removed By default, the quality is set to 100.
Converts an image to the specified format in-place. The original image is not
removed. By default, the quality is set to 100.
"""

img = Image.open(image_file)
if img.mode not in image_format.modes:
img = img.convert(image_format.modes[0])

dest = dest or image_file.with_suffix(WEBP)
img.save(dest, FORMAT, quality=quality)
dest = dest or image_file.with_suffix(image_format.suffix)
img.save(dest, image_format.format, quality=quality)

return dest

@staticmethod
def to_jpg(image_file: Path, dest: Path | None = None, quality: int = 100) -> Path:
return PillowMinifier._convert_image(image_file, JPG, dest, quality)

@staticmethod
def to_webp(image_file: Path, dest: Path | None = None, quality: int = 100) -> Path:
return PillowMinifier._convert_image(image_file, WEBP, dest, quality)

@staticmethod
def crop_center(pil_img: Image, crop_width=300, crop_height=300):
img_width, img_height = pil_img.size
Expand Down Expand Up @@ -122,7 +143,7 @@ def minify(self, image_file: Path, force=True):
else:
img = Image.open(image_file)
tiny_image = PillowMinifier.crop_center(img)
tiny_image.save(tiny_dest, FORMAT, quality=70)
tiny_image.save(tiny_dest, WEBP.format, quality=70)
self._logger.info("Tiny image saved")
success = True

Expand Down
6 changes: 3 additions & 3 deletions mealie/services/openai/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class OpenAILocalImage(OpenAIImageBase):
path: Path

def get_image_url(self) -> str:
image = img.PillowMinifier.to_webp(
self.path, dest=self.path.parent.joinpath(f"{self.filename}-min-original.webp")
image = img.PillowMinifier.to_jpg(
self.path, dest=self.path.parent.joinpath(f"{self.filename}-min-original.jpg")
)
with open(image, "rb") as f:
b64content = base64.b64encode(f.read()).decode("utf-8")
return f"data:image/webp;base64,{b64content}"
return f"data:image/jpg;base64,{b64content}"


class OpenAIService(BaseService):
Expand Down

0 comments on commit a3f474e

Please sign in to comment.