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.
#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


Messages In This Thread
RE: Assign image names in folder to images in other folder. - by snippsat - Sep-03-2018, 05:27 PM

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