Spaces:
Sleeping
Sleeping
| from PIL import Image | |
| import io | |
| def compress_image(image, quality=85): | |
| """ | |
| Komprimiert ein Bild mit angegebener Qualität | |
| :param image: PIL Image Objekt | |
| :param quality: Kompressionsqualität (1-100) | |
| :return: Komprimiertes PIL Image | |
| """ | |
| img_byte_arr = io.BytesIO() | |
| image.save(img_byte_arr, format='JPEG', quality=quality) | |
| img_byte_arr.seek(0) | |
| return Image.open(img_byte_arr) | |
| def convert_image_format(image, target_format='webp', quality=85): | |
| """ | |
| Konvertiert Bild in Zielformat | |
| :param image: PIL Image Objekt | |
| :param target_format: Zielformat (jpg, png, webp) | |
| :param quality: Kompressionsqualität | |
| :return: Konvertiertes PIL Image | |
| """ | |
| img_byte_arr = io.BytesIO() | |
| image.save(img_byte_arr, format=target_format.upper(), quality=quality) | |
| img_byte_arr.seek(0) | |
| return Image.open(img_byte_arr) | |
| def resize_image(image, max_width=None, max_height=None): | |
| """ | |
| Ändert Bildgröße unter Beibehaltung des Seitenverhältnisses | |
| :param image: PIL Image Objekt | |
| :param max_width: Maximale Breite | |
| :param max_height: Maximale Höhe | |
| :return: Größenverändertes PIL Image | |
| """ | |
| if max_width and max_height: | |
| image.thumbnail((max_width, max_height)) | |
| return image |