def test_fail_on_missing_env_variable(self):
     env_backup = None
     if svgtopng._BATIK_ENVIRONMENT_HOME in os.environ:
         env_backup = os.environ.copy()
         del os.environ[svgtopng._BATIK_ENVIRONMENT_HOME]
     try:
         svgtopng.convert("dummy.svg")
         self.fail("Expected exception 'EnvironmentError' not thrown")
     except EnvironmentError:
         pass
     finally:
         if env_backup:
             os.environ.clear()
             os.environ.update(env_backup)
         assert svgtopng._BATIK_ENVIRONMENT_HOME in os.environ
 def test_parse_svgfile_and_convert_to_png_antenna(self):
     img_proc_dir = os.path.join(helpers.APP_PATH, "test", "res", "img_proc_png")
     input_svg_path = os.path.join(img_proc_dir, "input_antenna.svg")
     expected_image_path = os.path.join(img_proc_dir, "expected_antenna.png")
     
     assert os.path.exists(input_svg_path)
     
     rotation_x, rotation_y = 53, 43
     
     output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y)
     
     from javax.imageio import ImageIO
     from java.io import File
     bufferd_image = ImageIO.read(File(output_png_path))
     width, height = bufferd_image.getWidth(), bufferd_image.getHeight()
     output_image_matrix = [[bufferd_image.getRGB(i, j) for j in xrange(height)] for i in xrange(width)]
     
     bufferd_image = ImageIO.read(File(expected_image_path))
     width, height = bufferd_image.getWidth(), bufferd_image.getHeight()
     expected_image_matrix = [[bufferd_image.getRGB(i, j) for j in xrange(height)] for i in xrange(width)]
     
     for i in xrange(width):
         for j in xrange(height):
             exp_rgb_val = expected_image_matrix[i][j]
             result_rgb_val = output_image_matrix[i][j]
             assert exp_rgb_val == result_rgb_val
Exemple #3
0
    def test_parse_svgfile_and_convert_to_png_background(self):
        img_proc_dir = os.path.join(helpers.APP_PATH, "test", "res",
                                    "img_proc_png")
        input_svg_path = os.path.join(img_proc_dir, "input_background.svg")
        expected_image_path = os.path.join(img_proc_dir,
                                           "expected_background.png")

        assert os.path.exists(input_svg_path)

        rotation_x, rotation_y = 255, 180

        output_png_path = svgtopng.convert(input_svg_path, rotation_x,
                                           rotation_y, ns_registry_lock)

        from javax.imageio import ImageIO
        from java.io import File
        bufferd_image = ImageIO.read(File(output_png_path))
        width, height = bufferd_image.getWidth(), bufferd_image.getHeight()
        output_image_matrix = [[
            bufferd_image.getRGB(i, j) for j in xrange(height)
        ] for i in xrange(width)]

        bufferd_image = ImageIO.read(File(expected_image_path))
        width, height = bufferd_image.getWidth(), bufferd_image.getHeight()
        expected_image_matrix = [[
            bufferd_image.getRGB(i, j) for j in xrange(height)
        ] for i in xrange(width)]

        for i in xrange(width):
            for j in xrange(height):
                exp_rgb_val = expected_image_matrix[i][j]
                result_rgb_val = output_image_matrix[i][j]
                assert exp_rgb_val == result_rgb_val
Exemple #4
0
    def test_parse_svgfile_and_convert_to_png_hat_q4(self):
        img_proc_dir = os.path.join(helpers.APP_PATH, "test", "res",
                                    "img_proc_png")
        input_svg_path = os.path.join(img_proc_dir, "input_hat.svg")

        assert os.path.exists(input_svg_path)

        rotation_x, rotation_y = 97, -51

        expected_image_path = os.path.join(
            img_proc_dir, "expected_hat" + "_rotX_" + str(rotation_x) +
            "_rotY_" + str(rotation_y) + ".png")

        output_png_path = svgtopng.convert(input_svg_path, rotation_x,
                                           rotation_y)

        from javax.imageio import ImageIO
        from java.io import File
        bufferd_image = ImageIO.read(File(output_png_path))
        width, height = bufferd_image.getWidth(), bufferd_image.getHeight()
        output_image_matrix = [[
            bufferd_image.getRGB(i, j) for j in xrange(height)
        ] for i in xrange(width)]

        bufferd_image = ImageIO.read(File(expected_image_path))
        width, height = bufferd_image.getWidth(), bufferd_image.getHeight()
        expected_image_matrix = [[
            bufferd_image.getRGB(i, j) for j in xrange(height)
        ] for i in xrange(width)]

        for i in xrange(width):
            for j in xrange(height):
                exp_rgb_val = expected_image_matrix[i][j]
                result_rgb_val = output_image_matrix[i][j]
                assert exp_rgb_val == result_rgb_val
    def test_can_convert_file_from_svg_to_png(self):
        regular_svg_path = os.path.join(common_testing.get_test_project_path("dancing_castle"), "1.svg")
        svg_path_with_fileext = os.path.join(self.temp_dir, "test_path_which_includes_extension_.svg.svg", "1.svg")
        os.makedirs(os.path.dirname(svg_path_with_fileext))
        shutil.copy(regular_svg_path, svg_path_with_fileext)
        for input_svg_path in [regular_svg_path, svg_path_with_fileext]:
            assert os.path.exists(input_svg_path)

            output_png_path = svgtopng.convert(input_svg_path)

            assert os.path.exists(output_png_path)
            assert imghdr.what(output_png_path) == "png"
        def write_mediafiles(original_to_converted_catrobat_resource_file_name):
            def resource_name_for(file_path):
                return common.md5_hash(file_path) + os.path.splitext(file_path)[1]

            for scratch_md5_name, src_path in self.scratch_project.md5_to_resource_path_map.iteritems():
                if scratch_md5_name in self.scratch_project.unused_resource_names:
                    log.info("Ignoring unused resource file: %s", src_path)
                    continue

                file_ext = os.path.splitext(scratch_md5_name)[1].lower()
                converted_file = False

                # TODO: refactor to a MediaConverter class
                if file_ext in {".png", ".svg", ".jpg", ".gif"}:
                    target_dir = images_path

                    if file_ext == ".svg":
                        # converting svg to png -> new md5 and filename
                        src_path = svgtopng.convert(src_path)
                        if not os.path.exists(src_path):
                            assert False, "Not existing: {}. Available files in directory: {}".format(src_path, os.listdir(os.path.dirname(src_path)))
                        converted_file = True

                elif file_ext in {".wav", ".mp3"}:
                    target_dir = sounds_path
                    if file_ext == ".wav":
                        if not wavconverter.is_android_compatible_wav(src_path):
                            temp_path = src_path.replace(".wav", "converted.wav")
                            wavconverter.convert_to_android_compatible_wav(src_path, temp_path)
                            src_path = temp_path
                            converted_file = True

                else:
                    assert file_ext in {".json"}, "Unknown media file extension: %s" % src_path
                    continue

                assert os.path.exists(src_path), "Not existing: {}. Available files in directory: {}".format(src_path, os.listdir(os.path.dirname(src_path)))

                # for Catrobat separate file is needed for resources which are used multiple times but with different names
                for scratch_resource_name in self.scratch_project.find_all_resource_names_for(scratch_md5_name):
                    catrobat_resource_file_name = self._catrobat_resource_file_name_for(scratch_md5_name, scratch_resource_name)
                    if converted_file:
                        original_resource_file_name = catrobat_resource_file_name
                        converted_scratch_md5_name = resource_name_for(src_path)
                        converted_resource_file_name = self._catrobat_resource_file_name_for(converted_scratch_md5_name, scratch_resource_name)
                        catrobat_resource_file_name = original_to_converted_catrobat_resource_file_name[original_resource_file_name] = converted_resource_file_name
                        assert catrobat_resource_file_name != original_resource_file_name
                    shutil.copyfile(src_path, os.path.join(target_dir, catrobat_resource_file_name))
                if converted_file:
                    os.remove(src_path)
    def run(self):
        old_src_path = self._kwargs["data"]["src_path"]
        media_type = self._kwargs["data"]["media_type"]
        info = self._kwargs["data"]["info"]

        if media_type == MediaType.UNCONVERTED_SVG:
            # converting svg to png -> new md5 and filename
            new_src_path = svgtopng.convert(old_src_path, info["rotationCenterX"], info["rotationCenterY"], ns_registry_lock)
        elif media_type == MediaType.UNCONVERTED_WAV:
            # converting Android-incompatible wav to compatible wav
            new_src_path = wavconverter.convert_to_android_compatible_wav(old_src_path)
        else:
            assert False, "Unsupported Media Type! Cannot convert media file: %s" % old_src_path

        self._kwargs["new_src_paths"][old_src_path] = new_src_path
        progress_bar = self._kwargs["progress_bar"]
        if progress_bar != None:
            progress_bar.update(ProgressType.CONVERT_MEDIA_FILE)
        assert os.path.exists(new_src_path), "Not existing: {}. Available files in directory: {}" \
               .format(new_src_path, os.listdir(os.path.dirname(new_src_path)))
    def run(self):
        old_src_path = self._kwargs["data"]["src_path"]
        media_type = self._kwargs["data"]["media_type"]
        info = self._kwargs["data"]["info"]

        if media_type == MediaType.UNCONVERTED_SVG:
            # converting svg to png -> new md5 and filename
            new_src_path = svgtopng.convert(old_src_path, info["rotationCenterX"], info["rotationCenterY"])
        elif media_type == MediaType.UNCONVERTED_WAV:
            # converting Android-incompatible wav to compatible wav
            new_src_path = wavconverter.convert_to_android_compatible_wav(old_src_path)
        else:
            assert False, "Unsupported Media Type! Cannot convert media file: %s" % old_src_path

        self._kwargs["new_src_paths"][old_src_path] = new_src_path
        progress_bar = self._kwargs["progress_bar"]
        if progress_bar != None:
            progress_bar.update(ProgressType.CONVERT_MEDIA_FILE)
        assert os.path.exists(new_src_path), "Not existing: {}. Available files in directory: {}" \
               .format(new_src_path, os.listdir(os.path.dirname(new_src_path)))