hashes_file_name = "svg2png.hashes"
hashes_file_path = image_sources_path + "/" + hashes_file_name

file_names = os.listdir(image_sources_path)
svg_file_paths = [image_sources_path + "/" + file_name for file_name in file_names if file_name.endswith(".svg")]

svg_file_paths_to_convert = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, svg_file_paths)
svg_file_names = [re.sub(".svg$", "", re.sub(".*/", "", file_path)) for file_path in svg_file_paths_to_convert]


def convert_svg_to_png(svg_file_name, png_file_name, dpi):
    svg_full_path = image_sources_path + "/" + svg_file_name + ".svg"
    png_full_path = images_path + "/" + png_file_name + ".png"
    convert_command = "inkscape -f %s -e %s -d %s" % (svg_full_path, png_full_path, dpi)
    proc = subprocess.Popen(convert_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    return proc


processes = {}
for file_name in svg_file_names:
    name = re.sub(".svg$", "", file_name)
    name2x = name + "_2x"
    processes[name] = convert_svg_to_png(name, name, 90)
    processes[name2x] = convert_svg_to_png(name, name2x, 180)

for file_name, proc in processes.items():
    (convert_out, _) = proc.communicate()
    print("Conversion of %s finished: %s" % (file_name, convert_out))

devtools_file_hashes.update_file_hashes(hashes_file_path, svg_file_paths)
        print "This script needs \"%s\" to be installed." % app_name
        sys.exit(1)


check_installed("npx")


def optimize_svg(svg_input_path):
    svg_output_path = os.path.join(images_path, os.path.basename(svg_input_path))
    optimize_command = "npx svgo -i %s -o %s" % (svg_input_path, svg_output_path)
    proc = subprocess.Popen(optimize_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=chromium_src_path)
    return proc


if len(SVG_FILE_NAMES):
    print "%d unoptimized svg files found." % len(SVG_FILE_NAMES)
else:
    print "All svg files are already optimized."
    sys.exit()

processes = {}
for svg_file_path in SVG_FILE_PATHS_TO_OPTIMIZE:
    name = os.path.splitext(os.path.basename(svg_file_path))[0]
    processes[name] = optimize_svg(svg_file_path)

for file_name, proc in processes.items():
    (optimize_out, _) = proc.communicate()
    print("Optimization of %s finished: %s" % (file_name, optimize_out))

devtools_file_hashes.update_file_hashes(HASHES_FILE_PATH, svg_file_paths)
Exemplo n.º 3
0
svg_file_names = [
    re.sub(".svg$", "", re.sub(".*/", "", file_path))
    for file_path in svg_file_paths_to_convert
]


def convert_svg_to_png(svg_file_name, png_file_name, dpi):
    svg_full_path = image_sources_path + "/" + svg_file_name + ".svg"
    png_full_path = images_path + "/" + png_file_name + ".png"
    convert_command = "inkscape -f %s -e %s -d %s" % (svg_full_path,
                                                      png_full_path, dpi)
    proc = subprocess.Popen(convert_command,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.STDOUT,
                            shell=True)
    return proc


processes = {}
for file_name in svg_file_names:
    name = re.sub(".svg$", "", file_name)
    name2x = name + "_2x"
    processes[name] = convert_svg_to_png(name, name, 90)
    processes[name2x] = convert_svg_to_png(name, name2x, 180)

for file_name, proc in processes.items():
    (convert_out, _) = proc.communicate()
    print("Conversion of %s finished: %s" % (file_name, convert_out))

devtools_file_hashes.update_file_hashes(hashes_file_path, svg_file_paths)