ValueError: cannot reshape array of size xxx into shape (xxx,xxx,xxx)解决方法

报错:

ValueError: cannot reshape array of size 149184 into shape (28,28,1)

报错原因:

由于图片的w,h,c相乘等于149184所导致的。无法将大小为149184的数组重塑为形状(28,28,1)

解决方法:

我输入的图片shape为(224, 222, 3) 所以224 * 222 * 3 = 149184

通过opencv去改变图片的size

	#resize图片大小 先将原本的(224,222,3) ---> (28,28,3)
    pred_img = cv2.resize(pred_img,(28,28))                          
    #转换np数组格式
    pred_img = np.array(pred_img)   
    #重新reshape图片                      
    pred_img = pred_img.reshape(-1,28,28,1)
    #查看reshape后的图片shape                       
    print(pred_img.shape)








😍😍😍

若本文对你有所帮助不妨点个赞点➕关注

  • 17
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
这个错误提示表明,您尝试将大小为1的数组重新形状为(417,556),但这是不可能的。出现这种情况的原因可能是在图像压缩或解压缩过程中出现了错误,导致数据大小不正确。 为了解决这个问题,您可以检查图像压缩或解压缩的过程,并确保数据的大小正确。您可以使用以下代码来调试并查看压缩后的数据的大小: ```python import numpy as np from PIL import Image import lzss def compress_gray_image(image_path, compression_level): # 读取输入图像并转换为灰度图像 img = Image.open(image_path).convert('L') # 转换为一维的numpy数组 data = np.array(img).flatten() # 使用lzss算法进行压缩 compressed_data = lzss.compress(data, compression_level) # 输出压缩后的数据大小 print('Compressed data size:', len(compressed_data)) # 将压缩后的数据写入到输出文件中 with open('compressed.bin', 'wb') as f: f.write(compressed_data) def decompress_gray_image(compressed_image_path): # 读取压缩后的数据 with open(compressed_image_path, 'rb') as f: compressed_data = f.read() # 输出压缩后的数据大小 print('Compressed data size:', len(compressed_data)) # 使用lzss算法进行解压缩 decompressed_data = lzss.decompress(compressed_data) # 输出解压缩后的数据大小 print('Decompressed data size:', len(decompressed_data)) # 将解压缩后的一维数组转换为二维数组 h, w = 417, 556 # 假设图像大小为417x556 data = np.reshape(decompressed_data, (h, w)) # 将二维数组转换为图像并显示 img = Image.fromarray(data.astype('uint8'), mode='L') img.show() # 测试代码 compress_gray_image('lena.png', compression_level=2) decompress_gray_image('compressed.bin') ``` 在上述代码中,我们分别在压缩和解压缩函数中输出了压缩后和解压缩后的数据大小,以便于您检查数据是否正确。如果数据大小不正确,您可以检查图像大小、压缩级别等参数是否正确,并尝试调整它们。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

令人作呕的溏心蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值