Resize ``img`` to fit within ``max_w × max_h`` preserving aspect ratio. Returns ``(resized_img, new_w, new_h)``. No-op when image already fits. Matches sharp's ``fit: 'inside', withoutEnlargement: true``.
(img: Any, max_w: int, max_h: int)
| 219 | |
| 220 | |
| 221 | def _resize_to_envelope(img: Any, max_w: int, max_h: int) -> tuple[Any, int, int]: |
| 222 | """Resize ``img`` to fit within ``max_w × max_h`` preserving aspect ratio. |
| 223 | |
| 224 | Returns ``(resized_img, new_w, new_h)``. No-op when image already fits. |
| 225 | Matches sharp's ``fit: 'inside', withoutEnlargement: true``. |
| 226 | """ |
| 227 | Image, _ = _pil() |
| 228 | w, h = img.size |
| 229 | if w <= max_w and h <= max_h: |
| 230 | return img, w, h |
| 231 | scale = min(max_w / w, max_h / h) |
| 232 | new_w = max(1, int(w * scale)) |
| 233 | new_h = max(1, int(h * scale)) |
| 234 | resized = img.resize((new_w, new_h), Image.LANCZOS) |
| 235 | return resized, new_w, new_h |
| 236 | |
| 237 | |
| 238 | # --------------------------------------------------------------------------- |
no test coverage detected