当前位置: 首页 > news >正文

svg文本转为对应图片的base64编码

转换脚本(node)

npm init -y
npm install express sharp body-parser

const express = require('express');
const bodyParser = require('body-parser');
const sharp = require('sharp');const app = express();
const port = 5000;// 配置body-parser以接受JSON格式的数据
app.use(bodyParser.json());app.post('/convert-svg', async (req, res) => {try {// 从请求体中获取svg_contentconst svg = req.body.svg_content;const pngBuffer = await sharp(Buffer.from(svg)).toFormat('png').toBuffer();const base64PNG = pngBuffer.toString('base64');res.send(base64PNG);} catch (error) {console.error('Error converting SVG to PNG:', error);res.status(500).send('An error occurred during the conversion process.');}
});app.listen(port, () => {console.log(`Server running at http://localhost:${port}`);
});

python测试脚本

import requests
import json# SVG文件的路径
svg_file_path = 'svgfile.svg'# Node.js服务器的URL
url = 'http://localhost:5000/convert-svg'# 从文件中读取SVG内容
with open(svg_file_path, 'r') as file:svg_content = file.read()# 构造JSON数据
data = {"svg_content": svg_content}# 发送POST请求
response = requests.post(url, json=data)# 检查响应
if response.status_code == 200:# 打印Base64编码的PNG图像print("Base64 PNG:", response.text)
else:print("Error:", response.status_code, response.text)

相关文章:

  • 高防CDN在游戏服务商中的关键意义
  • crmeb v5新增一个功能的完整示例记录
  • 【迅搜09】索引管理(二)增删改操作
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • Debezium发布历史08
  • AIGC(生成式AI)试用 15 -- 小结
  • 风速预测(五)基于Pytorch的EMD-CNN-LSTM模型
  • Android动画
  • 【跨域】同源策略、跨域解决
  • 什么是PHP中的数组迭代器?
  • 2043杨辉三角(C语言)
  • 前端实现一个时间区间内,再次单选功能,使用Antd组件库内日历组件Calendar
  • 13、ble_mesh_vendor_model 客户端,自定义模型
  • 【自动化测试】web3py 连接 goerli
  • SpringBoot零基础入门到项目实战——学习路线规划与目录结构
  • 《深入 React 技术栈》
  • 【comparator, comparable】小总结
  • 77. Combinations
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • Druid 在有赞的实践
  • happypack两次报错的问题
  • Java基本数据类型之Number
  • Swift 中的尾递归和蹦床
  • 从0到1:PostCSS 插件开发最佳实践
  • 复习Javascript专题(四):js中的深浅拷贝
  • 记一次删除Git记录中的大文件的过程
  • 看域名解析域名安全对SEO的影响
  • 普通函数和构造函数的区别
  • 如何在GitHub上创建个人博客
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • MyCAT水平分库
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • ​人工智能书单(数学基础篇)
  • # 计算机视觉入门
  • #13 yum、编译安装与sed命令的使用
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)springboot 个人网页的网站 毕业设计031623
  • (附源码)计算机毕业设计ssm电影分享网站
  • (过滤器)Filter和(监听器)listener
  • (利用IDEA+Maven)定制属于自己的jar包
  • (算法二)滑动窗口
  • (推荐)叮当——中文语音对话机器人
  • (学习日记)2024.03.25:UCOSIII第二十二节:系统启动流程详解
  • (一)Linux+Windows下安装ffmpeg
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .describe() python_Python-Win32com-Excel
  • .NET Micro Framework初体验(二)
  • .NET 反射的使用
  • .ui文件相关
  • @EnableConfigurationProperties注解使用
  • @font-face 用字体画图标
  • @private @protected @public