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
|
RE: Assign image names in folder to images in other folder. - snippsat - Sep-02-2018 Can explain in more detail and also do a test Linux(Mint 19). import os path = '/home/tom/Documents/py_files/rename' new_path = '/home/tom/Documents/py_files/new_names' 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')in folder rename(also the images i want to rename): image_1.png image_2.png image_3.png image_4.pngRunning this need 3.6 or newer 3.7. # Check python 3 version tom@tom-VirtualBox:~/Documents/py_files$ python3 -V Python 3.6.5 # Run with python3 tom@tom-VirtualBox:~/Documents/py_files$ python3 rename.pyNow in folder new_names:
Quote:can you tell what does the following lines mean?It's string formatting f-string: >>> new_path = 'foo' >>> index = 9 >>> print(f'{new_path}/{index:03}.png') foo/009.png RE: Assign image names in folder to images in other folder. - ujjwalrathod007 - Sep-02-2018 Here is my Python code. import os import numpy as np list1=os.listdir("/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/") list2=os.listdir("/home/ujjval/Downloads/video-image/data/") list1.sort() list2.sort() path= "/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/" path1="/home/ujjval/Downloads/video-image/data/" print(list1); print(list2); i=1 print(str(path)) for index,img in sorted(enumerate(os.listdir(path), 1)): if img.endswith('.png'): print(index,img) os.rename(img, f'{path1}{index:07}.png') RE: Assign image names in folder to images in other folder. - snippsat - Sep-02-2018 (Sep-02-2018, 07:51 PM)ujjwalrathod007 Wrote: Here is my Python code.You do not change path as i mention. snippsat Wrote:See that i change to path where images are RE: Assign image names in folder to images in other folder. - snippsat - Sep-03-2018 Here how it look with os.path.join() What this dos is generate a absolute path, so now have os.rename() absolute path(full path) to folder where images are placed and the folder that images shall be moved to.import os path = r'E:\div_code\rename' new_path = r'E:\div_code\new' for index,img in sorted(enumerate(os.listdir(path), 1)): if img.endswith('.png'): os.rename(os.path.join(path, img), os.path.join(new_path, f'{index:03}.png')) RE: Assign image names in folder to images in other folder. - ujjwalrathod007 - Sep-03-2018 Let me tell few things again, The code worked with the above suggestions but what I have to do is somewhat different. I have images in folder which I want to rename. path:"/home/ujjval/Downloads/video-image/data/" Quote:0000000.pngWith images in another folder with names path:"/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/ Quote:1305031102.175304.pngfor that I have to write os.rename as follows os.rename(f'{path1}{index:09}.png', img) also the following code is not assigning images in ascending order Order of images is important for input to my Algorithm, for index,img in sorted(enumerate(os.listdir(path),100)): It is no doubt placing some of the names in the correct order but not all!! import os import numpy as np list1=os.listdir("/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/") list2=os.listdir("/home/ujjval/Downloads/video-image/data/") list1.sort() list2.sort() path= "/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/" path1="/home/ujjval/Downloads/video-image/data/" os.chdir(path) print(list1); print(list2); i=1 print(str(path)) for index,img in sorted(enumerate(os.listdir(path),100)): if img.endswith('.png'): print(index,img) os.rename(f'{path1}{index:09}.png', img) RE: Assign image names in folder to images in other folder. - snippsat - Sep-03-2018 (Sep-03-2018, 01:35 PM)ujjwalrathod007 Wrote: also the following code is not assigning images in ascending orderIt can be wrong if just sort the strings,it's called natural sorting(human sorting). So i guess you want to sort after floating value in string. import re from pprint import pprint def try_float(text): try: return float(text) except ValueError: text def natural(text): return [try_float(c) for c in re.split('(\d+.\d+)', text)] lst = [ '1305031102.975304.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', ] lst.sort(key=natural) pprint(lst) See that i changed '1305031102.9 75304.png'Now it's largest float value an are last is list. |