-1

I have this code which show me all the .tif files in a folder I give.

timestr = datetime.now().strftime('%Y%m%d-%H%M%S%f')    

ex_area_files = []
tif_files = glob.glob('C:\\Users\\Bob\\Desktop\\Folder\\' + str(timestr) + '\\EXTRACTED\\*.tif')
ex_area_files = [tif_files]
print(ex_area_files)

How can I move some specified ones (to another folder) ? I mean, I want to move all the .tif files which result of width*height is less/more than a certain value.

What I tried was to loop through the array and, after set the codition, move the files. All the result was a loop fail which blocked all the system :)

It follows...

image = cv2.imread('image.jpg')
height = np.size(image, 0)
width = np.size(image, 1)

condition = (height*width) > 9600

How can I also set ex_area_files (my .tif array) as directory of files from which cv2 can read ? And, more imporant, how to set a file at once ?

The files which satisfy the condition (images with values of 320*30px) should be moved to another directory. How to do it after the program decided that the file is ok to be moved ?

Thanks

Tip: this is a next step after this other piece of code: Exclude images of certain dimensions from process (OpenCV, Python) In this case, take a look at ex_area14.png. I want to move a series of files like that (but in .tif format..)

lucians
  • 2,239
  • 5
  • 36
  • 64
  • 1
    If you look through the documentation, you should find tools to help you get lists of files that you can iterate over or even tools that let you walk through directories. You may want to spend some time working your way through [the Tutorial](https://docs.python.org/3/tutorial/index.html). – wwii Sep 14 '17 at 16:28

1 Answers1

2

I recommend using shutil for moving files. To move them You can use shutil.copy() - I Personally use shutil.copy2()

so try something like this:

    import shutil
    import opencv

    for files in ex_area_files:
        if files (PLACE YOUR CONDITION HERE):
            `shutil.copy('PATH_OF_file', 'PATH_OF_WHERE_TO_DROP')

EDIT:

So i personally like os.walk(), here i'm looping through the files, and if files endswith .tif, I will read the file with imread get the height and width, and check if that condition is met. You'll have to provide where you want to copy the files to. (Take note of .replace() - imread for some reason likes the slashes like / instead of \)

import shutil
import opencv
import os

for root, dirs, files in os.walk(r'FOLDER HERE'):
    for file in files:
        if file.lower().endswith('.tif'):
            image = cv2.imread(root.replace('\\', '/') + '/' +file
            height = np.size(image, 0)
            width = np.size(image, 1)
            if height*width > 9600:
                shutil.copy(root.replace('\\', '/') + '/' +file, 'PATH_OF_WHERE_TO_DROP')
MattR
  • 4,887
  • 9
  • 40
  • 67
  • Thanks. It seems ok for shutil.. But how to use an array (looped for every file in the folder which satisfy condition) as PATH_OF_file? It's more than one.. Thank you – lucians Sep 14 '17 at 16:23
  • @Link, I updated my code. I don't have `.tif` files so I can't check if it works in your specific example. The issue to watch out for is if `image` returns as `None`. – MattR Sep 14 '17 at 16:41
  • Thank you so much. I will test in a few, will update this post and reply. – lucians Sep 14 '17 at 16:53
  • it works, just that copy all tif files...seems that the condition > 9600 don't work...I am looking at it now... EDIT: the if condition must be outside for loop. – lucians Sep 15 '17 at 07:55