python - cv2.imread 更改图像中的 base64 字符串并在操作系统之间生成不同的结果
问题描述
我在 MacOS 和 Linux Ubuntu 系统上的 Python 中使用 OpenCV。我的 OpenCV 版本是 3.4.1.15。
我分别在两个操作系统系统上尝试了三种为图像生成 base64 字符串的不同方法:
使用纯 Python:
from base64 import b64encode with open("image_file.jpg", "rb") as file: base64string = b64encode(file.read()).decode("UTF-8")
使用
cv2.imread
:import cv2 from base64 import b64encode cvbase64string = b64encode(cv2.imencode('.jpg', cv2.imread(image_file.jpg))[1]).decode("UTF-8")
使用后使用纯 Python得到
cv2.imwrite
什么cv2.imread
:import cv2 from base64 import b64encode cv2.imwrite("ioimage_file.jpg", cv2.imread("image_file.jpg")) with open("ioimage_file.jpg", "rb") as file: iobase64string = b64encode(file.read()).decode("UTF-8")
用我的肉眼,我无法区分“ioimage_file.jpg”和“image_file.jpg”。但是,base64 字符串会发生变化。
- 在同一个操作系统(MacOS 或 Linux)上,
base64string
!=cvbase64string
==iobase64string
。 - 跨操作系统
MAC_base64string
==LINUX_base64string
,但MAC_cvbase64string
!=LINUX_cvbase64string
和MAC_iobase64string
!=LINUX_iobase64string
。
这是为什么?有什么办法可以解决吗?还是需要报告的错误?
这让我很困扰,因为我在不同平台上开发 OCR 算法,但不同的 base64 字符串会从 Google Vision API 产生不同的识别字符,这意味着我什至无法从同一张图像中重现我的 OCR 结果。
解决方案
推荐阅读
- spring-boot - 我应该选择哪个 graphql-spring-boot-starter?
- graphql - 如何简化graphql响应
- python - [WinError 2] python Selenium webDriver中的FileNotFoundError
- continuous-integration - 用于 create-react-app 和 GitFlow 的 Azure DevOps 发布管道?
- sql - oracle如何将表从一个表空间复制到另一个表空间?
- php - PHP多维关联数组值更新
- google-bigquery - 我想在 Bigquery 中划分两列
- c# - .Net Framework mscorlib.dll GetProperty 在使用 Microsoft.Core.Avro 序列化对象时由于其缓存而导致错误
- azure - 如何在开发 Azure Function 版本 2 时在 local.settings.json 中配置“ConsumerGroup”
- javascript - 检测 webapp 上是否启用了 UDP 或 TCP