7

i have short code for crop image all image in folder that i labeled and save as csv using opencv like this:

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)

but in the end of line have said "AttributeError: 'numpy.ndarray' object has no attribute 'save'"

2
  • I think you're supposed to use cv2.imsave('filename', new_crop)? Can't test that though as don't have cv2 available Commented Mar 27, 2020 at 2:12
  • 2
    docs.opencv.org/2.4/modules/highgui/doc/… Commented Mar 27, 2020 at 2:19

2 Answers 2

15

try using

cv2.imwrite(path,img_to_save)

in the last line.

Sign up to request clarification or add additional context in comments.

Comments

1
img = cv2.imread(IMG_DIR +'/' + file)

if you need to save file using PIL you must need to load the image using PIL

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



img.save('image.png')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.