Esempio n. 1
0
    def tranfun(self, image_and_loc):
        image, left, top, right, bottom = image_and_loc
        w, h = image.size
        
        left = np.clip(left, 0, w - 1)
        right = np.clip(right, 0, w - 1)
        top = np.clip(top, 0, h - 1)
        bottom = np.clip(bottom, 0, h - 1)
        img = trans_utils.getcvimage(image)
        
        try:
            # global index
            res = getpilimage(img[top:bottom, left:right])
            # res.save('test_imgs/crop-debug-{}.jpg'.format(index))
            # index += 1
            return res
        
        except AttributeError as e:
            print('error')
            image.save('test_imgs/t.png')
            print( left, top, right, bottom)

        h = bottom - top
        w = right - left
        org = np.array([[left - np.random.randint(0, self.maxv_w), top + np.random.randint(-self.maxv_h, self.maxv_h // 2)],
                        [right + np.random.randint(0, self.maxv_w), top + np.random.randint(-self.maxv_h, self.maxv_h // 2)],
                        [left - np.random.randint(0, self.maxv_w), bottom - np.random.randint(-self.maxv_h, self.maxv_h // 2)],
                        [right + np.random.randint(0, self.maxv_w), bottom - np.random.randint(-self.maxv_h, self.maxv_h // 2)]], np.float32)
        dst = np.array([[0, 0], [w, 0], [0, h], [w, h]], np.float32)
        M = cv2.getPerspectiveTransform(org, dst)
        res = cv2.warpPerspective(img, M, (w, h))
        return getpilimage(res)
Esempio n. 2
0
 def tranfun(self, image):
     img = trans_utils.getcvimage(image)
     param = [int(cv2.IMWRITE_JPEG_QUALITY), random.randint(self.lower, self.upper)]
     img_encode = cv2.imencode('.jpeg', img, param)
     img_decode = cv2.imdecode(img_encode[1], cv2.IMREAD_COLOR)
     pil_img = trans_utils.cv2pil(img_decode)
     
     if len(image.split())==1:
         pil_img = pil_img.convert('L')
     return pil_img
Esempio n. 3
0
 def tranfun(self, image):
     img = trans_utils.getcvimage(image)
     h,w = img.shape[:2]
     
     org = np.array([[0, np.random.randint(0, self.maxv)], [w, np.random.randint(0, self.maxv)],
                     [0, h - np.random.randint(0, self.maxv)], [w, h - np.random.randint(0, self.maxv)]], np.float32)
     dst = np.array([[0, 0], [w, 0], [0, h], [w, h]], np.float32)
     
     M = cv2.getPerspectiveTransform(org, dst)
     res = cv2.warpPerspective(img, M, (w, h))
     return getpilimage(res)
Esempio n. 4
0
 def tranfun(self, image):
     image = trans_utils.getcvimage(image)
     h, w = image.shape[:2]
     x0 = random.randint(0, w)
     y0 = random.randint(0, h)
     x1 = random.randint(x0, w)
     y1 = random.randint(y0, h)
     transparent_area = (x0, y0, x1, y1)
     mask = Image.new('L', (w, h), color=255)
     draw = ImageDraw.Draw(mask)
     mask = np.array(mask)
     if len(image.shape) == 3:
         mask = mask[:, :, np.newaxis]
         mask = np.concatenate([mask, mask, mask], axis=2)
     draw.rectangle(transparent_area, fill=random.randint(150, 255))
     reflection_result = image + (255 - mask)
     reflection_result = np.clip(reflection_result, 0, 255)
     return trans_utils.cv2pil(reflection_result)