在使用图片base64编码时遇到RuntimeError: Given groups=1, weight of size [64, 3, 6, 6], expected input[1, 4, 896, 1280] to have 3 channels, but got 4 channels instead 问题
解决方法
- 该问题出现在png格式的图片base64编码后解码为数组格式时会出现通道数为4的情况,需要将解码的数组用函数convert(‘RGB’)做RGB的转换现在将代码给出如下:
// 将png编码的base64字符串解码为数组格式
def imgStr2imgArr(img_str):
'''
img_str: base64图片字符串
'''
buff = BytesIO(base64.b64decode(img_str))
image = Image.open(buff).convert('RGB')
img_arr = np.array(image)
return img_arr