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.
#11
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.png
Running 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.py
Now in folder new_names:
Output:
001.png 002.png 003.png 004.png
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
Reply
#12
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')
Reply
#13
(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 os.chdir(path),if not has to use os.path.join() in rename
Reply
#14
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'))
Reply
#15
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.png
0000001.png
0000002.png
0000003.png
With images in another folder with names
path:"/home/ujjval/Downloads/rgbd_dataset_freiburg1_xyz/rgb/
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
for 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)
Reply
#16
(Sep-03-2018, 01:35 PM)ujjwalrathod007 Wrote: also the following code is not assigning images in ascending order
Order of images is important for input to my Algorithm,
It 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)
Output:
['1305031102.211214.png', '1305031102.243211.png', '1305031102.275326.png', '1305031102.311267.png', '1305031102.343233.png', '1305031102.375329.png', '1305031102.411258.png', '1305031102.443271.png', '1305031102.975304.png']
See that i changed '1305031102.975304.png'
Now it's largest float value an are last is list.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Script Folder in Python 3.6.2 Download jriemer 6 17,436 Aug-21-2023, 02:34 PM
Last Post: SaWe1995
  Insert images in a folder into dataframe tofi 0 5,396 Dec-14-2018, 08:05 PM
Last Post: tofi
  Batch processing and saving into new folder aeritano 3 4,920 Jun-10-2018, 01:17 AM
Last Post: aeritano
  Backtesting a folder with csv files, problem fiddelush 8 4,735 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