Python Forum

Full Version: Assign image names in folder to images in other folder.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello all,

I want to use images for input to my algorithm. the images are stored in a file with names and I want to extract those names and give it to the other images in a folder in a given order.

I tried oslistdir() but the order in array generated is different than the file's order. how can I solve this?

import os
import numpy as np

list=os.listdir("/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/")
list2=os.listdir("/home/ujjval/Downloads/video-image/data")

print(list);
print(list2);
Could you elaborate a bit, (example would be nice), and perhaps show what the file looks like.
Does the file contain just names?
The directory just images?
Yes the source directory contains images with names as follows. the images are 480*640 wide...

'1305031114.111263.png', '1305031111.111508.png', '1305031115.879198.png', '1305031128.379404.png', '1305031117.843291.png' etc etc....

I want to give these names to files which also contains images with

'000000179.png', '00000059.png', '000000186.png', '00000094.png', '000000281.png', '000000199.png', '000000181.png', '000000201.png' etc. etc.

but I think I got it now. the files are in acending order so I can use numpy sort function but I will try than tomorrow
load the file into a list, and sort using python sort. Or if you are going to be using numpy anyway, that will also work.
But what about renaming it?
I wrote the following code but it is not working the way I want.

import os
import numpy as np

list=os.listdir("/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/")
list2=os.listdir("/home/ujjval/Downloads/video-image/data/")

list.sort()

print(list);
print(list2);

for i, filename in enumerate(list):
    os.rename("/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/" + filename ,"/home/ujjval/Downloads/video-image/data/" + str(i)+".png" )
don't ever name a variable the same as a python reserved word 'list' is one such word.
This begs for trouble.
I want to rename the images in a folder with images in another folder. after trying a while it seems bit tricky.

My images in the source folder looks like the following
Quote:1305031102.175304.png
1305031102.211214.png
1305031102.243211.png
1305031102.275326.png
1305031102.311267.png
1305031102.343233.png
1305031102.375329.png
1305031102.411258.png
1305031102.443271.png
I want to give same names as above to the images in another folder with names as following

Quote:0000000.png
0000001.png
0000002.png
0000003.png

I have tried os.rename as follows

import os
import numpy as np

path= "/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/"
path1="/home/ujjval/Downloads/video-image/data/"

list1=os.listdir("/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/")
list2=os.listdir("/home/ujjval/Downloads/video-image/data/")

list1.sort()
list2.sort()

print(list1);
print(list2);
i=1
print(str(path))

#for file in path:
 #   for files in path1:
        #os.rename(os.path.join(path,file), os.path.join(path1,files))
        #os.rename("/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/"+ str(file), "/home/ujjval/Downloads/video-image/data/"+str(file) +".png" )
A example.
import os

path = r'E:\div_code\rename'
new_path = r'E:\div_code\new'
os.chdir(path)
for index,img in sorted(enumerate(os.listdir(path), 1)):
    if img.endswith('.png'):
        #print(index, img)
        os.rename(img, f'{new_path}/{index:03}.png')
See that i change to path where images are os.chdir(path),if not has to use os.path.join() in rename.
String formatting used f-string from 3.6-->.
>>> n = 5
>>> print(f'{n:03}')
005
>>> print(f'{n:08}')
00000005
>>> print(f'{n:015}')
000000000000005
I did exactly the same as you suggested. In fact it is giving me an error

Error:
FileNotFoundError: [Errno 2] No such file or directory: '1305031114.111263.png' -> '/home/ujjval/Downloads/video-image/data/0000001.png'
but there is such file as 0000001.png and it should be renamed.!!

can you tell what does the following lines mean?

 os.rename(img, f'{new_path}/{index:03}.png')
Quote:I did exactly the same as you suggested.
can't be sure if can't see code, it could be a single character causing issue.
please, when showing errors, re-display code that is generating the error,
and post the error traceback verbatim.
Pages: 1 2