You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用patchify生成图像块时遭遇PIL TypeError问题求助

解决patchify生成图像块时的TypeError问题

问题详情

使用patchify库切割图像时触发TypeError: Cannot handle this data type: (1, 1, 299, 3), |u1错误,尝试调整维度后无效。patches.shape输出为(7, 5, 1, 299, 299, 3)。

原代码

import numpy as np
from patchify import patchify
from PIL import Image
import cv2
#ocean =Image.open("ocean.jpg") #612 X 408
ocean =cv2.imread("/kaggle/input/supercooldudeslolz/new_300.jpg")
ocean = cv2.resize(ocean, (1495, 2093))
print(ocean.size)
ocean = np.asarray(ocean)
patches =patchify(ocean,(299,299, 3),step=299)
print(patches.shape)
for i in range(patches.shape[0]):
    for j in range(patches.shape[1]):
        patch = patches[i, j]
        patch = Image.fromarray(patch)
        num = i * patches.shape[1] + j
        patch.save(f"patch_{num}.jpg")

完整报错信息

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /opt/conda/lib/python3.10/site-packages/PIL/Image.py:3089, in fromarray(obj, mode)
   3088 try:
-> 3089     mode, rawmode = _fromarray_typemap[typekey]
   3090 except KeyError as e:

KeyError: ((1, 1, 299, 3), '|u1')

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
Cell In[16], line 15
     13 for j in range(patches.shape[1]):
     14     patch = patches[i, j]
---> 15     patch = Image.fromarray(patch)
     16     num = i * patches.shape[1] + j
     17     patch.save(f"patch_{num}.jpg")

File /opt/conda/lib/python3.10/site-packages/PIL/Image.py:3092, in fromarray(obj, mode)
   3090     except KeyError as e:
   3091         msg = "Cannot handle this data type: %s, %s" % typekey
-> 3092         raise TypeError(msg) from e
   3093 else:
   3094     rawmode = mode

TypeError: Cannot handle this data type: (1, 1, 299, 3), |u1

解决方案

问题根源

patchify返回的每个图像块多了两个冗余的单维度(对应shape中的(1,1)),而PIL的Image.fromarray仅支持(高度,宽度,通道)的三维数组,无法识别这种五维子数组。

修改后的代码

import numpy as np
from patchify import patchify
from PIL import Image
import cv2

ocean = cv2.imread("/kaggle/input/supercooldudeslolz/new_300.jpg")
ocean = cv2.resize(ocean, (1495, 2093))
ocean = np.asarray(ocean)
patches = patchify(ocean, (299, 299, 3), step=299)
print(patches.shape)

for i in range(patches.shape[0]):
    for j in range(patches.shape[1]):
        # 移除所有单维度,将(1,1,299,299,3)转为(299,299,3)
        patch = patches[i, j].squeeze()
        # 可选:cv2默认读取BGR格式,转为RGB让PIL保存正确颜色
        patch = cv2.cvtColor(patch, cv2.COLOR_BGR2RGB)
        patch = Image.fromarray(patch)
        num = i * patches.shape[1] + j
        patch.save(f"patch_{num}.jpg")

替代方法

如果不想用squeeze(),也可以直接索引到有效维度,效果完全一致:

patch = patches[i, j, 0, 0]

内容的提问来源于stack exchange,提问作者NikoMolecule

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 13:44:52