WebGL:使用 tf.browser.fromPixels 转换本地图像时 INVALID_VALUE

WebGL: INVALID_VALUE when using tf.browser.fromPixels to convert a local image

执行以下代码后:

  var myImage = new Image(48,48);
  myImage.src = '1.png';
  document.body.appendChild(myImage);
  console.log(tf.browser.fromPixels(myImage));

出现一条警告消息:

WebGL: INVALID_VALUE: texImage2D: no image

虽然图像已成功加载并显示在 html 页面上,但转换似乎失败了。如何解决?

通常您需要等待图片加载。

const myImage = new Image();
myImage.onload = function() {
  console.log(tf.browser.fromPixels(myImage));
};
myImage.src = '1.png';
document.body.appendChild(myImage);