You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Python与JavaScript调用OpenCV读取同一张图片结果不一致?

4000×4000像素图片:

enter image description here

Python代码:

import cv2
pic_data = cv2.imread(path_to_picture)

读取得到的pic_data:

pic_data
Out[21]: 
array([[[125, 189, 204],
        [125, 189, 204],
        [125, 189, 204],
        ...,
        [125, 189, 204],
        [125, 189, 204],
        [125, 189, 204]],

JavaScript代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
      <input type="file" id="js-input">
      <img id="js-img" />
    <script>
      function onOpenCvReady() {
        const input = document.getElementById('js-input');
        const img = document.getElementById('js-img');
        input.onchange = function () {
          const file = event.target.files[0];
          img.src = URL.createObjectURL(file);
          console.log(file)
        }
        img.onload = function () {
          const res = cv.imread(img);
          console.log(res)
        }
        console.log('ready', cv)
      }
    </script>
    <script src="https://docs.opencv.org/4.7.0/opencv.js" onload="onOpenCvReady()" type="text/javascript"></script>
  </body>
</html>

使用Chrome打开该JavaScript代码并加载图片后,读取到的数据如下:
enter image description here

问题描述

(忽略通道顺序不一致的情况)

Python代码读取到的第一个像素值为[125, 189, 204]
JavaScript代码读取到的第一个像素值为[126, 190, 205]

用Python和JavaScript读取同一张图片,得到的结果不一致,不清楚原因,希望实现两者读取结果一致。

更新补充

参考相关回答和评论后,随机选取其他几张图片测试,发现Python与JavaScript的读取结果一致。仅这张4000×4000像素的图片存在问题,其他图片均正常。

例如图片1.png:
Python代码读取到的第一个像素值为[245, 213, 154]
JavaScript代码读取到的第一个像素值为[245, 213, 154]

图片1.png

enter image description here

图片2.png

enter image description here

图片3.jpg

enter image description here


内容的提问来源于stack exchange,提问作者sun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 00:35:16