Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
73 views
in Technique[技术] by (71.8m points)

python - 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

question from:https://stackoverflow.com/questions/65845850/how-to-read-an-image-and-that-image-copy-to-another-directory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...