| 630 | |
| 631 | |
| 632 | class RandomSizedEarser(object): |
| 633 | def __init__(self, sl=0.02, sh=0.2, asratio=0.3, p=0.8): |
| 634 | self.sl = sl |
| 635 | self.sh = sh |
| 636 | self.asratio = asratio |
| 637 | self.p = p |
| 638 | |
| 639 | def __call__(self, img): |
| 640 | p1 = random.uniform(-1, 1.0) |
| 641 | W = img.size[0] |
| 642 | H = img.size[1] |
| 643 | area = H * W |
| 644 | |
| 645 | if p1 > self.p: |
| 646 | return img |
| 647 | else: |
| 648 | gen = True |
| 649 | while gen: |
| 650 | Se = random.uniform(self.sl, self.sh)*area |
| 651 | re = random.uniform(self.asratio, 1/self.asratio) |
| 652 | He = np.sqrt(Se*re) |
| 653 | We = np.sqrt(Se/re) |
| 654 | xe = random.uniform(0, W-We) |
| 655 | ye = random.uniform(0, H-He) |
| 656 | if xe+We <= W and ye+He <= H and xe>0 and ye>0: |
| 657 | x1 = int(np.ceil(xe)) |
| 658 | y1 = int(np.ceil(ye)) |
| 659 | x2 = int(np.floor(x1+We)) |
| 660 | y2 = int(np.floor(y1+He)) |
| 661 | part1 = img.crop((x1, y1, x2, y2)) |
| 662 | Rc = random.randint(0, 255) |
| 663 | Gc = random.randint(0, 255) |
| 664 | Bc = random.randint(0, 255) |
| 665 | I = Image.new('RGB', part1.size, (Rc, Gc, Bc)) |
| 666 | img.paste(I, (x1, y1)) |
| 667 | return img |
| 668 | |
| 669 | class RandomSizedEarserCV2(object): |
| 670 | def __init__(self, sl=0.02, sh=0.2, asratio=0.3, p=0.8): |