从具有 4 个通道的流中读取 PNG 图像
Read PNG image from stream with 4 channels
我正在开发一个 discord 机器人,它可以接受各种用于 OCR 目的的图像。当用户上传具有 4 个通道的 PNG 时,它会出错。我的意思是它读取的图像几乎是 100% 的白色,而实际图像充满了色彩。
我尝试用 cv2.COLOR_BGRA2BGR 转换它,但这不起作用。但是在我的 pytest 中它确实有效。这可能是因为我在我的 pytest 中使用 cv2.imread 这与我的实际代码有点不同。
代码(已损坏)
for a in supported_attachments:
async with aiohttp.ClientSession() as session:
async with session.get(a) as res:
if res.status == 200:
buffer = io.BytesIO(await res.read())
arr = np.asarray(bytearray(buffer.read()), dtype=np.uint8)
img = cv2.imdecode(arr, -1)
if len(img.shape) > 2 and img.shape[2] == 4:
print('convert')
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
images.append(img)
pytest(有效)
image = cv2.imread('../tests/images/stats/english/kills_png.png',
cv2.COLOR_BGRA2BGR)
有什么建议或想法吗?
标记 cv2.COLOR_BGRA2BGR
用于 cv2.cvtColor()
而非 cv2.imread()
。
cv2.imread()
的所有清单常量都以 cv2.IMREADxxx
开头并定义为 here。
我正在开发一个 discord 机器人,它可以接受各种用于 OCR 目的的图像。当用户上传具有 4 个通道的 PNG 时,它会出错。我的意思是它读取的图像几乎是 100% 的白色,而实际图像充满了色彩。
我尝试用 cv2.COLOR_BGRA2BGR 转换它,但这不起作用。但是在我的 pytest 中它确实有效。这可能是因为我在我的 pytest 中使用 cv2.imread 这与我的实际代码有点不同。
代码(已损坏)
for a in supported_attachments: async with aiohttp.ClientSession() as session: async with session.get(a) as res: if res.status == 200: buffer = io.BytesIO(await res.read()) arr = np.asarray(bytearray(buffer.read()), dtype=np.uint8) img = cv2.imdecode(arr, -1) if len(img.shape) > 2 and img.shape[2] == 4: print('convert') img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.show() images.append(img)
pytest(有效)
image = cv2.imread('../tests/images/stats/english/kills_png.png', cv2.COLOR_BGRA2BGR)
有什么建议或想法吗?
标记 cv2.COLOR_BGRA2BGR
用于 cv2.cvtColor()
而非 cv2.imread()
。
cv2.imread()
的所有清单常量都以 cv2.IMREADxxx
开头并定义为 here。