예제 #1
0
def test_sanity():
    transform = ImageTransform.AffineTransform(seq[:6])
    assert_no_exception(lambda: im.transform((100, 100), transform))
    transform = ImageTransform.ExtentTransform(seq[:4])
    assert_no_exception(lambda: im.transform((100, 100), transform))
    transform = ImageTransform.QuadTransform(seq[:8])
    assert_no_exception(lambda: im.transform((100, 100), transform))
    transform = ImageTransform.MeshTransform([(seq[:4], seq[:8])])
    assert_no_exception(lambda: im.transform((100, 100), transform))
예제 #2
0
    def test_sanity(self):
        im = Image.new("L", (100, 100))

        seq = tuple(range(10))

        transform = ImageTransform.AffineTransform(seq[:6])
        im.transform((100, 100), transform)
        transform = ImageTransform.ExtentTransform(seq[:4])
        im.transform((100, 100), transform)
        transform = ImageTransform.QuadTransform(seq[:8])
        im.transform((100, 100), transform)
        transform = ImageTransform.MeshTransform([(seq[:4], seq[:8])])
        im.transform((100, 100), transform)
예제 #3
0
 def forward(self, data, state):
     im = Image.fromarray(data)
     width, height = im.size
     displacement = int(self.level / 10 * height / 3 * random.choice([1.0, -1.0]))
     im = im.transform((width, height),
                       ImageTransform.AffineTransform((1.0, 0.0, 0.0, 0.0, 1.0, displacement)),
                       resample=Image.BICUBIC)
     return np.copy(np.asarray(im))
예제 #4
0
    def test_info(self):
        comment = b"File written by Adobe Photoshop\xa8 4.0"

        with Image.open("Tests/images/hopper.gif") as im:
            assert im.info["comment"] == comment

            transform = ImageTransform.ExtentTransform((0, 0, 0, 0))
            new_im = im.transform((100, 100), transform)
        assert new_im.info["comment"] == comment
예제 #5
0
 def _apply_translatex(data: np.ndarray, factor: float) -> np.ndarray:
     im = Image.fromarray(data)
     width, height = im.size
     displacement = factor * width
     im = im.transform((width, height),
                       ImageTransform.AffineTransform(
                           (1.0, 0.0, displacement, 0.0, 1.0, 0.0)),
                       resample=Image.BICUBIC)
     return np.array(im)
예제 #6
0
    def test_info(self):
        comment = b"File written by Adobe Photoshop\xa8 4.0"

        im = Image.open("Tests/images/hopper.gif")
        self.assertEqual(im.info["comment"], comment)

        transform = ImageTransform.ExtentTransform((0, 0, 0, 0))
        new_im = im.transform((100, 100), transform)
        self.assertEqual(new_im.info["comment"], comment)
예제 #7
0
 def _apply_translatex(self, data: np.ndarray) -> np.ndarray:
     im = Image.fromarray(data)
     width, height = im.size
     displacement = random.uniform(self.shift_limit[0] * width,
                                   self.shift_limit[1] * width)
     im = im.transform((width, height),
                       ImageTransform.AffineTransform(
                           (1.0, 0.0, displacement, 0.0, 1.0, 0.0)),
                       resample=Image.BICUBIC)
     return np.array(im)
예제 #8
0
 def _apply_sheary(data: np.ndarray, shear_coeff: float) -> np.ndarray:
     im = Image.fromarray(data)
     width, height = im.size
     yshift = round(abs(shear_coeff) * height)
     newheight = height + yshift
     im = im.transform((width, newheight),
                       ImageTransform.AffineTransform(
                           (1.0, 0.0, 0.0, shear_coeff, 1.0,
                            -yshift if shear_coeff > 0 else 0.0)),
                       resample=Image.BICUBIC)
     im = im.resize((width, height))
     return np.array(im)
예제 #9
0
 def forward(self, data, state):
     im = Image.fromarray(data)
     shear_coeff = self.shear_coef * random.choice([1.0, -1.0])
     width, height = im.size
     yshift = int(round(self.shear_coef * height))
     newheight = height + yshift
     im = im.transform((width, newheight),
                       ImageTransform.AffineTransform(
                           (1.0, 0.0, 0.0, shear_coeff, 1.0, -yshift if shear_coeff > 0 else 0.0)),
                       resample=Image.BICUBIC)
     if self.shear_coef > 0:
         im = im.resize((width, height))
     return np.copy(np.asarray(im))
예제 #10
0
 def _apply_shearx(self, data: np.ndarray) -> np.ndarray:
     im = Image.fromarray(data)
     shear_coeff = random.uniform(self.shear_coef[0], self.shear_coef[1])
     width, height = im.size
     xshift = round(abs(shear_coeff) * width)
     new_width = width + xshift
     im = im.transform(
         (new_width, height),
         ImageTransform.AffineTransform(
             (1.0, shear_coeff, -xshift if shear_coeff > 0 else 0.0, 0.0,
              1.0, 0.0)),
         resample=Image.BICUBIC)
     im = im.resize((width, height))
     return np.array(im)
    def __getitem__(self, index):
        """Return a data point and its metadata information.

        Parameters:
            index - - a random integer for data indexing

        Returns a dictionary that contains A, B, A_paths and B_paths
            A (tensor) - - an image in the input domain
            B (tensor) - - its corresponding image in the target domain
            A_paths (str) - - image paths
            B_paths (str) - - image paths (same as A_paths)
        """
        # read a image given a random integer index
        AB_path = self.AB_paths[index]
        AB = Image.open(AB_path).convert('RGB')
        # split AB image into A and B
        w, h = AB.size
        w2 = int(w / 2)
        A = AB.crop((0, 0, w2, h))
        B = AB.crop((w2, 0, w, h))

        # apply random deformation if requested
        # TODO: merge this into the get_transform() function of base_dataset.py
        if 'deform' in self.opt.preprocess:
            points = self.opt.deform_points
            sigma = self.opt.deform_sigma

            # set up coordinate grid
            x = np.arange(points) * w2 / (points - 1)
            y = np.arange(points) * h / (points - 1)

            X, Y = np.meshgrid(x, y)

            # add deformations
            X += sigma * np.random.randn(points, points)
            Y += sigma * np.random.randn(points, points)

            # set up boxes and quads for the mesh transformation
            quads = []
            boxw = int(w2 / (points - 1))
            boxh = int(h / (points - 1))

            for iqy in range(points - 1):
                for iqx in range(points - 1):
                    # set up box (destination for this quadrilateral)
                    box = (iqx * boxw, iqy * boxh, (iqx + 1) * boxw,
                           (iqy + 1) * boxh)

                    # set up coord list for this quadrilateral
                    quad_x = X[iqy:iqy + 2, iqx:iqx + 2]
                    quad_y = Y[iqy:iqy + 2, iqx:iqx + 2]

                    # arrange coords in counter-clockwise fashion
                    quad_x = np.expand_dims(quad_x.flatten()[[0, 2, 3, 1]], 1)
                    quad_y = np.expand_dims(quad_y.flatten()[[0, 2, 3, 1]], 1)

                    # arrange data for MeshTransform
                    quad = tuple(np.concatenate((quad_x, quad_y), 1).flatten())
                    quads.append((box, quad))

            transform = ImageTransform.MeshTransform(quads)

            A = A.transform((w2, h), transform)
            B = B.transform((w2, h), transform)

        # apply the same transform to both A and B
        transform_params = get_params(self.opt, A.size)
        A_transform = get_transform(self.opt,
                                    transform_params,
                                    grayscale=(self.input_nc == 1))

        transform_params[
            'no_erasing'] = True  # prevent random erasing from being applied to target image
        transform_params[
            'blur'] = False  # prevent blur augmentation from being applied to target image
        B_transform = get_transform(self.opt,
                                    transform_params,
                                    grayscale=(self.output_nc == 1))

        A = A_transform(A)
        B = B_transform(B)

        return {'A': A, 'B': B, 'A_paths': AB_path, 'B_paths': AB_path}