Python Forum
Assign image names in folder to images in other folder.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assign image names in folder to images in other folder.
#1
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);
Reply
#2
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?
Reply
#3
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
Reply
#4
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.
Reply
#5
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" )
Reply
#6
don't ever name a variable the same as a python reserved word 'list' is one such word.
This begs for trouble.
Reply
#7
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" )
Reply
#8
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
Reply
#9
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')
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Script Folder in Python 3.6.2 Download jriemer 6 17,333 Aug-21-2023, 02:34 PM
Last Post: SaWe1995
  Insert images in a folder into dataframe tofi 0 5,354 Dec-14-2018, 08:05 PM
Last Post: tofi
  Batch processing and saving into new folder aeritano 3 4,874 Jun-10-2018, 01:17 AM
Last Post: aeritano
  Backtesting a folder with csv files, problem fiddelush 8 4,681 Mar-13-2018, 11:57 AM
Last Post: fiddelush

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020