Last active
February 6, 2026 11:07
-
-
Save phw/2b1354cb85011354a59ae933e81cde4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| from enum import Enum | |
| @dataclass | |
| class ImageFormatInfo: | |
| name: str | |
| mime: str | |
| extensions: tuple[str, ...] | |
| aliases: tuple[str, ...] | None = None | |
| can_convert: bool = True | |
| use_quality: bool = False | |
| class ImageFormat(Enum): | |
| JPEG = "jpeg" | |
| PNG = "png" | |
| WEBP = "webp" | |
| @property | |
| def title(self): | |
| return _INFO_MAPPING[self].name | |
| @property | |
| def aliases(self) -> tuple[str, ...]: | |
| aliases = _INFO_MAPPING[self].aliases or tuple() | |
| if self.value not in aliases: | |
| aliases = (self.value,) + aliases | |
| return aliases | |
| @property | |
| def extensions(self) -> tuple[str, ...]: | |
| return _INFO_MAPPING[self].extensions | |
| _INFO_MAPPING = { | |
| ImageFormat.JPEG: ImageFormatInfo( | |
| name="JPEG", | |
| mime="image/jpeg", | |
| aliases=("jpg",), | |
| extensions=(".jpg", ".jpeg"), | |
| use_quality=True, | |
| ), | |
| ImageFormat.PNG: ImageFormatInfo( | |
| name="PNG", | |
| mime="image/png", | |
| extensions=(".png",), | |
| ), | |
| ImageFormat.WEBP: ImageFormatInfo( | |
| name="WebP", | |
| mime="image/webp", | |
| extensions=(".webp",), | |
| use_quality=True, | |
| ), | |
| } | |
| v = ImageFormat.JPEG | |
| print(v) | |
| print(v.title) | |
| print(v.aliases) | |
| print(v.extensions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment