python ‘PngImageFile‘ object has no attribute ‘shape‘

该博客介绍了在Python中使用PIL库处理`PngImageFile`对象时,如何通过`np.asarray()`转换为数组来获取图像的shape属性。此外,还展示了如何使用`Image.open()`打开图片,`image.crop()`进行图像裁剪,并利用`cv2.imwrite()`保存裁剪后的PNG图像。裁剪的坐标遵循(x, y, x+w, y+h)的格式。
摘要由CSDN通过智能技术生成
python 'PngImageFile' object has no attribute 'shape'

Image.open打开的图片类型为’PngImageFile’,不能直接img.shape查看维度,要转成数组,即可查看

def crop_img(img_path):
    save_path = r'E:\Code-\ir_detection\test_gan_neg_112_0422'
    img_name = img_path.split('\\')[-1]
    image = Image.open(img_path) #(128,128)
    image = image.crop([8,8,120,120])
    image = np.asarray(image)
    print('image.shape',image.shape)
    print('save_path', save_path+'\\'+img_name)
    #image.save(save_path+'\\'+img_name)
    #image = cv2.merge([image, image, image])
    cv2.imwrite(save_path+'\\'+img_name, image)

另外,Image方法中的crop对应的四个点坐标为(x, y, x+w, y+h)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我现在强的可怕~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值