-3

this the output but i want to arrange these file names in numerical order like(0,1,2,...1000) and not based on the first value(0,1,10,100)

D:\deep>python merge.py
./data/00.jpg
./data/01.jpg
./data/010.jpg
./data/0100.jpg
./data/0101.jpg
./data/0102.jpg
./data/0103.jpg
./data/0104.jpg
./data/0105.jpg
./data/0106.jpg
./data/0107.jpg
./data/0108.jpg
./data/0109.jpg
./data/011.jpg
./data/0110.jpg
./data/0111.jpg
./data/0112.jpg
./data/0113.jpg
./data/0114.jpg
./data/0115.jpg
./data/0116.jpg
./data/0117.jpg
./data/0118.jpg
./data/0119.jpg

the code i used is the below. i want to sort the filenames in a numerical order i tried using sort function with key as int but it dint work

import cv2
import os
import numpy as np

image_folder = 'd:/deep/data'
video_name = 'video.avi'

images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]

print(images)

frame = cv2.imread(os.path.join(image_folder, images[0]))

height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video = cv2.VideoWriter(video_name, fourcc,15.0, (width,height))



for image in images:

    video.write(cv2.imread(os.path.join(image_folder, image)))


cv2.destroyAllWindows()
video.release()
Keren sam
  • 21
  • 1
  • 10

6 Answers6

1

Let's say that your paths are in a list paths. It could also be a generator, of course.

paths = ["./data/00.jpg", "./data/100.jpg", "./data/01.jpg"]

Then

sorted(paths, key=lambda p: int(p[7:-4]))

returns exactly the desired output:

['./data/00.jpg', './data/01.jpg', './data/100.jpg']
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
1

list.sort() accept key function which will be used to compare key values instead of original values when sorting so using regex to pull numbers from string like this and using key would work:

import re
yourlist.sort(key=lambda x: re.search(r'\d+', x).group())

Output:

['./data/00.jpg', './data/01.jpg', './data/010.jpg', './data/0100.jpg', './data/0101.jpg', './data/0102.jpg', './data/0103.jpg', './data/0104.jpg', './data/0105.jpg', './data/0106.jpg', './data/0107.jpg', './data/0108.jpg', './data/0109.jpg', './data/011.jpg', './data/0110.jpg', './data/0111.jpg', './data/0112.jpg', './data/0113.jpg', './data/0114.jpg', './data/0115.jpg', './data/0116.jpg', './data/0117.jpg', './data/0118.jpg', './data/0119.jpg']
zamir
  • 2,144
  • 1
  • 11
  • 23
1

You can do the following

import copy
files = ['./data/00.jpg',
'./data/01.jpg',
'./data/010.jpg',
'./data/0100.jpg',
'./data/0101.jpg',
'./data/0102.jpg',
'./data/0103.jpg',
'./data/0104.jpg',
'./data/0105.jpg',
'./data/0106.jpg',
'./data/0107.jpg',
'./data/0108.jpg',
'./data/0109.jpg',
'./data/011.jpg',
'./data/0110.jpg',
'./data/0111.jpg',
'./data/0112.jpg',
'./data/0113.jpg',
'./data/0114.jpg',
'./data/0115.jpg',
'./data/0116.jpg',
'./data/0117.jpg',
'./data/0118.jpg',
'./data/0119.jpg']

numbers = [int(name.split('/')[2][:-4]) for name in files]
unsorted_numbers = copy.copy(numbers)
numbers.sort()
indices = [unsorted_numbers.index(i) for  i in numbers]
print(numbers)
print(indices)
jeremy_rutman
  • 3,552
  • 4
  • 28
  • 47
1
lst = ['./data/00.jpg'
       './data/01.jpg'
       './data/010.jpg'
       './data/0100.jpg'
       './data/0101.jpg'
       './data/0102.jpg'
       './data/0103.jpg'
       './data/0104.jpg'
       './data/0105.jpg'
       './data/0106.jpg'
       './data/0107.jpg'
       './data/0108.jpg'
       './data/0109.jpg'
       './data/011.jpg'
       './data/0110.jpg'
       './data/0111.jpg'
       './data/0112.jpg'
       './data/0113.jpg'
       './data/0114.jpg'
       './data/0115.jpg'
       './data/0116.jpg'
       './data/0117.jpg'
       './data/0118.jpg'
       './data/0119.jpg']

sorted(lst, key=lambda x: int(x[x.rfind('/') + 1: x.rfind('.')]))
print(lst)

output

['./data/00.jpg./data/01.jpg./data/010.jpg./data/0100.jpg./data/0101.jpg./data/0102.jpg./data/0103.jpg./data/0104.jpg./data/0105.jpg./data/0106.jpg./data/0107.jpg./data/0108.jpg./data/0109.jpg./data/011.jpg./data/0110.jpg./data/0111.jpg./data/0112.jpg./data/0113.jpg./data/0114.jpg./data/0115.jpg./data/0116.jpg./data/0117.jpg./data/0118.jpg./data/0119.jpg']
balderman
  • 22,927
  • 7
  • 34
  • 52
0

you can sort the list using key which will split the string by \ taking the last element then spliting by . taking the first element. and converting this to an int. This will mean the key will be sorted by integer value.

data = """./data/00.jpg
./data/01.jpg
./data/010.jpg
./data/0100.jpg
./data/0101.jpg
./data/0102.jpg
./data/0103.jpg
./data/0104.jpg
./data/0105.jpg
./data/0106.jpg
./data/0107.jpg
./data/0108.jpg
./data/0109.jpg
./data/011.jpg
./data/0110.jpg
./data/0111.jpg
./data/0112.jpg
./data/0113.jpg
./data/0114.jpg
./data/0115.jpg
./data/0116.jpg
./data/0117.jpg
./data/0118.jpg
./data/0119.jpg"""

files = data.splitlines()
files.sort(key=lambda x: int(x.split('/')[-1].split('.')[0]))
print(files)

OUTPUT

['./data/00.jpg', './data/01.jpg', './data/010.jpg', './data/011.jpg', './data/0100.jpg', './data/0101.jpg', './data/0102.jpg', './data/0103.jpg', './data/0104.jpg', './data/0105.jpg', './data/0106.jpg', './data/0107.jpg', './data/0108.jpg', './data/0109.jpg', './data/0110.jpg', './data/0111.jpg', './data/0112.jpg', './data/0113.jpg', './data/0114.jpg', './data/0115.jpg', './data/0116.jpg', './data/0117.jpg', './data/0118.jpg', './data/0119.jpg']
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
0

the following worked:


images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
images.sort(key=lambda x: int(''.join(filter(str.isdigit, x))))
print(images)

Keren sam
  • 21
  • 1
  • 10