简体   繁体   English

SVG,将线条形成图像

[英]SVG, forming lines into an image

I'm forming an image in SVG from a bitmap using svgwrite and python where each line is rotated by theta around a common origin into a fan like-pattern. 我正在使用svgwrite和python从位图在SVG中形成图像,其中每条线围绕共同的原点旋转theta到扇形图案中。 Currently this image is running around 10 MB for 128 lines largely because the excessive amount of floating point retained in the 79000+ line segments (one line segment per pixel). 当前,此图像在128行上大约运行10 MB,这在很大程度上是因为79000+线段(每个像素一个线段)中保留了过多的浮点。

扫描转换后的图像

I'd like to get the image size down significantly by drawing a single line from the origin out to the end-point and then stretch one line 'image' over that SVG line. 我想通过从原点到终点画一条线,然后在该SVG线上拉伸一行“图像”,来显着减小图像尺寸。 Is this possible using SVG? 使用SVG是否可能? Beyond that, I'm open to any suggestion that might get the size down significantly and so I later I can animate the lines in place. 除此之外,我乐于接受任何可能会大大减小尺寸的建议,因此我以后可以对线条进行动画处理。

How about this solution (see fiddle ): 这个解决方案如何(请参阅小提琴 ):

  1. Slice the image up in the number of strips you need. 将图像切成所需数量的条带。 For the example in the linked fiddle, I used ImageMagick as follows to cut up a 128x128 PNG image into 128 vertical strips: 对于链接的小提琴中的示例,我如下使用ImageMagick将128x128 PNG图像切成128个垂直条:
    convert image.png -crop 1x128 +repage +adjoin strip-%d.png
  2. Convert all the image strips to data URI format and embed them in the SVG. 将所有图像条转换为数据URI格式,并将其嵌入SVG中。 I used this bash one-liner: 我使用了这种bash一线式:
    for i in strip-*.png; do echo "data:image/png;base64,$(openssl base64 < $i | tr -d '\\n')"; done > data-uris.txt
  3. In the SVG, use the transform() attribute on the image strip elements to rotate them to the required degree. 在SVG中,使用image带元素上的transform()属性将它们旋转到所需的程度。

For a 128x128 PNG icon of 16.4KB, I end up with an SVG file of 128.1KB. 对于16.4KB的128x128 PNG图标,我最终得到了128.1KB的SVG文件。 Most of that is the base64-encoded image data (the total size of the 128 PNG strips is already 85.1KB). 其中大多数是base64编码的图像数据(128个PNG条带的总大小已为85.1KB)。 It could be further reduced a little by rounding of some floats, but I don't think there's a whole lot to be gained. 可以通过对一些浮点数进行四舍五入来进一步降低它,但是我认为没有很多收获。

There might be another approach possible where you embed the image as a whole, and reference another clipped section of the same image over and over, but I couldn't get that to work. 可能存在另一种方法,您可以将图像作为一个整体嵌入,并一遍又一遍地引用同一图像的另一个裁剪部分,但是我无法使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM