0

I'm so much newbie in openCV/Python tasks. I use Python 3.7 and openCV 4 running by a JNotebook. The question: I wanna save just 1,000 images from a dataset with 10,000 pictures, extracting them from it and write only those 1,000.jpeg in a new folder, is it possible using openCV package in Python? I've already had a list of names (1,000 images).

  • 2
    If you are not doing anything else with the image (some image processing) then it is just copying the file, take a look to [this post](https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) – api55 Jan 23 '19 at 13:23

2 Answers2

3

If you need just to copy files, you even don't need OpenCV tools:

out_folder_path = '...'
in_folder_path = '...'
images_to_save_names = [...]
for image_name in images_to_save_names:
    cur_image_path = os.path.join(in_folder_path, image_name)
    cur_image_out_path = os.path.join(out_folder_path, image_name)
    shutil.copyfile(cur_image_path, cur_image_out_path)

If you have image names and their binary data from some specific DS file(.csv, .hdf, e.t.c.), you can use cv2.imwrite(path, image) instead of copying.

D. Ermolaev
  • 148
  • 1
  • 6
2

Assuming you have OpenCV correctly installed on your machine, you can first read the images with img = cv.imread(filename) and then write them with cv.imwrite(filename, img).

Jorge Ribeiro
  • 56
  • 1
  • 5
  • It will be 2 requests to FS, which is slower, than just copying. If there is no image processing, then there is no need to read it. – D. Ermolaev Jan 23 '19 at 13:31
  • 1
    @D.Ermolaev Not to mention the overhead in decoding/encoding the image format, and degradation of quality of the images due to the lossy codec. How this gets upvotes... – Dan Mašek Jan 23 '19 at 16:48