将 json 图像嵌入到打字文档中

问题描述 投票:0回答:1

我有一个 JSON 格式的图像,我想将其直接包含在用 Typst 编写的文档中。可以做下面这样的事情吗?

#let myimage = "
 \"image\": {
    \"mime\": \"image/png\",
    \"data\": iVBORw0K[...]
  }
}"

#image(myimage)
json image typst
1个回答
0
投票

您可以使用

json
函数读取JSON文件,使用based
解码base64字符串,并使用
image.decode
解码图像数据缓冲区。

放在一起,看起来像这样:

#import "@preview/based:0.1.0": base64 #let file = json("image.json") #image.decode(base64.decode(file.image.data))
这将读取文件 

image.json

,解码其 
image.data
 字段中的 Base64 数据,并将其解码为 PNG 图像。 Typst 将使用文件中的魔术字节来自动检测格式,但您也可以在 
format
 的命名参数 
image.decode
 中显式传递格式。

本示例中的 JSON 文件如下所示:

{ "image": { "type": "image/png", "data": "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAM..." } }
您还可以使用 

json.decode

 函数从字符串而不是文件中解析 JSON。

© www.soinside.com 2019 - 2024. All rights reserved.