def test_read_image(self):
     out_dir = tempfile.gettempdir()
     fname = os.path.join(out_dir, 'img.png')
     sca.save_image(self.img_rgba.copy(), fname)
     res = sca.read_image(fname)
     res = np.array(res.getdata(), dtype=np.float32)
     src = np.array(self.img_rgba.getdata(), dtype=np.float32)
     self.assertEqual(np.max(res - src), 0)
    def test_update_existing_image(self):
        out_dir = tempfile.gettempdir()
        fname = os.path.join(out_dir, 'img.png')
        sca.save_image(self.img_rgba.copy(), fname)
        data = 255 * np.ones(self.data.shape, dtype=np.uint8)
        # Replace part of the alpha channel with zeros, so that no all of the
        # image is updated
        data[0, :] *= 0
        data_stack = np.dstack((data, data, data, data))
        new_img = Image.fromarray(data_stack, mode='RGBA')
        res = sca.update_existing_image(fname, new_img)
        res = np.array(res)
        self.assertTrue(np.all(res[1:, :, :] == 255))
        self.assertTrue(np.all(res[0, :, :-1] ==
                               np.array(self.img_rgba)[0, :, :-1]))

        # Update L with L
        sca.save_image(self.img_l.copy(), fname)
        res = sca.update_existing_image(fname, self.img_l.copy())
        self.assertTrue(res.mode == 'L')
        # Update L with LA
        res = sca.update_existing_image(fname, self.img_la.copy())
        self.assertTrue(res.mode == 'LA')
        # Update L with RGB
        res = sca.update_existing_image(fname, self.img_rgb.copy())
        self.assertTrue(res.mode == 'RGB')
        # Update L with RGBA
        res = sca.update_existing_image(fname, self.img_rgba.copy())
        self.assertTrue(res.mode == 'RGBA')

        # Update LA with L
        sca.save_image(self.img_la.copy(), fname)
        res = sca.update_existing_image(fname, self.img_l.copy())
        self.assertTrue(res.mode == 'L')
        # Update LA with LA
        res = sca.update_existing_image(fname, self.img_la.copy())
        self.assertTrue(res.mode == 'LA')
        # Update LA with RGB
        res = sca.update_existing_image(fname, self.img_rgb.copy())
        self.assertTrue(res.mode == 'RGB')
        # Update LA with RGBA
        res = sca.update_existing_image(fname, self.img_rgba.copy())
        self.assertTrue(res.mode == 'RGBA')

        # Update RGB with L
        sca.save_image(self.img_rgb.copy(), fname)
        res = sca.update_existing_image(fname, self.img_l.copy())
        self.assertTrue(res.mode == 'L')
        # Update RGB with LA
        res = sca.update_existing_image(fname, self.img_la.copy())
        self.assertTrue(res.mode == 'LA')
        # Update RGB with RGB
        res = sca.update_existing_image(fname, self.img_rgb.copy())
        self.assertTrue(res.mode == 'RGB')
        # Update RGB with RGBA
        res = sca.update_existing_image(fname, self.img_rgba.copy())
        self.assertTrue(res.mode == 'RGBA')

        # Update RGBA with L
        sca.save_image(self.img_rgba.copy(), fname)
        res = sca.update_existing_image(fname, self.img_l.copy())
        self.assertTrue(res.mode == 'L')
        # Update RGBA with LA
        res = sca.update_existing_image(fname, self.img_la.copy())
        self.assertTrue(res.mode == 'LA')
        # Update RGBA with RGB
        res = sca.update_existing_image(fname, self.img_rgb.copy())
        self.assertTrue(res.mode == 'RGB')
        # Update RGBA with RGBA
        res = sca.update_existing_image(fname, self.img_rgba.copy())
        self.assertTrue(res.mode == 'RGBA')
 def test_save_image(self):
     out_dir = tempfile.gettempdir()
     fname = os.path.join(out_dir, 'img.png')
     sca.save_image(self.img_rgba.copy(), fname)
     fname = os.path.join(out_dir, 'img.tif')
     sca.save_image(self.img_rgba.copy(), fname)