|
28 | 28 |
|
29 | 29 |
|
30 | 30 | if TYPE_CHECKING: |
| 31 | + from PIL import Image |
31 | 32 | from transformers import PreTrainedTokenizerBase |
32 | 33 | from transformers.configuration_utils import PretrainedConfig |
33 | 34 |
|
@@ -729,3 +730,103 @@ def handle_stop_sequences( |
729 | 730 | if eos is not None and eos not in until: |
730 | 731 | until.append(eos) |
731 | 732 | return until |
| 733 | + |
| 734 | + |
| 735 | +def resize_image( |
| 736 | + image: "Image.Image", |
| 737 | + width: Optional[int] = None, |
| 738 | + height: Optional[int] = None, |
| 739 | + max_dimension: Optional[int] = None, |
| 740 | + keep_aspect_ratio: bool = True, |
| 741 | + resample_filter: Union[int, str] = "Image.BICUBIC", |
| 742 | + min_width: int = 1, |
| 743 | + min_height: int = 1, |
| 744 | +) -> "Image.Image": |
| 745 | + """ |
| 746 | + Resizes a PIL Image object with flexible options. |
| 747 | +
|
| 748 | + Args: |
| 749 | + image: The PIL Image object to resize. |
| 750 | + width: Target width in pixels. |
| 751 | + height: Target height in pixels. |
| 752 | + max_dimension: Maximum size for the longer dimension of the image. |
| 753 | + keep_aspect_ratio: If True (default) and both width and height are provided, |
| 754 | + the image is resized to fit within these dimensions while |
| 755 | + maintaining its aspect ratio. If False, the image is stretched |
| 756 | + to the exact width and height. |
| 757 | + resample_filter: The resampling filter to use for resizing. |
| 758 | + Defaults to Image.BICUBIC. |
| 759 | + min_width: Minimum width for the resized image. Defaults to 1. |
| 760 | + min_height: Minimum height for the resized image. Defaults to 1. |
| 761 | +
|
| 762 | + Returns: |
| 763 | + The resized PIL Image object. If no resize parameters are provided |
| 764 | + or if the image already meets the criteria, the original image is returned. |
| 765 | +
|
| 766 | + Order of precedence for resizing: |
| 767 | + 1. If width AND height are provided: |
| 768 | + - If keep_aspect_ratio is True: Fits image within bounds, preserving aspect ratio. |
| 769 | + - If keep_aspect_ratio is False: Resizes to exact dimensions (may distort). |
| 770 | + 2. Else if only width is provided: Calculates height proportionally. |
| 771 | + 3. Else if only height is provided: Calculates width proportionally. |
| 772 | + 4. Else if max_dimension is provided: Resizes the longest side to max_dimension |
| 773 | + and scales the other side proportionally. |
| 774 | + 5. If none of the above are provided, returns the original image. |
| 775 | + """ |
| 776 | + original_width, original_height = image.size |
| 777 | + |
| 778 | + # If no arguments are provided, return the original image |
| 779 | + if width is None and height is None and max_dimension is None: |
| 780 | + return image |
| 781 | + |
| 782 | + new_width = original_width |
| 783 | + new_height = original_height |
| 784 | + |
| 785 | + if width is not None and height is not None: |
| 786 | + # No resize needed if image is already smaller than target dimensions |
| 787 | + if original_width <= width and original_height <= height: |
| 788 | + return image |
| 789 | + |
| 790 | + if keep_aspect_ratio: |
| 791 | + # Calculate the ratio to fit within the target dimensions |
| 792 | + ratio = min(width / original_width, height / original_height) |
| 793 | + new_width = int(original_width * ratio) |
| 794 | + new_height = int(original_height * ratio) |
| 795 | + else: |
| 796 | + # Stretch to exact dimensions |
| 797 | + new_width = width |
| 798 | + new_height = height |
| 799 | + elif width is not None: |
| 800 | + # No resize needed if width is already smaller |
| 801 | + if original_width <= width: |
| 802 | + return image |
| 803 | + # Calculate height proportionally |
| 804 | + new_width = width |
| 805 | + new_height = int((original_height / original_width) * new_width) |
| 806 | + elif height is not None: |
| 807 | + # No resize needed if height is already smaller |
| 808 | + if original_height <= height: |
| 809 | + return image |
| 810 | + # Calculate width proportionally |
| 811 | + new_height = height |
| 812 | + new_width = int((original_width / original_height) * new_height) |
| 813 | + elif max_dimension is not None: |
| 814 | + # No resize needed if both dimensions are smaller than max_dimension |
| 815 | + if max(original_height, original_width) <= max_dimension: |
| 816 | + return image |
| 817 | + |
| 818 | + if original_width > original_height: |
| 819 | + # Width is the longer side |
| 820 | + new_width = max_dimension |
| 821 | + new_height = int((original_height / original_width) * new_width) |
| 822 | + else: |
| 823 | + # Height is the longer side or sides are equal |
| 824 | + new_height = max_dimension |
| 825 | + new_width = int((original_width / original_height) * new_height) |
| 826 | + |
| 827 | + # Ensure dimensions are at least minimum values |
| 828 | + new_width = max(min_width, new_width) |
| 829 | + new_height = max(min_height, new_height) |
| 830 | + |
| 831 | + # Perform the resize operation with the calculated dimensions |
| 832 | + return image.resize((new_width, new_height), resample_filter) |
0 commit comments