| 9 | |
| 10 | |
| 11 | class PadToSquare: |
| 12 | def __init__(self, background_color:Tuple[float, float, float]): |
| 13 | """ |
| 14 | pad an image to squre (borrowed from LLAVA, thx) |
| 15 | :param background_color: rgb values for padded pixels, normalized to [0, 1] |
| 16 | """ |
| 17 | self.bg_color = tuple(int(x*255) for x in background_color) |
| 18 | |
| 19 | def __call__(self, img: Image.Image): |
| 20 | width, height = img.size |
| 21 | if width == height: |
| 22 | return img |
| 23 | elif width > height: |
| 24 | result = Image.new(img.mode, (width, width), self.bg_color) |
| 25 | result.paste(img, (0, (width - height) // 2)) |
| 26 | return result |
| 27 | else: |
| 28 | result = Image.new(img.mode, (height, height), self.bg_color) |
| 29 | result.paste(img, ((height - width) // 2, 0)) |
| 30 | return result |
| 31 | |
| 32 | def __repr__(self) -> str: |
| 33 | format_string = self.__class__.__name__ + f"(bg_color={self.bg_color})" |
| 34 | return format_string |
| 35 | |
| 36 | |
| 37 | def T_random_resized_crop(size=224): |