简体   繁体   中英

How to read an image and that image copy to another directory?

I read an image with OpenCV for processing and copy that image to another directory folder. But I get following error. How can solve this and can copy it perfectly?

    image_file = os.path.join("<ROOT_PATH>", file_)
    image = cv2.imread(image_file, cv2.IMREAD_UNCHANGED)
    image = np.stack([image]*3, axis=-1)  # make image 3-channel
    crop_img = image[int(ymin):int(ymax), int(xmin):int(xmax)]
    plt.imshow(crop_img)
    shutil.copy2(crop_img, "<dst_folder>")

Error:

TypeError: expected str, bytes or os.PathLike object, not numpy.ndarray

shutil copies file objects directly. Specifically, copy2 will copy the contents of a file directly, which can be in string, bytes, or os.PathLike form. Since you are modifying the image with opencv, and you have the numpy form, it might be easier to just call imsave :

cv2.imwrite(os.path.join("<dst_folder>", os.path.basename("<ROOT_PATH>")), crop_img)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM