Example #1
0
def main():

    workpath = tempfile.mkdtemp(dir='.')

    tools.make_random_png32_files(width=(1, 64), height=(1, 64), num_files=10, dirpath=workpath)

    input_filepaths = [
        workpath + '/*.png',
    ]
    output_filepath = workpath + '/atlas.png'
    container_width = 100

    options = {
        'margin': (1, 1, 1, 1),
        'collapse_margin': False,
        'enable_auto_size': True,
        'enable_vertical_flip': True,
        'force_pow2': False
    }

    packer.pack(
        input_filepaths=input_filepaths,
        output_filepath=output_filepath,
        container_width=container_width,
        options=options
    )
Example #2
0
    def test_collapse_margin(self):
        with tempfile.TemporaryDirectory() as workpath:
            tools.make_random_png32_files(width=(1, 64),
                                          height=(1, 64),
                                          num_files=4,
                                          dirpath=workpath)
            tools.make_random_bmp_files(width=(1, 64),
                                        height=(1, 64),
                                        num_files=3,
                                        dirpath=workpath)
            tools.make_random_jpeg_files(width=(1, 64),
                                         height=(1, 64),
                                         num_files=3,
                                         dirpath=workpath)

            input_filepaths = [
                os.path.join(workpath, '*.*'),
            ]
            output_filepath = os.path.join(workpath, 'output.png')
            container_width = 66

            options = {'margin': (1, 1, 1, 1), 'collapse_margin': True}

            packer.pack(
                input_filepaths=input_filepaths,
                output_filepath=output_filepath,
                container_width=container_width,
                options=options,
            )
            self.assertTrue(os.path.exists(output_filepath))
            self.assertTrue(
                os.path.exists(os.path.splitext(output_filepath)[0] + '.json'))
Example #3
0
    def test_combination(self):
        keys = ('collapse_margin', 'enable_auto_size', 'enable_vertical_flip',
                'force_pow2')
        patterns = (
            (True, True, True, True),
            (True, False, False, False),
            (True, True, False, False),
            (True, False, True, False),
            (True, False, False, True),
            (True, True, True, False),
            (True, True, False, True),
            (True, False, True, True),
            (False, True, False, False),
            (False, True, True, False),
            (False, True, False, True),
            (False, True, True, True),
            (False, False, True, False),
            (False, False, True, True),
            (False, False, False, True),
            (False, False, False, False),
        )

        container_width = 66
        for pattern in patterns:
            options = {k: v for k, v in zip(keys, pattern)}
            options['margin'] = (1, 1, 1, 1)
            with tempfile.TemporaryDirectory() as workpath:
                tools.make_random_png32_files(width=(1, 64),
                                              height=(1, 64),
                                              num_files=4,
                                              dirpath=workpath)
                tools.make_random_bmp_files(width=(1, 64),
                                            height=(1, 64),
                                            num_files=3,
                                            dirpath=workpath)
                tools.make_random_jpeg_files(width=(1, 64),
                                             height=(1, 64),
                                             num_files=3,
                                             dirpath=workpath)

                input_filepaths = [
                    os.path.join(workpath, '*.*'),
                ]
                output_filepath = os.path.join(workpath, 'output.png')

                packer.pack(input_filepaths=input_filepaths,
                            output_filepath=output_filepath,
                            container_width=container_width,
                            options=options)
                self.assertTrue(os.path.exists(output_filepath))
                if options['force_pow2']:
                    with Image.open(fp=output_filepath) as im:
                        self.assertTrue(self.is_power_of_2(im.width))
                        self.assertTrue(self.is_power_of_2(im.height))

                self.assertTrue(
                    os.path.exists(
                        os.path.splitext(output_filepath)[0] + '.json'))
Example #4
0
    def test_cli(self):
        command = 'impack -h'
        returncode = subprocess.call(command.split(),
                                     stderr=subprocess.DEVNULL,
                                     stdout=subprocess.DEVNULL)
        self.assertEqual(returncode, 0)

        command = 'impack'
        returncode = subprocess.call(command.split(),
                                     stderr=subprocess.DEVNULL,
                                     stdout=subprocess.DEVNULL)
        self.assertEqual(returncode, 2)

        with tempfile.TemporaryDirectory() as workpath:
            tools.make_random_png32_files(width=(1, 64),
                                          height=(1, 64),
                                          num_files=4,
                                          dirpath=workpath)
            tools.make_random_bmp_files(width=(1, 64),
                                        height=(1, 64),
                                        num_files=3,
                                        dirpath=workpath)
            tools.make_random_jpeg_files(width=(1, 64),
                                         height=(1, 64),
                                         num_files=3,
                                         dirpath=workpath)

            output_filepath = workpath + '/output.png'

            command = 'impack -i {i} -o {o} -w {w}'.format(i=workpath + '/*.*',
                                                           o=output_filepath,
                                                           w=100)
            returncode = subprocess.call(command.split(),
                                         stderr=subprocess.DEVNULL,
                                         stdout=subprocess.DEVNULL)
            self.assertEqual(returncode, 0)
            self.assertTrue(os.path.exists(output_filepath))

        with tempfile.TemporaryDirectory() as workpath:
            tools.make_random_png32_files(width=(1, 64),
                                          height=(1, 64),
                                          num_files=4,
                                          dirpath=workpath)
            tools.make_random_bmp_files(width=(1, 64),
                                        height=(1, 64),
                                        num_files=3,
                                        dirpath=workpath)
            tools.make_random_jpeg_files(width=(1, 64),
                                         height=(1, 64),
                                         num_files=3,
                                         dirpath=workpath)

            output_filepath = workpath + '/output.png'

            command = 'impack -i {i} -o {o} -w 1 --disable-auto-size'.format(
                i=workpath + '/*.*',
                o=output_filepath,
            )
            returncode = subprocess.call(command.split(),
                                         stderr=subprocess.DEVNULL,
                                         stdout=subprocess.DEVNULL)
            self.assertEqual(returncode, 1)
            self.assertFalse(os.path.exists(output_filepath))
Example #5
0
    def test_disable_auto_size(self):
        with tempfile.TemporaryDirectory() as workpath:
            tools.make_random_png32_files(width=(1, 64),
                                          height=(1, 64),
                                          num_files=4,
                                          dirpath=workpath)
            tools.make_random_bmp_files(width=(1, 64),
                                        height=(1, 64),
                                        num_files=3,
                                        dirpath=workpath)
            tools.make_random_jpeg_files(width=(1, 64),
                                         height=(1, 64),
                                         num_files=3,
                                         dirpath=workpath)

            input_filepaths = [
                os.path.join(workpath, '*.*'),
            ]
            output_filepath = os.path.join(workpath, 'output.png')
            container_width = 66

            options = {'margin': (1, 1, 1, 1), 'enable_auto_size': False}

            packer.pack(
                input_filepaths=input_filepaths,
                output_filepath=output_filepath,
                container_width=container_width,
                options=options,
            )
            self.assertTrue(os.path.exists(output_filepath))
            self.assertTrue(
                os.path.exists(os.path.splitext(output_filepath)[0] + '.json'))

        with tempfile.TemporaryDirectory() as workpath:
            tools.make_random_png32_files(width=(1, 64),
                                          height=(1, 64),
                                          num_files=4,
                                          dirpath=workpath)
            tools.make_random_bmp_files(width=(1, 64),
                                        height=(1, 64),
                                        num_files=3,
                                        dirpath=workpath)
            tools.make_random_jpeg_files(width=(1, 64),
                                         height=(1, 64),
                                         num_files=3,
                                         dirpath=workpath)

            input_filepaths = [
                os.path.join(workpath, '*.*'),
            ]
            output_filepath = os.path.join(workpath, 'output.png')
            container_width = 1

            options = {'margin': (1, 1, 1, 1), 'enable_auto_size': False}

            with self.assertRaises(blf.LocationNotFoundError):
                packer.pack(
                    input_filepaths=input_filepaths,
                    output_filepath=output_filepath,
                    container_width=container_width,
                    options=options,
                )
            self.assertFalse(os.path.exists(output_filepath))
            self.assertFalse(
                os.path.exists(os.path.splitext(output_filepath)[0] + '.json'))