def transform(self, pil_img, label, **kwargs): pil_img = pil_img.copy() degree = categorize_value(self.level, self.value_range, "int") width, height = pil_img.size x0 = np.random.uniform(width) y0 = np.random.uniform(height) x0 = int(max(0, x0 - degree / 2.0)) y0 = int(max(0, y0 - degree / 2.0)) x1 = min(width, x0 + degree) y1 = min(height, y0 + degree) xy = (x0, y0, x1, y1) if self.color is not None: color = self.color elif pil_img.mode == "RGB": color = (125, 123, 114) elif pil_img.mode == "L": color = 121 else: raise ValueError(f"Unspported image mode {pil_img.mode}") ImageDraw.Draw(pil_img).rectangle(xy, color) return pil_img, label
def transform(self, pil_img, label, **kwargs): degree = categorize_value(self.level, self.value_range, "float") if random.random() > 0.5: degree = -degree return ( pil_img.transform(pil_img.size, Image.AFFINE, (1, 0, 0, degree, 1, 0)), label, )
def transform(self, pil_img, label, **kwargs): degree = categorize_value(self.level, self.value_range, "int") return ImageOps.posterize(pil_img, degree), label
def transform(self, pil_img, label, **kwargs): degree = categorize_value(self.level, self.value_range, "float") return ImageEnhance.Color(pil_img).enhance(degree), label