Python skia 将 skia.DynamicMemoryWStream 保存为 SVG 文件

huangapple go评论94阅读模式
英文:

python skia save skia.DynamicMemoryWStream to svg file

问题

短而言之,以下代码可以提供上下文:

import skia
stream = skia.DynamicMemoryWStream()
...

还有更多对 stream 变量的操作,但它们不重要,关键是我想将 stream 转换为SVG文件。我查看了Python skia库的文档,但它们写得很差,我无法找到实现这个目标的方法。

英文:

in short, the following code can give the context:

import skia
stream = skia.DynamicMemoryWStream()
...

There are more operations done over the stream variable, but they are not important and the key thing here is I want to convert the stream to an SVG file. I have looked Python skia library documentation, however, their documentation is poorly written and I wasn't able to find out a way to do so.

答案1

得分: 1

我找到了解决方法。
假设你有一个变量 stream,其中包含数据并且是 DynamicMemoryWStream 类,那么你首先需要将数据转移到 FileWStream 类,然后使用 FileWStream 将数据转移到 svg 文件。具体步骤如下:

  1. 创建一个空的 svg 文件,例如命名为 example.svg
  2. 使用以下代码创建与文件的流
file_stream = skia.FILEWStream("example.svg")
  1. 从流写入文件流
stream.writeToStream(file_stream)
  1. 最后刷新文件流,以便将所有接收到的数据写入文件
file_stream.flush()
英文:

I figured out how to solve it.
Say you have a variable stream which is contain the data and is DynamicMemoryWStream class, then you first need to transfer the data into class FileWStream, then you use FileWStream to transfer the data into the svg file. To be more specific, do the following:

  1. create an empty svg file, say name it example.svg
  2. create stream to the file with following code
file_stream = skia.FILEWStream("example.svg")
  1. write from stream to file stream
stream.writeToStream(file_stream)
  1. Finally flush the file stream so it write all data received to the file
file_stream.flush()

huangapple
  • 本文由 发表于 2023年8月4日 21:37:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定