猿问

如何在Windows 10上使用python 3.6.5调整比例的同时调整svg的大小

我正在尝试将一些默认默认值约为420 x 500像素的SVG缩小,它们的大小并非全部相同,但是我想在输出文件上保持50px的宽度。


这是我到目前为止管理(从这个):


import os

import svgutils



path = 'path/to/files'

svg_files = [f for f in os.listdir(path) if f.endswith('.svg')]


for i in svg_files:

    svg_file = os.path.join(path, i)

    svg = svgutils.transform.fromfile(svg_file)

    original = svgutils.compose.SVG(svg_file)

    original.scale(.1)

    svg_out = os.path.splitext(svg_file)[0] + '_scale.svg'

    new_svg = svgutils.compose.Figure(float(svg.height) * .1,

                                      float(svg.width) * .1, original)

    new_svg.save(svg_out)

但是它仅添加<g>with变换,而不会调整原始大小。而且,最终结果不会在inkscape中显示。


知道我在这里做错了吗?


白猪掌柜的
浏览 190回答 2
2回答

qq_笑_17

到目前为止,我有一些东西:# scale SVG images from filesimport osimport reimport svgutils as suSCALE = .1path = 'E:/Slicke/Sitne'svg_files = [f for f in os.listdir(path) if f.endswith('.svg')]for i in svg_files:&nbsp; &nbsp; svg_file = os.path.join(path, i)&nbsp; &nbsp; # Get SVGFigure from file&nbsp; &nbsp; original = su.transform.fromfile(svg_file)&nbsp; &nbsp; # Original size is represetnted as string (examle: '600px'); convert to float&nbsp; &nbsp; original_width = float(re.sub('[^0-9]','', original.width))&nbsp; &nbsp; original_height = float(re.sub('[^0-9]','', original.width))&nbsp; &nbsp; scaled = su.transform.SVGFigure(original_width * SCALE, original_height * SCALE,)&nbsp; &nbsp; # Get the root element&nbsp; &nbsp; svg = original.getroot()&nbsp; &nbsp; # Scale the root element&nbsp; &nbsp; svg.scale_xy(SCALE, SCALE)&nbsp; &nbsp; # Add scaled svg element to figure&nbsp; &nbsp; scaled.append(svg)&nbsp; &nbsp; # Create the path and new file name&nbsp; &nbsp; svg_out = os.path.splitext(svg_file)[0] + '_scale.svg'&nbsp; &nbsp; print(svg_out)&nbsp; &nbsp; scaled.save(svg_out)
随时随地看视频慕课网APP

相关分类

Python
我要回答