Assign image names in folder to images in other folder. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Assign image names in folder to images in other folder. (/thread-12540.html) Pages:
1
2
|
Assign image names in folder to images in other folder. - ujjwalrathod007 - Aug-29-2018 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); RE: Assign image names in folder to images in other folder. - Larz60+ - Aug-29-2018 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? RE: Assign image names in folder to images in other folder. - ujjwalrathod007 - Aug-29-2018 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 RE: Assign image names in folder to images in other folder. - Larz60+ - Aug-29-2018 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. RE: Assign image names in folder to images in other folder. - ujjwalrathod007 - Sep-01-2018 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" ) RE: Assign image names in folder to images in other folder. - Larz60+ - Sep-01-2018 don't ever name a variable the same as a python reserved word 'list' is one such word. This begs for trouble. renaming the image files in a folder with the image files in another folder - ujjwalrathod007 - Sep-01-2018 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.pngI want to give same names as above to the images in another folder with names as following Quote:0000000.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" ) RE: Assign image names in folder to images in other folder. - snippsat - Sep-01-2018 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 RE: Assign image names in folder to images in other folder. - ujjwalrathod007 - Sep-02-2018 I did exactly the same as you suggested. In fact it is giving me an error 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') RE: Assign image names in folder to images in other folder. - Larz60+ - Sep-02-2018 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. |