def optimize_file(input_file): with open(input_file, mode="rb") as input: with tempfile.NamedTemporaryFile(suffix=".svg", mode="wb", delete=False) as output: scour.start(OPTIONS, input, output) shutil.move(output.name, input_file)
def render(gdf, outfile, filter=None, optimize=True): logger.info(gdf.crs) gdf = gdf.to_crs(epsg=3857) # 3395 logger.info(gdf.head()) minx, miny, maxx, maxy = gdf.geometry.total_bounds gdf.geometry = gdf.geometry.translate(-minx, -miny) minx, miny, maxx, maxy = gdf.geometry.total_bounds logger.info((minx, miny, maxx, maxy)) # gdf = gdf.scale(100000, 100000, origin=(0, 0)) viewbox = " ".join(map(str, gdf.total_bounds)) dwg = svgwrite.Drawing(outfile, height="100%", width="100%", viewBox=(viewbox)) white = "#FFFFFF" grey = "#969696" dwg.fill(color=white) dwg.stroke(color=grey, width=1) extras = {} if filter: filtered_gdf = gdf[filter] else: filtered_gdf = gdf for g in filtered_gdf.geometry: mp = [(x, maxy - y) for x, y in zip(*g.coords.xy)] # gr = svgwrite.container.Group(**extras) dp = dwg.polyline(points=mp) # gr.add(dp) dwg.add(dp) if optimize: class ScourOptions: quiet = True buf1 = io.StringIO() dwg.write(buf1) buf1.seek(0) buf1 = io.BytesIO(buf1.getvalue().encode()) buf1.seek(0) buf1.name = outfile with open(outfile, "wb") as f: scour.start(ScourOptions, buf1, f) else: with open(outfile, "w", encoding="utf-8") as f: dwg.write(f)
def optimize_with_scour(files): from scour import scour """ Optimize SVG files using Scour. """ # Configure scour options = scour.parse_args() options.digits = 4 # values lower than 4 for '.digits' led to visilble differences between # the original and 'optimized' file options.indent_depth = 2 options.simple_colors = False options.enable_viewboxing = True options.embed_rasters = True options.group_create = True options.group_collapse = True options.shorten_ids = True options.strip_comments = True options.strip_ids = True options.strip_xml_prolog = True options.strip_xml_space_attribute = True options.remove_titles = True options.remove_descriptions = True options.remove_metadata = True options.remove_descriptive_elements = True options.quiet = True for file in files: options.infilename = file options.outfilename = file[:-4] + "-scoured.svg" try: # .start will close the files. Weird flex but ok with open(file, 'rb') as infile, open(options.outfilename, 'wb') as outfile: scour.start(options, infile, outfile) except FileNotFoundError: # Doing this because we have a list of # hard-coded file names print(f"File {file} not found") except: print("Failed to optimize:", file) if not infile.closed: infile.close() if not outfile.closed: outfile.close() if Path(options.outfilename).is_file(): Path(options.outfilename).unlink() else: Path(options.outfilename).rename(file)
def scour(target, source, env=[]): """ Use scour to clean an svg file. """ options = scour.generateDefaultOptions() # override defaults for max cleansing options.enable_viewboxing = True options.strip_comments = True options.strip_ids = True options.remove_metadata = True options.indent_type = None options.shorten_ids = True if 'SCOUR_OPTIONS' in env: options.__dict__.update(env['SCOUR_OPTIONS']) instream = open(source, 'rb') outstream = open(target, 'wb') scour.start(options, instream, outstream)
def optimize_svg(in_path: Path, out_path: Path): """ Optimize svg file, but keep metadata. """ options = optparse.Values({ 'infilename': str(in_path), 'outfilename': str(out_path), 'digits': 5, 'quiet': True, 'verbose': False, 'cdigits': -1, 'simple_colors': True, 'style_to_xml': True, 'group_collapse': True, 'group_create': False, 'keep_editor_data': False, 'keep_defs': False, 'renderer_workaround': True, 'strip_xml_prolog': False, 'remove_titles': False, 'remove_descriptions': False, 'remove_metadata': False, 'remove_descriptive_elements': False, 'strip_comments': True, 'embed_rasters': True, 'enable_viewboxing': True, 'indent_type': 'none', 'indent_depth': 0, 'newlines': False, 'strip_xml_space_attribute': False, 'strip_ids': True, 'shorten_ids': True, 'shorten_ids_prefix': '', 'protect_ids_noninkscape': False, 'protect_ids_list': None, 'protect_ids_prefix': None, 'error_on_flowtext': False }) in_file, out_file = scour.getInOut(options) scour.start(options, in_file, out_file)
def cleanup(source): target = source + ".tmp" options = parse_args([ "--enable-viewboxing", "--remove-metadata", "--enable-id-stripping", "--enable-comment-stripping", "--strip-xml-space", "--strip-xml-prolog", "--shorten-ids", "-i", source, "-o", target ]) input, output = getInOut(options) start(options, input, output) os.remove(source) os.rename(target, source) register_namespace("", "http://www.w3.org/2000/svg") tree = ElementTree() tree.parse(source) svg = next(tree.iter()) for attr in ["style", "verstion", "x", "y"]: drop(svg, attr) svg.attrib["width"] = "100%" svg.attrib["height"] = "100%" tree.write(source)
elif quality > 95: quality = 95 dest.convert('RGB').save(fileout, 'JPEG', optimize=True, subsampling=0, quality=quality) # python-scour if ".svg" in fileout: from scour.scour import sanitizeOptions, start options = sanitizeOptions() options.strip_xml_prolog = True # --strip-xml-prolog options.remove_metadata = True # --remove-metadata options.strip_comments = True # --enable-comment-stripping options.strip_ids = True # --enable-id-stripping options.indent_type = None # --indent=none start(options, open(filein, 'rb'), open(fileout, 'wb')) # python-pil else: from PIL import Image, ImageSequence source = Image.open(filein) size = calcSize(source, size) if source.format == 'GIF' and ".gif" in fileout: dest = resizeAnimatedGif(source, size, fixed) saveGif(dest, fileout, quality) elif source.format == 'PNG' and ".png" in fileout: dest = resizeAnimatedPng(source, size, fixed) savePng(dest, fileout, quality) elif source.format == 'WEBP' and ".webp" in fileout: dest = resizeAnimatedWeb(source, size, fixed) saveWebp(dest, fileout, quality)
#!/usr/bin/env python import sys from scour.scour import run, parse_args, getInOut, start options = parse_args() (input, output) = getInOut(options) start(options, input, output)