from django.core.exceptions import ValidationError from django.core.files.images import ImageFile, get_image_dimensions from django.utils.translation import gettext_lazy as _ def validate_category_image_dimensions( image: ImageFile, max_width: int | None = None, max_height: int | None = None ) -> None: max_width = max_width or 7680 max_height = max_height or 4320 if image: try: width, height = get_image_dimensions(image.file) except (FileNotFoundError, OSError, ValueError): return if int(width) > max_width or int(height) > max_height: raise ValidationError( _( f"image dimensions should not exceed w{max_width} x h{max_height} pixels" ) )