首页 > 其他分享 >openCV 图片转为base64

openCV 图片转为base64

时间:2024-04-30 17:56:42浏览次数:21  
标签:std Base64 string image base64 openCV include 转为

在Python中,使用OpenCV处理图片并将其转换为Base64编码的字符串可以通过以下步骤实现:

  1. 使用OpenCV读取图片。

  2. 使用cv2.imencode将图片转换为字节流。

  3. 使用base64库将字节流编码为Base64字符串。

以下是实现这一功能的示例代码:

import cv2
import numpy as np
import base64
 
# 读取图片
image = cv2.imread('path_to_image.jpg')
 
# 将图片转换为JPG格式的字节流
_, buffer = cv2.imencode('.jpg', image)
 
# 将字节流编码为Base64字符串
base64_image = base64.b64encode(buffer)
 
# 将Base64字符串转化为Python字符串
base64_image_str = base64_image.decode('utf-8')
 
print(base64_image_str)

 

在C++中使用OpenCV将图片转换为Base64编码,你可以使用以下步骤:

  1. 使用OpenCV读取图片。

  2. 将图片编码为字节流(例如PNG或JPEG)。

  3. 对编码后的字节流进行Base64编码。

以下是一个简单的示例代码,展示了如何将图片转换为Base64编码:

#include <opencv2/opencv.hpp>
#include <vector>
#include <string>
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui_c.h>
 
#include <opencv2/core/utils/base64.hpp>
 
std::string encodeBase64(const std::vector<uchar>& data) {
    std::string base64_string;
    cv::base64::encode(data, base64_string);
    return base64_string;
}
 
std::string imageToBase64(const std::string& imagePath) {
    cv::Mat img = cv::imread(imagePath, cv::IMREAD_COLOR);
    std::vector<uchar> buff;
    cv::imencode(".png", img, buff);
    return encodeBase64(buff);
}
 
int main() {
    std::string imagePath = "path_to_your_image.jpg";
    std::string base64_string = imageToBase64(imagePath);
    std::cout << "Base64 Encoded String: " << base64_string << std::endl;
    return 0;
}

 

标签:std,Base64,string,image,base64,openCV,include,转为
From: https://www.cnblogs.com/FBsharl/p/18168486

相关文章

  • opencv距离变换函数distanceTransform
    当图像内的各个子图没有连接时,可以直接使用形态学的腐蚀操作确定前景对象,但是如果图像内的子图连接在一起时,就很难确定前景对象了。此时,借助于距离变换函数cv2.distanceTransform()可以方便地将前景对象提取出来。距离变换函数cv2.distanceTransform()计算二值图像内任意点到最......
  • base64转file文件的两种方式
    base64加载图片文件使用base64可以不发送请求将图片文件转换为base64格式的链接渲染到图片上,减少服务器访问次数,下面是base64加载图片的方式document.getElementById("front-file").onchange=(e)=>{constfile=e.target.files[0];constreader=newFileReader()......
  • uniapp将图片base64绘制到画布中
     html<viewclass="content"><canvascanvas-id="myCanvas"style="width:300px;height:300px;"></canvas></view> js//获取图片的完整base64this.qrurl=res.data......
  • 浅析OpenCV分水岭变换watershed函数的markers参数[C++]
    0.前言本文是笔者在学习C++OpenCV库时学习心得,在学习分水岭变换函数时,由于缺少相关学习资料,导致笔者理解吃力,故写此文章阐述一下对该函数的理解,希望对其他学习人士提供帮助。本文主要介绍了watershed函数参数以及参数实际表示。请您按文章次序阅读。您需要提前了解的相关知......
  • vue3 把quill的base64变成图片地址
      你可以看看https://www.kancloud.cn/liuwave/quill/1434141或者看看别人的文章我的项目是vu3的 template的是这样的<el-form-itemlabel="中文详情"prop="content"><div><quill-editorref="QuillEditor"v-model:content="form.......
  • 在Python中将字典转为成员变量的方法
    当我们在Python中写一个class时,如果有一部分的成员变量需要用一个字典来命名和赋值,此时应该如何操作呢?这个场景最常见于从一个文件(比如json、npz之类的文件)中读取字典变量到内存当中,再赋值给一个类的成员变量,或者已经生成的实例变量。使用__dict__定义成员变量在python中直接支......
  • openEuler操作系统安装OpenCV 3.4.10
    安装依赖JDK要求需求java17系统要求centos8centos7需要自行升级glibc到某个版本,哪一个忘记了,风险很大,不要尝试!基础依赖#缺什么就下什么,其中cmake是最重要的yum-yinstallepel-releaseyuminstallgccgcc-c++yuminstallcmakeyuminstallepel-releaseyum......
  • 循序栈练习题:十进制转为十六进制
    数据结构练习题:进制转换/*********************************************************************************************************@filename: DEXchangetoHEX.c*@brief实现十进制转换为十六进制*@[email protected]*@date2024/04/25*@ve......
  • openCV 图像清晰度检测
    图像清晰度评价算法有很多种,在空域中,主要思路是考察图像的领域对比度,即相邻像素间的灰度特征的梯度差;在频域中,主要思路是考察图像的频率分量,对焦清晰的图像高频分量较多,对焦模糊的图像低频分量较多。这里实现3种清晰度评价方法,分别是Tenengrad梯度方法、Laplacian梯度方法和方差......
  • Centos7 下安装OpenCV 3.4.6 (本人参考这个步骤,在openEuler操作系统安装了OpenCV 3.4.1
    Centos7下安装OpenCV3.4.6 转自 https://blog.csdn.net/MaXiaoTiancsdn/article/details/115336499(本人参考这个步骤,在openEuler操作系统安装了OpenCV3.4.10的版本)  1.参考资料本文主要参考自以下文章,按照这篇文章的安装过程十分顺利,网上的贴子很多但是很......