【问题标题】:AttributeError: 'numpy.ndarray' object has no attribute 'save'AttributeError:“numpy.ndarray”对象没有属性“保存”
【发布时间】:2020-11-10 01:17:10
【问题描述】:

我有裁剪图像的短代码文件夹中的所有图像,我使用 opencv 标记并保存为 csv,如下所示:

import os, sys
from PIL import Image
import cv2
import pandas as pd

# The annotation file consists of image names, text label, 
# bounding box information like xmin, ymin, xmax and ymax.
ANNOTATION_FILE = 'data/annot_crop_plate.csv'
df = pd.read_csv(ANNOTATION_FILE)

#image directory path
IMG_DIR = 'data/images'
# The cropped images will be stored here
CROP_DIR = 'data/crops'

files = df['filename']

size = (200,200)

for file in files:
    print(file)
    img = cv2.imread(IMG_DIR +'/' + file)
    annot_data = df[df['filename'] == file]
    xmin = int(annot_data['xmin'])
    ymin = int(annot_data['ymin'])
    xmax = int(annot_data['xmax'])
    ymax = int(annot_data['ymax'])
    crop = img[ymin:ymax,xmin:xmax]
    new_crop = cv2.resize(crop, dsize=size, interpolation=cv2.INTER_CUBIC)
    new_crop.save(CROP_DIR + '/' + file.split('.')[0] + '.png', 'PNG', quality=90)

但在行尾已经说“AttributeError:'numpy.ndarray'对象没有属性'save'”

【问题讨论】:

标签: python pandas opencv


【解决方案1】:

尝试使用

cv2.imwrite(path,img_to_save)

在最后一行。

【讨论】:

    【解决方案2】:
    img = cv2.imread(IMG_DIR +'/' + file)
    

    如果您需要使用PIL 保存文件,您必须需要使用PIL 加载图像

    img = Image.open(IMG_DIR +'/' + file)
    
    
    
    img.save('image.png')
    

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2020-11-29
      • 2020-10-06
      • 2018-01-25
      • 2016-06-29
      • 2020-03-25
      • 2013-12-07
      • 2017-10-16
      • 2020-02-23
      相关资源
      最近更新 更多