opencv读取图片转二进制并进行bs64编码
时间: 2023-08-02 19:05:22 浏览: 42
好的,您可以使用OpenCV库来读取图片,然后使用Python自带的base64库进行编码,代码如下:
```python
import cv2
import base64
# 读取图片
img = cv2.imread('image.jpg')
# 将图片转为二进制
_, img_binary = cv2.imencode('.jpg', img)
# 进行base64编码
b64_encoded = base64.b64encode(img_binary)
# 打印编码结果
print(b64_encoded)
```
请将代码中的 `image.jpg` 替换为您要转码的图片路径,然后运行即可。
相关问题
opencv imdecode读取二进制图片后进行图像等比例放缩
使用 OpenCV 的 `imdecode` 函数读取二进制图片后,可以使用 `resize` 函数对图像进行等比例放缩。
具体步骤如下:
1. 使用 `imdecode` 函数读取二进制图片,并将其存储为 `img`。
```python
import cv2
import numpy as np
# 读取二进制图片
data = open('image.bin', 'rb').read()
nparr = np.fromstring(data, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
```
2. 使用 `resize` 函数对图像进行等比例放缩。例如,将图像缩小为原来的一半。
```python
# 缩小图像为原来的一半
img_resized = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
```
在上述代码中,`fx` 和 `fy` 分别表示水平和垂直方向的缩放比例,`interpolation` 参数为插值方法,这里选择了 `cv2.INTER_AREA`。
3. 可以将等比例放缩后的图像进行保存或显示。
```python
# 保存等比例缩放后的图像
cv2.imwrite('resized_image.jpg', img_resized)
# 显示等比例缩放后的图像
cv2.imshow('resized image', img_resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
python opencv 读取图片并进行旋转
可以使用 OpenCV 的 Python 接口,读取图片并进行旋转。以下是示例代码:
```python
import cv2
# 读取图片
img = cv2.imread('image.jpg')
# 获取图片宽度和高度
height, width = img.shape[:2]
# 设置旋转中心点
center = (width // 2, height // 2)
# 设置旋转角度
angle = 45
# 计算旋转矩阵
M = cv2.getRotationMatrix2D(center, angle, 1.0)
# 执行旋转操作
rotated = cv2.warpAffine(img, M, (width, height))
# 显示旋转后的图片
cv2.imshow('Rotated Image', rotated)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
上述代码中,首先使用 `cv2.imread()` 函数读取图片,然后使用 `cv2.getRotationMatrix2D()` 函数计算旋转矩阵,最后使用 `cv2.warpAffine()` 函数执行旋转操作。注意,旋转角度是以逆时针方向为正方向的。