【问题标题】:Unable to load image无法加载图片
【发布时间】:2018-05-25 14:41:53
【问题描述】:

我无法将图像从网络更改为数组。

谁能帮忙?

非常感谢。

from PIL import Image
from io import BytesIO
import requests
import numpy as np

url =r'https://drive.google.com/uc?id=1xrKyFZAIIR_XVhlhYGHlfLvIr5vQ5EnW'
img = Image.open(BytesIO(requests.get(url).content))
arr = np.array(img, dtype = np.uint8)
Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    arr = np.array(img, dtype = np.uint8)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'JpegImageFile'

【问题讨论】:

  • 下载图片并保存为.jpg格式。尝试 Scipy 库中的“imread”命令。
  • 你有错误吗?哪一个 ?我猜这取决于图像格式
  • 这真的很奇怪......第一次调用np.array(img, dtype = np.uint8) 会抛出一个ValueError,但如果你再试一次它就可以了......
  • 您要将图像文件的原始字节,还是解码后的图像数据(RGB 值)转换为数组?
  • 让@zindarod 帮助...stackoverflow.com/a/50530022/2836621

标签: python image


【解决方案1】:

感谢马克的帮助。这个问题的解决方法是:

import cv2
import requests
import numpy as np

url =r'https://drive.google.com/uc?id=1xrKyFZAIIR_XVhlhYGHlfLvIr5vQ5EnW'
resp = requests.get(url).content
image = np.asarray(bytearray(resp), dtype="uint8")
arr = cv2.imdecode(image, cv2.IMREAD_COLOR)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多