首页 > 解决方案 > cv2.imread 更改图像中的 base64 字符串并在操作系统之间生成不同的结果

问题描述

我在 MacOS 和 Linux Ubuntu 系统上的 Python 中使用 OpenCV。我的 OpenCV 版本是 3.4.1.15。

我分别在两个操作系统系统上尝试了三种为图像生成 base64 字符串的不同方法:

  1. 使用纯 Python:

    from base64 import b64encode
    with open("image_file.jpg", "rb") as file:
        base64string = b64encode(file.read()).decode("UTF-8")
    
  2. 使用cv2.imread

    import cv2
    from base64 import b64encode
    cvbase64string = b64encode(cv2.imencode('.jpg', cv2.imread(image_file.jpg))[1]).decode("UTF-8")
    
  3. 使用后使用纯 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 字符串会发生变化。

  1. 在同一个操作系统(MacOS 或 Linux)上,base64string!= cvbase64string== iobase64string
  2. 跨操作系统MAC_base64string== LINUX_base64string,但MAC_cvbase64string!=LINUX_cvbase64stringMAC_iobase64string!= LINUX_iobase64string

这是为什么?有什么办法可以解决吗?还是需要报告的错误?

这让我很困扰,因为我在不同平台上开发 OCR 算法,但不同的 base64 字符串会从 Google Vision API 产生不同的识别字符,这意味着我什至无法从同一张图像中重现我的 OCR 结果。

标签: pythonopencvbase64cross-platform

解决方案


推荐阅读