1

First, here is the code I have:

import cv2
import os,errno
from os.path import expanduser
from datetime import datetime
import glob
import numpy as np

##################### Creating folder & subfolders #####################

b_temp_folder = 'b_temp'
c_path = os.path.join('C:\\', b_temp_folder)

try:
    c_dir = os.mkdir(c_path)
except OSError as e:
    if e.errno != errno.EEXIST:
        raise

now = datetime.now()

id_folder = now.strftime('%Y%m%d-%H%M%S-%f')
b_path = os.path.join('C:\\b_temp\\',id_folder)
b_dir = os.mkdir(b_path)


##################### Images processing #####################

for img in glob.glob('C:\\Users\\User\\Desktop\\Images\\*.tif'):
    cv_img = cv2.imread(img)
    cv_show = cv2.imshow('img',cv_img)
    cv2.waitKey(0)

In the first part, some folders are being created. First a folder in 'C:\' and inside it, a folder with datetime as name. These last are created everytime the code is executed, so they are unique (sorted by milliseconds..).

Running the for will show all images at once.

Here is the question: how can I sent every image to it's unique folder ? I mean, the first image to the first unique folder, and so on..

Going by logic, folders are being created every time all the code run (one folder at the time). The images, instead, are iterated all in the same instance. How can I modify this ?

P.S - As you can see, I created this temp folder in the C drive. My plan was to make this code a little bit cross-platform. I tried the os.path.expanduser but it seems not working. No folder is created at all... Someone know why ?

Thanks

BlueTrack
  • 442
  • 3
  • 7
  • 19

2 Answers2

1

Here is what I've done. The code create a main folder, a unique folder inside the main and transfer a file at once.

import cv2
import os,errno
from os.path import expanduser
from datetime import datetime
import glob
import numpy as np
import shutil


print('\ncreating main folder (temp) - "b_temp" IN C: \n')

b_temp_folder = 'b_temp'
c_path = os.path.join('C:\\', b_temp_folder)

try:
    c_dir = os.mkdir(c_path)
except OSError as e:
    if e.errno != errno.EEXIST:
        raise

print('creating unique folder in main folder - "id_folder" IN "b_temp" \n')


now = datetime.now()

id_folder = now.strftime('%Y%m%d-%H%M%S-%f')
b_path = os.path.join('C:\\b_temp\\',id_folder)
b_dir = os.mkdir(b_path)


print('moving image to unique folder \n')


idx = 0
for img in glob.glob('C:\\Users\\User\\Desktop\\Images\\' + str(idx) + '*.tif'):
    cv_img = cv2.imread(img)
idx+=1

shutil.move(img, b_path)

Now, someone know how to make all the code more cross-platform ? I put all the files in C:\, but it doesn't seems to be a good solution...

BlueTrack
  • 442
  • 3
  • 7
  • 19
0

I would suggest create folder in the for loop itself. You will have access of one image at a time and one folder(if this is what I understood from your description).

for img in glob.glob('C:\\Users\\User\\Desktop\\Images\\*.tif'):
    cv_img = cv2.imread(img)
    cv_show = cv2.imshow('img',cv_img)
    now = datetime.now()
    id_folder = now.strftime('%Y%m%d-%H%M%S-%f')
    b_path = os.path.join('C:\\b_temp\\',id_folder)
    b_dir = os.mkdir(b_path)
    shutil.move(img, 
              b_path)

Updated code. image file from the 'img' path will be stored in b_path. It worked for me.

gB08
  • 182
  • 2
  • 10
  • Yes, one image for every folder created. But the code doesn't seems to work... cv_show is not a folder, just a commnd to show the images.. – BlueTrack Jul 20 '17 at 09:54
  • Still don't work. I think I have to change nearly all... Maybe the solution could be to put a counter and do an image at once, one for every run of the program. After, I'll do a main which call the code and iterate it as many times as there are images in the folder.. – BlueTrack Jul 20 '17 at 12:26
  • Updated code. This one worked for me. pls give it a shot. – gB08 Jul 21 '17 at 05:31
  • Thank you. Believe me or not, it still not working. No images are being transfered. Instead I did some changes in my code and it works with just one image at the time (create main folder ---> create unique folder ---> move the image to the unique folder). All in one single iteration. I put it in answer, take a look.. – BlueTrack Jul 21 '17 at 08:47